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


28
29
30
31
32
33
34
# File 'app/models/attributable/association.rb', line 28

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


26
27
28
# File 'app/models/attributable/association.rb', line 26

def name
  @name
end

Instance Method Details

#assignable_attribute_nameObject


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_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


48
49
50
51
# File 'app/models/attributable/association.rb', line 48

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)

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

def optional?
  !required?
end

#required?Boolean

Returns:

  • (Boolean)

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

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