Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Have any option set new value default to true? #62

Open
zlx opened this issue Aug 11, 2014 · 6 comments
Open

Have any option set new value default to true? #62

zlx opened this issue Aug 11, 2014 · 6 comments

Comments

@zlx
Copy link

zlx commented Aug 11, 2014

Let's look an example:

Suppose we have an attribute push_settings config as below

class User < ActiveRecord::Base
  bitmask :push_settings, :as => [:push, :push1]
end

now we need to add a new push setting and default is true, we must first add it into as array:

class User < ActiveRecord::Base
  bitmask :push_settings, :as => [:push, :push1, :push2]
end

and run an task to migrate all exists data like below:

User.find_each do |u|
  u.push_settings << :push2
  u.save
end

_And in this case, almost every time add new push, it will default to be true(means it will always push)._

_So there are any option can support it?_, it seems

class User < ActiveRecord::Base
  bitmask :push_settings, :as => [:push, :push1, :push2], :revert => true
end

when you add new push, eg: :push2, without migration the exists data it default will be true, such as

user = User.first
user.push_settings?(:push2)  #=> true
# before add :push2
# user.push_settings #=> [:push]
user.push_settings #=> [:push, :push2]
User.with_push_settings(::push2)  # will include user
User.with_any_push_settings(::push2)  # will include user
User.with_exact_push_settings(::push2)  # will exclude user
@braddunbar
Copy link

Hi @zlx! I'm pretty sure you can use the default option for new models. Does that not work for your case?

@zlx
Copy link
Author

zlx commented Aug 12, 2014

@braddunbar Does the default option also work with the legacy data, which exist before change the bitmask settings.

I find it just define an after_initialize, am I missing something? Thanks

@braddunbar
Copy link

No, but I assume that you would pair this with a migration. If that doesn't work you could probably add the value in your own after_initialize block.

@zlx
Copy link
Author

zlx commented Aug 13, 2014

You are right, But in my scenario, the new added value is always default true, while the default behaviour is false, so if it can be config, we needn't create migrate every time, especially when the legacy data is large, the job is so hard. I will try to make a patch for this

@braddunbar
Copy link

Well, I noticed in your migration you're calling save on each model.

User.find_each do |u|
  u.push_settings << :push2
  u.save
end

This will take forever. Instead, I'd recommend doing this via #update_all, though you'll have to account for any hooks as they will not be run. In your case it'd be something like the following:

User.update_all('users.push_settings = users.push_settings | 4')

This will likely satisfy your speed requirements and obviate the need for the after_initialize hook.

@zlx
Copy link
Author

zlx commented Aug 14, 2014

Good method, I also make a pull request for this option, hope can help somebody like me

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants