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.
29 30 31 32 33 34 35 |
# File 'app/models/attributable/association.rb', line 29 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.
27 28 29 |
# File 'app/models/attributable/association.rb', line 27 def name @name end |
Instance Method Details
#assignable_attribute_name ⇒ Object
45 46 47 |
# File 'app/models/attributable/association.rb', line 45 def assignable_attribute_name :"#{@name}_#{@method}" end |
#configure(target) ⇒ Object
rubocop:todo Metrics/MethodLength
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/models/attributable/association.rb', line 82 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
53 54 55 |
# File 'app/models/attributable/association.rb', line 53 def display_name Attribute.find_display_name(@owner, name) end |
#find_default(*_args) ⇒ Object
61 62 63 |
# File 'app/models/attributable/association.rb', line 61 def find_default(*_args) nil end |
#from(record) ⇒ Object
49 50 51 |
# File 'app/models/attributable/association.rb', line 49 def from(record) record.send(@name).try(@method) end |
#kind ⇒ Object
57 58 59 |
# File 'app/models/attributable/association.rb', line 57 def kind FieldInfo::SELECTION end |
#optional? ⇒ Boolean
41 42 43 |
# File 'app/models/attributable/association.rb', line 41 def optional? !required? end |
#required? ⇒ Boolean
37 38 39 |
# File 'app/models/attributable/association.rb', line 37 def required? @required end |
#selection? ⇒ Boolean
65 66 67 |
# File 'app/models/attributable/association.rb', line 65 def selection? true end |
#selection_options(_) ⇒ Object
69 70 71 |
# File 'app/models/attributable/association.rb', line 69 def (_) scoped_selection.all.map(&@method.to_sym).sort end |
#to_field_info(*_args) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'app/models/attributable/association.rb', line 73 def to_field_info(*_args) FieldInfo.new( display_name: display_name, key: assignable_attribute_name, kind: kind, selection: (nil) ) end |