Class: ActiveData::Model::Associations::Reflections::ReferenceReflection

Inherits:
Base
  • Object
show all
Defined in:
lib/active_data/model/associations/reflections/reference_reflection.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, #default, generate_methods, #inspect, #klass, #macro

Constructor Details

#initialize(name, *args) ⇒ ReferenceReflection

Returns a new instance of ReferenceReflection.



12
13
14
15
16
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 12

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, &block) ⇒ Object



6
7
8
9
10
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 6

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

Instance Method Details

#embedded?Boolean

Returns:



38
39
40
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 38

def embedded?
  false
end

#primary_keyObject



26
27
28
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 26

def primary_key
  @primary_key ||= options[:primary_key].presence.try(:to_sym) || :id
end

#read_source(object) ⇒ Object



18
19
20
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 18

def read_source object
  object.read_attribute(reference_key)
end

#scopeObject



30
31
32
33
34
35
36
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 30

def scope
  @scope ||= begin
    scope = klass.unscoped
    scope = scope.instance_exec(&@scope_proc) if @scope_proc
    scope
  end
end

#write_source(object, value) ⇒ Object



22
23
24
# File 'lib/active_data/model/associations/reflections/reference_reflection.rb', line 22

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