albertosaurus / power_enum Goto Github PK
View Code? Open in Web Editor NEWThis project forked from protocool/enumerations_mixin
PowerEnum - Enumerations for Rails done right.
License: Other
This project forked from protocool/enumerations_mixin
PowerEnum - Enumerations for Rails done right.
License: Other
In power_enum module EnumClassMethods, the 'all' method is overridden as,
def all return @all if @all @all = load_all.collect{|val| val.freeze}.freeze end
(Link to the method in power_enum repository)
As Rails 4 has modified its target_scope method in 'lib/active_record/associations/association.rb' (Link to the method in rails repository) when we define any association of any Rails model with a power_enum model, the association throws an error.
Eg;
class BookingStatus < ActiveRecord::Base
acts_as_enumerated
has_and_belongs_to_many :bookings
end
class Booking < ActiveRecord::Base
has_and_belongs_to_many :booking_statuses
end
b = Booking.first
b.booking_statuses
ArgumentError: [] is not an ActiveRecord::Relation
Since you're the one, who can has signing key, it's not possible to insall the gem from Github for others because of lack pf private key:
Errno::ENOENT: No such file or directory - gem-private_key.pem
An error occurred while installing power_enum (1.3.1), and Bundler cannot continue.
Make sure that `gem install power_enum -v '1.3.1'` succeeds before bundling.
As far as I understand the key is necessary only when you build the gem.
So could you add a codition to gemspec, like this: https://github.com/delano/rye/pull/30/files#L2R71
To allow people use the gem from github/forks without removing singing_key
manually.
I think it would be reasonable to deprecate exclude_<enum>
scope methods in favor of without_<enum>
method to make syntax compatible with state machine, since both provide with_*
scopes.
Then instead such call
BookingStatus.update_enumerations_model do
BookingStatus.create :name => 'Foo',
:description => 'Bar',
:active => false
end
It would be possible to make call like this
BookingStatus.update_enumerations_model do |klass|
[....].each do
klass.create(....)
end
end
Instead of this:
Book.enumeration_model_updates_permitted = true
Book.create(:author => "Dostoyevsky F.M.", :name => "Crime and Punishment")
Book.create(:author => "Dostoyevsky F.M.", :name => "The Idiot" )
Book.enumeration_model_updates_permitted = false
It would be better to have syntax like this:
Book.affect_enumeration do
Book.create(:author => "Dostoyevsky F.M.", :name => "Crime and Punishment")
Book.create(:author => "Dostoyevsky F.M.", :name => "The Idiot" )
end
Sometimes it's necessary to update an attribute of enumerated model in migration, but it's not possible since instance variable @attributes
of model is frozen hash.
To avoid it we have to do hacks like this:
# duplicate forzen hash
new_attributes = model.instance_variable_get('@attributes').dup
# Assign duplication back, no we can update model attributes
model.instance_variable_set('@attributes', new_attributes)
# update attribute
model.update_attribute("attr_name", "value")
# Freeze @attributes variable as it was before
new_attributes.freeze
It would be cool to have a helper or something else to handle it.
Hey.
I am using enumerations_mixin for rails-3.2.1 and get the following error:
undefined method `read_inheritable_attribute' for #<Class:0xe14c6bc>
undefined method ` write_inheritable_attribute' for #<Class:0xe14c6bc>
http://apidock.com/rails/Class/read_inheritable_attribute
for rails-3.2.1 this is method not available
it should be replaced by
class_attribute
ArgumentError:
wrong number of arguments (1 for 0)
# power_enum-2.8.0/lib/power_enum/reflection.rb:115:in `association_primary_key'
# activerecord-4.1.10/lib/active_record/relation/predicate_builder.rb:64:in `expand'
# activerecord-4.1.10/lib/active_record/relation/predicate_builder.rb:43:in `block in build_from_hash'
# activerecord-4.1.10/lib/active_record/relation/predicate_builder.rb:21:in `each'
# activerecord-4.1.10/lib/active_record/relation/predicate_builder.rb:21:in `build_from_hash'
# activerecord-4.1.10/lib/active_record/relation/query_methods.rb:954:in `build_where'
# activerecord-4.1.10/lib/active_record/relation/query_methods.rb:572:in `where!'
# activerecord-4.1.10/lib/active_record/relation/query_methods.rb:559:in `where'
# activerecord-4.1.10/lib/active_record/querying.rb:10:in `where'
It would be great to have a migration syntax similar to this to add additional custom columns:
create_enum :city, :description => true do |t|
t.float :latitude
t.float :longitude
end
Why don't you rename the repo to power_enum
?
It would be less confusing... And Google would show better search results for power_enum
query.
Is there a method to verify that enum exist by name?
A few months ago []
used to return nil when String was passed:
Book["Hobbit"] # => nil, there is no Hobbit enum
not it raises exception:
Book["Hobbit"] # => ActiveRecord::RecordNotFound: Couldn't find a Book identified by ("Hobbit")
Example.
class Booking
has_enumerated :booking_status
end
Booking.with_booking_status(:on_hold, :cancelled)
should have the same effect as
ids = [BookingStatus[:on_hold], BookingStatus[:cancelled]].map(&:id)
Booking.where(:booking_status_id => ids)
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.