Giter Site home page Giter Site logo

active_flag's People

Contributors

cfillion avatar dependabot[bot] avatar ghiculescu avatar kenn avatar komagata avatar petergoldstein avatar rept avatar tomkra avatar vadimleader avatar ydakuka avatar

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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

active_flag's Issues

Forgetting to set default column value to zero causes "undefined method `>' for false:FalseClass error" errors

I've got my model setup with:

flag :ratings, [:g, :pg, :pg13, :r, :nc17]

And with debug console, the object looks good to my eyes:

>> MyModel.ratings.pairs

=> {"G"=>:g, "Pg"=>:pg, "Pg13"=>:pg13, "R"=>:r, "Nc17"=>:nc17}

I tried to load this into form_with checkboxes using:

<%= form.collection_check_boxes :ratings, MyModel.ratings.pairs, :last, :first %>

and it's throwing the following error:

NoMethodError: undefined method `>' for false:FalseClass
	from /user/.rvm/gems/ruby-2.5.3/gems/active_flag-1.4.0/lib/active_flag/definition.rb:45:in `block in to_array'
	from /user/.rvm/gems/ruby-2.5.3/gems/active_flag-1.4.0/lib/active_flag/definition.rb:45:in `each'
	from /user/.rvm/gems/ruby-2.5.3/gems/active_flag-1.4.0/lib/active_flag/definition.rb:45:in `map'
	from /user/.rvm/gems/ruby-2.5.3/gems/active_flag-1.4.0/lib/active_flag/definition.rb:45:in `to_array'
	from /user/.rvm/gems/ruby-2.5.3/gems/active_flag-1.4.0/lib/active_flag/definition.rb:41:in `to_value'
	from /user/.rvm/gems/ruby-2.5.3/gems/active_flag-1.4.0/lib/active_flag.rb:23:in `block in flag'

Diving further down the debug-hole, it appears read_attribute(column) returns nil, which causes integer to be nil when passed to to_value which is then calling to_array with integer still nil:
``
0> column
=> :ratings

0> read_attribute(column)
=> nil
``

Please add a license file

Please add a license file - if you don't have one in mind, I recommend using choosealicense.com to help choose an appropriate one.

Thanks!

Unpermitted Parameters

@kenn, Cheers for what looks to be the perfect solution for my use case. I followed the README and am attempting to flag two columns, :domains and :venues, on my DataSource model.

class DataSource < ApplicationRecord flag :domains, [:effectiveness, :suitability, :survivability, :interoperability] flag :venues, [:flight_test, :ground_test, :element_testing] ... end

DB schema entries as follows....

t.integer "domains", default: 0, null: false t.integer "venues", default: 0, null: false

The params are permitted in the controller, form is configured IAW the README and displaying properly, but I get....

Unpermitted parameter: :domains, :venues

All other params pass and are saved properly. Any ideas? Thank you.

Retired values

Have you given any thought about being able to retire values? I assume it would just require being able to specify a custom value map instead of building one based on array indices.

Form builder not working

The code to generate the form is not working
= f.collection_check_boxes :languages, Profile.languages.pairs

wrong number of arguments (given 2, expected 4..6)

Would there be a way to resolve this ? Thanks

Query methods isn't correct

Hi,

I believe the query method isn't correct.

Suppose you have this:

class Article < ApplicationRecord
flag :publisheds, [:nl, :fr, :en, :de]

And want to query articles that are published in nl.

You would expect:
Article.where_publisheds(:nl)

To yield the results, however, if you have an article which is published in both English and Dutch (nl) then it won't show up, because the query that is being generated is:

AND ("publisheds" = 1)

This should instead be (at least for postgres):
where (publisheds & 1 > 0);

which will contain all articles that are published for nl.

Document usage with Model.insert_all

It would be very helpful if there was a documented usage of ActiveFlag with insert_all new to the Ruby on Rails 6.

Consider the following dummy example of db/seeds.rb, where the langauges column uses ActiveFlag.

if Rails.env.development?
  now = Time.now
  users = []
  100.times do |n|
    users << {
      name: FFaker::Name.name,
      email: FFaker::Internet.unique.email,
      languages: User.languages.to_a.shuffle[0..2],
      created_at: now,
      updated_at: now
    }
  end
  User.insert_all(users)
end

I have also tried User.languages.maps.values.shuffle[0..2] and some other variations, all resulting in the following error:

ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "languages" violates not-null constraint

Is there a simple way to do so? If you show me how, I don't mind opening PR with README.md updates.

Cheers and thank you for the great work you did implementing this gem.

Addition to README about destructuring arrays in scope methods?

I'm been using ActiveFlag for a bit and got into a situation where I was trying to filter search results by flags. The filtering form would return an array of flags that needed to be present on the objects in order to be returned. I am using where_all_ to filter but ran into troubles feeding the array directly to the method. Maybe I'm a ruby n00b, but it took me a few minutes to figure out I had to destructure the array using the splat operator (*).

@industries = [ :aerospace, :financial ]
Person.where_all_industries(*@industries) 

In the interest of helping out other beginners, would you be interested in a PR that adds a line to the README about this case? More than happy to submit one if so.

The list of flags once used can only be appended to

It might be worth mentioning that once you start using the flags in production, the bit arrays are set and one cant remove an option from the list later on. For example if chinese is not a supported language anymore, you cant remove it from the list since the bit values will change and lead to invalid data.

The simplicity of the array to specify list of values is great but it is rigid.

Do you plan to implement `ActiveFlag::Value#to_h`?

We are using ActiveFlag to manage some permissions.
I am currently implementing an admin page, and I am trying to structure it using Hash to see each flags are active.
So I am checking the keys in the flags to see if the respective flags are up.
For example, using the example in the README, we write

class Profile < ActiveRecord::Base
  flag :languages, [:english, :spanish, :chinese, :french, :japanese]]

  def to_hash_language
    Profile.language.keys
               .map { |language| [language, languages.to_a.include?(language)] }
               .to_h
  end
end

profile.languages.set(:spanish)
profile.to_hash_languages # => { english: false, spanish: true, chinese: false, japanese: false }

I think it would be very useful if this Profile#to_hash_language could be written as profile.languages.to_h # => { english: false, spanish: true, chinese: false, japanese: false }. How about it?

Cut a new release

Hi @kenn, thank you for maintaining this awesome gem ๐Ÿคฉ
I would appreciate it if you could cut a new release any time soon.

Tanks a lot!

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.