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

flags_as_attributes method? #67

Open
mltsy opened this issue May 9, 2017 · 5 comments
Open

flags_as_attributes method? #67

mltsy opened this issue May 9, 2017 · 5 comments
Assignees
Milestone

Comments

@mltsy
Copy link

mltsy commented May 9, 2017

I'm not sure what a good name for this really is, or the best way for it to work, but I was just writing some tests, and expecting model.attributes to return all my flags as individual Boolean attribute values, which of course, it did not :)

My flag field is called setup_steps so I figured out I could rewrite the test to use selected_setup_steps, but I still think a method to retrieve the flag values in the format of regular attributes (the same way you can set them) would be useful (i.e. { flag1: true, flag2: true, flag3: false })

What I'm not sure about is whether it makes more sense for that to be a model-wide method, like model.attributes_with_flags which would return all the normal attributes, plus additional attributes for each defined flag (regardless of how many flag fields are defined), or to make one method per flag field, like model.setup_steps_as_attributes or something to that effect... that one would be marginally easier to implement at least. Any opinion?

@pboling
Copy link
Owner

pboling commented May 15, 2017

This is a really good idea. Some of the internals for this are already present, but not well exposed. I will work on improving the situation.

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

pboling commented May 15, 2017

@mltsy - One similar feature to one part of your idea is the form builder support:

object_with_flags.as_flag_collection(
    "flags",
    :sent_warm_up_email,
    :not_follow_up_called
)

Another similar thing already in the code is:

object_with_flags.chained_flags_with_signature

For which I'll just copy the documentation from the code:

  Use with chained_flags_with to find records with specific flags
    set to the same values as on this record.
  For a record that has sent_warm_up_email = true and the other flags false:

      user.chained_flags_with_signature
      => [:sent_warm_up_email,
          :not_follow_up_called,
          :not_sent_final_email,
          :not_scheduled_appointment]
      User.chained_flags_with("flags", *user.chained_flags_with_signature)
      => the set of Users that have the same flags set as user.

@mltsy
Copy link
Author

mltsy commented Jun 1, 2017

Oh, hot dang! That totally works 😄

Hash[object.as_flag_collection('flags')]
# => {:flag1 => true, :flag2 => true, :flag3 => false}

bitmoji

Well that would be an easy method to add 😃

@andychongyz
Copy link

andychongyz commented Jul 16, 2019

This is how I do it in the end!

  def attributes
    super.merge(Hash[as_flag_collection('services')].as_json)
  end

@pboling
Copy link
Owner

pboling commented Jul 17, 2019

Sweet Potato. This PR has almost written itself. 👯

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