Open mthurman opened 7 years ago
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.
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
?
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)
tomodel.update(flags: [:flag1, :flag2])
which causes anull
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)?