Class: Attributable::Association
- Inherits:
-
Object
- Object
- Attributable::Association
- Defined in:
- app/models/attributable/association.rb
Defined Under Namespace
Modules: Target
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #assignable_attribute_name ⇒ Object
-
#configure(target) ⇒ Object
rubocop:todo Metrics/MethodLength.
- #display_name ⇒ Object
- #find_default(*_args) ⇒ Object
- #from(record) ⇒ Object
-
#initialize(owner, name, method, options = {}) ⇒ Association
constructor
A new instance of Association.
- #kind ⇒ Object
- #optional? ⇒ Boolean
- #required? ⇒ Boolean
- #selection? ⇒ Boolean
- #selection_options(_) ⇒ Object
- #to_field_info(*_args) ⇒ Object
Constructor Details
#initialize(owner, name, method, options = {}) ⇒ Association
Returns a new instance of Association.
30 31 32 33 34 35 36 |
# File 'app/models/attributable/association.rb', line 30 def initialize(owner, name, method, = {}) @owner = owner @name = name @method = method @required = .delete(:required) || false @scope = Array(.delete(:scope)) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
28 29 30 |
# File 'app/models/attributable/association.rb', line 28 def name @name end |
Instance Method Details
#assignable_attribute_name ⇒ Object
46 47 48 |
# File 'app/models/attributable/association.rb', line 46 def assignable_attribute_name :"#{@name}_#{@method}" end |
#configure(target) ⇒ Object
rubocop:todo Metrics/MethodLength
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/attributable/association.rb', line 83 def configure(target) # rubocop:todo Metrics/MethodLength target.class_eval( %{ def #{assignable_attribute_name}=(value) record = self.class.reflections['#{@name}'].klass.find_by_#{@method}(value) or raise ActiveRecord::RecordNotFound, "Could not find #{@name} with #{@method} \#{value.inspect}" send(:#{@name}=, record) end def #{assignable_attribute_name} send(:#{@name}).send(:#{@method}) end } ) end |
#display_name ⇒ Object
54 55 56 |
# File 'app/models/attributable/association.rb', line 54 def display_name Attribute.find_display_name(@owner, name) end |
#find_default(*_args) ⇒ Object
62 63 64 |
# File 'app/models/attributable/association.rb', line 62 def find_default(*_args) nil end |
#from(record) ⇒ Object
50 51 52 |
# File 'app/models/attributable/association.rb', line 50 def from(record) record.send(@name).try(@method) end |
#kind ⇒ Object
58 59 60 |
# File 'app/models/attributable/association.rb', line 58 def kind FieldInfo::SELECTION end |
#optional? ⇒ Boolean
42 43 44 |
# File 'app/models/attributable/association.rb', line 42 def optional? !required? end |
#required? ⇒ Boolean
38 39 40 |
# File 'app/models/attributable/association.rb', line 38 def required? @required end |
#selection? ⇒ Boolean
66 67 68 |
# File 'app/models/attributable/association.rb', line 66 def selection? true end |
#selection_options(_) ⇒ Object
70 71 72 |
# File 'app/models/attributable/association.rb', line 70 def (_) scoped_selection.all.map(&@method.to_sym).sort end |
#to_field_info(*_args) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'app/models/attributable/association.rb', line 74 def to_field_info(*_args) FieldInfo.new( display_name: display_name, key: assignable_attribute_name, kind: kind, selection: (nil) ) end |