Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow updates similar to Rails enums #58

Open
mthurman opened this issue Dec 21, 2016 · 2 comments
Open

Allow updates similar to Rails enums #58

mthurman opened this issue Dec 21, 2016 · 2 comments
Assignees
Milestone

Comments

@mthurman
Copy link

I swapped out some enums I was using for some flags. Especially in tests where I had to set up specific models, I was changing code like this: model.update(enum_field: :enum_value) to model.update(flags: [:flag1, :flag2]) which causes a null value to get sent to the db.

Instead, I ended up doing something like model.update(flags: 3) with a magic number.

It looks like it might be possible to do something similar to enums (https://github.com/rails/rails/blob/a9dc45459abcd9437085f4dd0aa3c9d0e64e062f/activerecord/lib/active_record/enum.rb#L165). Is there any interest in that?

Or any other recommendations to avoid magic numbers when setting multiple flags at once (without having to have one line of code per flag)?

@pboling
Copy link
Owner

pboling commented May 2, 2017

Yes, that would be beautiful! Please add some tests if you end up working on this. The master build on travis is now passing!

I see two options.

Option 1

Hash to indicate flags on or off setting:

model.update(flags: {flag1: true, flag2: false})

Option 2

Tokens to indicate the flag setting:

model.update(flags: [
  :not_flag1,
  :flag2
])

Right now I am leaning toward the Option 1 hash version because it approximates the way you would use a hash of boolean attributes more closely, and it seems more Railsy.

@pboling pboling added this to the 0.4 milestone May 2, 2017
@pboling pboling self-assigned this May 2, 2017
@AlexDWu
Copy link

AlexDWu commented Nov 17, 2017

relevant? https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/attribute_methods/serialization.rb

though what happens when you have a flag that is sent and your update doesn't mention that set flag.

model.flag3 = true
model.save
model.update(flags: {flag1: true, flag2: false})
model.flag3 # probably should return true

then maybe serializer is not ideal, unless this works with store_accessor?

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

No branches or pull requests

3 participants