Sha256: 65cf4edd0b339c941d838f29704cbd675cf8c2aa1c71a68943b213805af43f95
Contents?: true
Size: 1.49 KB
Versions: 3
Compression:
Stored size: 1.49 KB
Contents
module ActiveRecord class Enum extend ActiveRecord::Enumerations::OptionsHelper attr_reader :id, :name, :extra_columns class_inheritable_accessor :label_method def initialize attrs = {} @id = attrs.delete(:id).to_i @name = attrs.delete(:name).to_s @extra_columns = attrs.reject { |k, _| [:enum_class, :on_style_not_matched].include?(k) } end def self.create_from value, values, options required_attrs = case value when String, Symbol { :name => value } else value end required_attrs[:id] ||= values.index(value) + 1 new options.merge(required_attrs) end def == other return id == other.id if other.is_a?(Enum) [id.to_s, name].include?(other.to_s) end def to_s try_labelize(self, :desc) || try_labelize(name, :titleize) end def to_sym name.to_sym end def self.enumeration *config, &block add_option config, :enum_class => self define_enums_getter ActiveRecord::Enumerations::Factory.make_enums(*config, &block) end def self.[] name_or_id all.detect { |enum| enum == name_or_id } end private def self.define_enums_getter enums cattr_accessor :all self.all = enums end def try_labelize object, default_method object.respond_to?(label_method) && object.send(label_method) or object.respond_to?(default_method) && object.send(default_method) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
ar-enums-0.3.2 | lib/enum.rb |
ar-enums-0.3.1 | lib/enum.rb |
ar-enums-0.3.0 | lib/enum.rb |