Class: Granite::Form::Model::Associations::Reflections::ReferencesAny

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

Direct Known Subclasses

ReferencesMany, ReferencesOne

Constant Summary

Constants inherited from Base

Base::READ, Base::WRITE

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #parent_reflection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

association_class, #belongs_to?, #build_association, #collection?, #default, generate_methods, #macro

Constructor Details

#initialize(name, *args) ⇒ ReferencesAny

Returns a new instance of ReferencesAny.



20
21
22
23
24
# File 'lib/granite/form/model/associations/reflections/references_any.rb', line 20

def initialize(name, *args)
  @options = args.extract_options!
  @scope_proc = args.first
  @name = name.to_sym
end

Class Method Details

.build(_target, generated_methods, name, *args) ⇒ Object



7
8
9
10
11
# File 'lib/granite/form/model/associations/reflections/references_any.rb', line 7

def self.build(_target, generated_methods, name, *args)
  reflection = new(name, *args)
  generate_methods name, generated_methods
  reflection
end

.persistence_adapter(klass) ⇒ Object



13
14
15
16
# File 'lib/granite/form/model/associations/reflections/references_any.rb', line 13

def self.persistence_adapter(klass)
  adapter = klass.granite_persistence_adapter if klass.respond_to?(:granite_persistence_adapter)
  adapter or raise PersistenceAdapterMissing, klass
end

Instance Method Details

#embedded?Boolean

Returns:



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

def embedded?
  false
end

#inspectObject



53
54
55
# File 'lib/granite/form/model/associations/reflections/references_any.rb', line 53

def inspect
  "#{self.class.name.demodulize}(#{persistence_adapter.data_type})"
end

#klassObject Also known as: data_source



26
27
28
29
30
31
32
# File 'lib/granite/form/model/associations/reflections/references_any.rb', line 26

def klass
  @klass ||= if options[:data_source].present?
               options[:data_source].to_s.constantize
             else
               super
             end
end

#persistence_adapterObject



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

def persistence_adapter
  @persistence_adapter ||= self.class.persistence_adapter(klass)
                               .new(data_source, options[:primary_key], @scope_proc)
end

#read_source(object) ⇒ Object



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

def read_source(object)
  object.read_attribute(reference_key)
end

#write_source(object, value) ⇒ Object



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

def write_source(object, value)
  object.write_attribute(reference_key, value)
end