Class: Formed::Associations::Association
- Inherits:
-
Object
- Object
- Formed::Associations::Association
- Defined in:
- lib/formed/associations/association.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Attribute Summary collapse
-
#disable_joins ⇒ Object
readonly
Returns the value of attribute disable_joins.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #extensions ⇒ Object
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#initialize_attributes(record, except_from_scope_attributes = nil) ⇒ Object
:nodoc:.
- #klass ⇒ Object
- #load_target ⇒ Object
-
#loaded! ⇒ Object
Asserts the target has been loaded setting the loaded flag to
true. - #loaded? ⇒ Boolean
-
#marshal_dump ⇒ Object
We can’t dump @reflection and @through_reflection since it contains the scope proc.
- #marshal_load(data) ⇒ Object
-
#reload(force = false) ⇒ Object
Reloads the target and returns
selfon success. - #reset ⇒ Object
-
#reset_negative_cache ⇒ Object
:nodoc:.
- #reset_scope ⇒ Object
- #scope ⇒ Object
-
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible.
- #stale_target? ⇒ Boolean
Constructor Details
#initialize(owner, reflection) ⇒ Association
Returns a new instance of Association.
10 11 12 13 14 15 16 17 18 |
# File 'lib/formed/associations/association.rb', line 10 def initialize(owner, reflection) reflection.check_validity! @owner = owner @reflection = reflection reset reset_scope end |
Instance Attribute Details
#disable_joins ⇒ Object (readonly)
Returns the value of attribute disable_joins.
6 7 8 |
# File 'lib/formed/associations/association.rb', line 6 def disable_joins @disable_joins end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
6 7 8 |
# File 'lib/formed/associations/association.rb', line 6 def owner @owner end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
6 7 8 |
# File 'lib/formed/associations/association.rb', line 6 def reflection @reflection end |
#target ⇒ Object
Returns the value of attribute target.
6 7 8 |
# File 'lib/formed/associations/association.rb', line 6 def target @target end |
Instance Method Details
#extensions ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/formed/associations/association.rb', line 78 def extensions extensions = reflection.extensions extensions |= reflection.scope_for(klass.unscoped, owner).extensions if reflection.scope extensions end |
#initialize_attributes(record, except_from_scope_attributes = nil) ⇒ Object
:nodoc:
107 108 109 110 111 112 113 114 115 |
# File 'lib/formed/associations/association.rb', line 107 def initialize_attributes(record, except_from_scope_attributes = nil) # :nodoc: except_from_scope_attributes ||= {} skip_assign = [reflection.foreign_key, reflection.type].compact assigned_keys = record.changed assigned_keys += except_from_scope_attributes.keys.map(&:to_s) attributes = {}.except!(*(assigned_keys - skip_assign)) record.send(:_assign_attributes, attributes) if attributes.any? set_inverse_instance(record) end |
#klass ⇒ Object
74 75 76 |
# File 'lib/formed/associations/association.rb', line 74 def klass reflection.klass end |
#load_target ⇒ Object
86 87 88 89 90 91 |
# File 'lib/formed/associations/association.rb', line 86 def load_target @target = find_target if (@stale_state && stale_target?) || find_target? loaded! unless loaded? target end |
#loaded! ⇒ Object
Asserts the target has been loaded setting the loaded flag to true.
44 45 46 47 |
# File 'lib/formed/associations/association.rb', line 44 def loaded! @loaded = true @stale_state = stale_state end |
#loaded? ⇒ Boolean
39 40 41 |
# File 'lib/formed/associations/association.rb', line 39 def loaded? @loaded end |
#marshal_dump ⇒ Object
We can’t dump @reflection and @through_reflection since it contains the scope proc
94 95 96 97 98 99 |
# File 'lib/formed/associations/association.rb', line 94 def marshal_dump ivars = (instance_variables - i[@reflection @through_reflection]).map do |name| [name, instance_variable_get(name)] end [@reflection.name, ivars] end |
#marshal_load(data) ⇒ Object
101 102 103 104 105 |
# File 'lib/formed/associations/association.rb', line 101 def marshal_load(data) reflection_name, ivars = data ivars.each { |name, val| instance_variable_set(name, val) } @reflection = @owner.class._reflect_on_association(reflection_name) end |
#reload(force = false) ⇒ Object
Reloads the target and returns self on success. The QueryCache is cleared if force is true.
32 33 34 35 36 37 |
# File 'lib/formed/associations/association.rb', line 32 def reload(force = false) reset reset_scope load_target self unless target.nil? end |
#reset ⇒ Object
20 21 22 23 24 |
# File 'lib/formed/associations/association.rb', line 20 def reset @loaded = true @target = nil @stale_state = nil end |
#reset_negative_cache ⇒ Object
:nodoc:
26 27 28 |
# File 'lib/formed/associations/association.rb', line 26 def reset_negative_cache # :nodoc: reset if loaded? && target.nil? end |
#reset_scope ⇒ Object
62 63 64 |
# File 'lib/formed/associations/association.rb', line 62 def reset_scope @association_scope = nil end |
#scope ⇒ Object
58 59 60 |
# File 'lib/formed/associations/association.rb', line 58 def scope target end |
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible
67 68 69 70 71 72 |
# File 'lib/formed/associations/association.rb', line 67 def set_inverse_instance(record) if (inverse = inverse_association_for(record)) inverse.inversed_from(owner) end record end |
#stale_target? ⇒ Boolean
49 50 51 |
# File 'lib/formed/associations/association.rb', line 49 def stale_target? loaded? && @stale_state != stale_state end |