kenn / active_flag Goto Github PK
View Code? Open in Web Editor NEWBit array for ActiveRecord
License: MIT License
Bit array for ActiveRecord
License: MIT License
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
Are there any plans to update this gem to support Rails 6?
Currently it's restricted here: https://github.com/kenn/active_flag/blob/master/active_flag.gemspec#L22
I haven't tested it on R6 yet - it may work totally fine.
@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.
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.
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.
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?
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.
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.
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
``
Is it possible to call set_all! with conditions ?
Entity.where_not_permissions(:enable_support).set_all!(:enable_support) ?
Have you considered adding a where_not_state
scope?
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!
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!
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.