Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/select_options/active_record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute_options(name, source_constant = nil) ⇒ Object



3
4
5
6
# File 'lib/select_options/active_record/base.rb', line 3

def self.attribute_options(name, source_constant = nil)
  constant = source_constant.presence || constant_for_attribute(name)
  constant.map { |value| [human_composite_attribute_name(name, value), value] } if constant.present?
end

.composite_attribute_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/select_options/active_record/base.rb', line 17

def self.composite_attribute_name?(name)
  attribute_options(name).present?
end

.constant_for_attribute(name) ⇒ Object



8
9
10
11
# File 'lib/select_options/active_record/base.rb', line 8

def self.constant_for_attribute(name)
  constant_name = name.to_s.pluralize.upcase
  const_get(constant_name) if const_defined?(constant_name)
end

Instance Method Details

#attribute_options(name, source_constant = nil) ⇒ Object



13
14
15
# File 'lib/select_options/active_record/base.rb', line 13

def attribute_options(name, source_constant = nil)
  self.class.attribute_options(name, source_constant)
end

#composite_attribute_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/select_options/active_record/base.rb', line 21

def composite_attribute_name?(name)
  self.class.composite_attribute_name?(name)
end