Class: Attributable::Association
- Inherits:
-
Object
- Object
- Attributable::Association
- Defined in:
- app/models/attributable/association.rb
Overview
rubocop:todo Style/Documentation
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
- #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.
28 29 30 31 32 33 34 |
# File 'app/models/attributable/association.rb', line 28 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
26 27 28 |
# File 'app/models/attributable/association.rb', line 26 def name @name end |
Instance Method Details
#assignable_attribute_name ⇒ Object
44 45 46 |
# File 'app/models/attributable/association.rb', line 44 def assignable_attribute_name :"#{@name}_#{@method}" end |
#configure(target) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/attributable/association.rb', line 82 def configure(target) 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
48 49 50 51 |
# File 'app/models/attributable/association.rb', line 48 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
40 41 42 |
# File 'app/models/attributable/association.rb', line 40 def optional? !required? end |
#required? ⇒ Boolean
36 37 38 |
# File 'app/models/attributable/association.rb', line 36 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 |