GithubHelp home page GithubHelp logo

shkm / enummer Goto Github PK

View Code? Open in Web Editor NEW
28.0 28.0 3.0 46 KB

πŸ³οΈβ€πŸŒˆ Multi enums (aka flags) for Rails

License: MIT License

Ruby 83.51% CSS 0.08% HTML 16.41%
activerecord bitwise-enum enums flags rails ruby

enummer's Introduction

β€œThe programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination.” ― Frederick P. Brooks Jr

enummer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

luctus whatcould 1v

enummer's Issues

Consider adapters

Right now we use the same implementation for any database, but it would be preferable to support optimizations in each. Postgres, for example, provides a bitstring field which was actually the original enummer implementation; whilst it's a little more complex to code around, I believe it can use indexes and should therefore be preferred under that DB.

Allow strings instead of symbols in the setter

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

Add scope for all inclusive bits

E.g. if I do where(permissions: %i[read execute]) it will return all items where permissions contain read OR execute. We'd like a scope that returns only items that include BOTH read AND execute.

This would be something like (bits & column = bits)

Does not recognize falsy values

  test "recognizes boolean params" do
    @user1.update!(ActionController::Parameters.new({"consumes_cigarettes"=>"false"}).permit(:consumes_cigarettes))
    refute @user1.consumes_cigarettes?
  end
Failure:
EnummerTest#test_update_with_prefix2 [test/enummer_test.rb:108]:
Expected true to not be truthy.

Bugged update with prefix

  test "update with prefix" do
    assert @user1.consumes_cigarettes?
    refute @user1.consumes_greens?
    @user1.update!(consumes_cigarettes: true)
    refute @user1.consumes_greens?
  end
Failure:
EnummerTest#test_update_with_prefix [test/enummer_test.rb:103]:
Expected true to not be truthy.

Not saving

Good gem, but unfortunately not saving attribute value:

  test "setting a setter to true adds the value" do
    @user3.read = true

    assert_equal %i[execute read], @user3.permissions

    @user3.save

    assert_equal %i[execute read], @user3.permissions
  end
Failure:
EnummerTest#test_setting_a_setter_to_true_adds_the_value [test/enummer_test.rb:63]:
Expected: [:execute, :read]
  Actual: [:execute]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.