Sha256: ae4feb446bf648b7af442189f56dca28bd6117e97844d7ea39d0bbc7759d5f8a

Contents?: true

Size: 1.01 KB

Versions: 18

Compression:

Stored size: 1.01 KB

Contents

# Copyright (c) 2011 Artem Kuzko
# Released under the MIT license.  See LICENSE for details.

module PowerEnum::Reflection
  extend ActiveSupport::Concern

  module ClassMethods
    def reflect_on_all_enumerated
      # Need to give it a full namespace to avoid getting Rails confused in development
      # mode where all objects are reloaded on every request.
      reflections.values.grep(PowerEnum::Reflection::EnumerationReflection)
    end

    def reflect_on_enumerated( enumerated )
      reflections[enumerated.to_sym].is_a?(PowerEnum::Reflection::EnumerationReflection) ? reflections[enumerated.to_sym] : nil
    end
  end

  class EnumerationReflection < ActiveRecord::Reflection::MacroReflection
    def initialize( name, options, active_record )
      super :has_enumerated, name, options, active_record
    end
    
    def class_name
      @class_name ||= (@options[:class_name] || @name).to_s.camelize
    end
    
    def foreign_key
      @foreign_key ||= (@options[:foreign_key] || "#{@name}_id").to_s
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
power_enum-0.10.0 lib/power_enum/reflection.rb
power_enum-0.9.4 lib/power_enum/reflection.rb
power_enum-0.9.3 lib/power_enum/reflection.rb
power_enum-0.9.2 lib/power_enum/reflection.rb
power_enum-0.9.1 lib/power_enum/reflection.rb
power_enum-0.8.6 lib/power_enum/reflection.rb
power_enum-0.8.5 lib/power_enum/reflection.rb
power_enum-0.8.4 lib/power_enum/reflection.rb
power_enum-0.8.3 lib/power_enum/reflection.rb
power_enum-0.8.2 lib/power_enum/reflection.rb
power_enum-0.8.1 lib/power_enum/reflection.rb
power_enum-0.8.0 lib/power_enum/reflection.rb
power_enum-0.7.0 lib/power_enum/reflection.rb
power_enum-0.6.3 lib/power_enum/reflection.rb
power_enum-0.6.2 lib/power_enum/reflection.rb
power_enum-0.6.1 lib/power_enum/reflection.rb
power_enum-0.6.0 lib/power_enum/reflection.rb
power_enum-0.5.5 lib/power_enum/reflection.rb