GithubHelp home page GithubHelp logo

albertosaurus / power_enum Goto Github PK

View Code? Open in Web Editor NEW

This project forked from protocool/enumerations_mixin

21.0 21.0 8.0 675 KB

PowerEnum - Enumerations for Rails done right.

License: Other

Ruby 99.16% Shell 0.17% JavaScript 0.38% CSS 0.29%

power_enum's People

Contributors

akuzko avatar albertosaurus avatar cryo28 avatar greyblake avatar protocool avatar zbelzer avatar

Stargazers

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

Watchers

 avatar  avatar

power_enum's Issues

Associations of powe enum models with other models not working in Rails 4

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;

power_enum model

class BookingStatus < ActiveRecord::Base
acts_as_enumerated
has_and_belongs_to_many :bookings
end

Normal model

class Booking < ActiveRecord::Base
has_and_belongs_to_many :booking_statuses
end

b = Booking.first
b.booking_statuses
ArgumentError: [] is not an ActiveRecord::Relation

Allow to install gem from github (signing key issue).

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.

Deprecate exclude_enum

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.

Put klass into block for `update_enumerations_model`

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

Use block to affect enumerations

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

Update attribute of enum model in migration

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.

Wrong number of arguments after upgrading to Rails 4.1.10

       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'

Ability to specify additional columns

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

Rename to power_enum

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.

Howto verify does enum exist by name

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")

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.