Class: Attributable::Association

Inherits:
Object
  • Object
show all
Defined in:
app/models/attributable/association.rb

Defined Under Namespace

Modules: Target

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @owner = owner
  @name = name
  @method = method
  @required = options.delete(:required) || false
  @scope = Array(options.delete(:scope))
end

Instance Attribute Details

#nameObject (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_nameObject



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_nameObject



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

#kindObject



57
58
59
# File 'app/models/attributable/association.rb', line 57

def kind
  FieldInfo::SELECTION
end

#optional?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/attributable/association.rb', line 41

def optional?
  !required?
end

#required?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/attributable/association.rb', line 37

def required?
  @required
end

#selection?Boolean

Returns:

  • (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 selection_options(_)
  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: selection_options(nil)
  )
end