Class: Granite::Form::Model::Associations::Reflections::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/granite/form/model/associations/reflections/base.rb

Direct Known Subclasses

EmbedsAny, ReferencesAny

Constant Summary collapse

READ =
->(reflection, object) { object.read_attribute reflection.name }
WRITE =
->(reflection, object, value) { object.write_attribute reflection.name, value }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Base

Returns a new instance of Base.



41
42
43
44
# File 'lib/granite/form/model/associations/reflections/base.rb', line 41

def initialize(name, options = {})
  @name = name.to_sym
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/granite/form/model/associations/reflections/base.rb', line 10

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/granite/form/model/associations/reflections/base.rb', line 10

def options
  @options
end

#parent_reflectionObject

AR compatibility



12
13
14
# File 'lib/granite/form/model/associations/reflections/base.rb', line 12

def parent_reflection
  @parent_reflection
end

Class Method Details

.association_classObject



37
38
39
# File 'lib/granite/form/model/associations/reflections/base.rb', line 37

def self.association_class
  @association_class ||= "Granite::Form::Model::Associations::#{name.demodulize}".constantize
end

.build(target, generated_methods, name, options = {}, &_block) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/granite/form/model/associations/reflections/base.rb', line 15

def self.build(target, generated_methods, name, options = {}, &_block)
  generate_methods name, generated_methods
  if options.delete(:validate) &&
      target.respond_to?(:validates_nested) &&
      !target.validates_nested?(name)
    target.validates_nested name
  end
  new(name, options)
end

.generate_methods(name, target) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/granite/form/model/associations/reflections/base.rb', line 25

def self.generate_methods(name, target)
  target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
  def #{name} force_reload = false
    association(:#{name}).reader(force_reload)
  end

  def #{name}= value
    association(:#{name}).writer(value)
  end
  RUBY
end

Instance Method Details

#belongs_to?Boolean

AR compatibility

Returns:



55
56
57
# File 'lib/granite/form/model/associations/reflections/base.rb', line 55

def belongs_to?
  false
end

#build_association(object) ⇒ Object



59
60
61
# File 'lib/granite/form/model/associations/reflections/base.rb', line 59

def build_association(object)
  self.class.association_class.new object, self
end

#collection?Boolean

Returns:



84
85
86
# File 'lib/granite/form/model/associations/reflections/base.rb', line 84

def collection?
  true
end

#default(object) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/granite/form/model/associations/reflections/base.rb', line 71

def default(object)
  defaultizer = options[:default]
  if defaultizer.is_a?(Proc)
    if defaultizer.arity.nonzero?
      defaultizer.call(object)
    else
      object.instance_exec(&defaultizer)
    end
  else
    defaultizer
  end
end

#klassObject



50
51
52
# File 'lib/granite/form/model/associations/reflections/base.rb', line 50

def klass
  @klass ||= (options[:class_name].presence || name.to_s.classify).to_s.constantize
end

#macroObject



46
47
48
# File 'lib/granite/form/model/associations/reflections/base.rb', line 46

def macro
  self.class.name.demodulize.underscore.to_sym
end

#read_source(object) ⇒ Object



63
64
65
# File 'lib/granite/form/model/associations/reflections/base.rb', line 63

def read_source(object)
  (options[:read] || READ).call(self, object)
end

#write_source(object, value) ⇒ Object



67
68
69
# File 'lib/granite/form/model/associations/reflections/base.rb', line 67

def write_source(object, value)
  (options[:write] || WRITE).call(self, object, value)
end