shkm / enummer

🏳️‍🌈 Multi enums (aka flags) for Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow strings instead of symbols in the setter

luctus opened this issue · comments

Hey there!

Thanks a lot for this gem! I just discovered it today and it's fantastic! I just have one issue when trying to use forms (rails 7) and enummer.

This is my setup:

# models/member.rb
class Member < ApplicationRecord
  ...
  enummer dietary_restriction: %i[vegetarian vegan celiac lactose_intolerant nut_alergy]
  ...
end

Then, the form.

# _form.erb
<%= form.label :dietary_restriction %>
  <% Member.dietary_restriction.each do |dr| %>
    <%= form.check_box :dietary_restriction, {multiple: true, checked: form.object.send("#{dr}?")}, dr.to_s, '' %>
    <%= form.label :dietary_restriction, value: dr.to_s %>
  <% end %>
<% end %>

At the end, the members_controller try to do an update to the model, but the dietary_restriction is an array of strings, so it doesn't save anything. I had to add this method to the model:

# models/member.rb
class Member < ApplicationRecord
  ...
  enummer dietary_restriction: %i[vegetarian vegan celiac lactose_intolerant nut_alergy]
  before_save :fix_enummer

  def fix_enummer
    self.dietary_restriction.map!{ |dr| dr.to_sym }
  end
  ...
end

Hi @luctus, and thanks for reporting this! My use of enummer thus far has been rather limited (and I haven't actually done anything with forms yet) which is why this wasn't caught.

I'll try to find some time to fix this as soon as I can! Alternatively, if you feel like it, feel free to submit a PR.

hey @shkm! did you have a chance to see the PR? thanks a lot!

hey @shkm! did you have a chance to see the PR? thanks a lot!

Thanks, super busy at the moment but I'll try to get to it on Sunday!

Thanks for the contribution; I"m closing this as it's now resolved -- #14

Amazing! thanks for this gem. It's everything I was looking for :)