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.



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

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



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

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

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



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

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



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

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:



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

def belongs_to?
  false
end

#build_association(object) ⇒ Object



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

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

#collection?Boolean

Returns:



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

def collection?
  true
end

#default(object) ⇒ Object



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

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



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

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

#macroObject



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

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

#read_source(object) ⇒ Object



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

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

#write_source(object, value) ⇒ Object



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

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