Class: CouchFoo::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- CouchFoo::Reflection::AssociationReflection
- Defined in:
- lib/couch_foo/reflection.rb
Overview
Holds all the meta-data about an association as it was specified in the Couch Foo class.
Instance Attribute Summary
Attributes inherited from MacroReflection
Instance Method Summary collapse
- #association_foreign_key ⇒ Object
- #check_validity! ⇒ Object
- #counter_cache_property ⇒ Object
-
#klass ⇒ Object
:nodoc:.
- #primary_key_name ⇒ Object
-
#source_reflection ⇒ Object
Gets the source of the through reflection.
-
#source_reflection_names ⇒ Object
Gets an array of possible
:through
source reflection names:. -
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the
:through
option of a HasManyThrough or HasOneThrough association.
Methods inherited from MacroReflection
#==, #class_name, #document_class_name, #initialize, #macro, #name, #options, #sanitized_conditions
Constructor Details
This class inherits a constructor from CouchFoo::Reflection::MacroReflection
Instance Method Details
#association_foreign_key ⇒ Object
144 145 146 |
# File 'lib/couch_foo/reflection.rb', line 144 def association_foreign_key @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key end |
#check_validity! ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/couch_foo/reflection.rb', line 192 def check_validity! if [:through] if through_reflection.nil? raise HasManyThroughAssociationNotFoundError.new(couch_foo.name, self) end if source_reflection.nil? raise HasManyThroughSourceAssociationNotFoundError.new(self) end if [:source_type] && source_reflection.[:polymorphic].nil? raise HasManyThroughAssociationPointlessSourceTypeError.new(couch_foo.name, self, source_reflection) end if source_reflection.[:polymorphic] && [:source_type].nil? raise HasManyThroughAssociationPolymorphicError.new(couch_foo.name, self, source_reflection) end unless [:belongs_to, :has_many].include?(source_reflection.macro) && source_reflection.[:through].nil? raise HasManyThroughSourceAssociationMacroError.new(self) end end end |
#counter_cache_property ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/couch_foo/reflection.rb', line 148 def counter_cache_property if [:counter_cache] == true "#{couch_foo.name.underscore.pluralize}_count" elsif [:counter_cache] [:counter_cache] end end |
#klass ⇒ Object
:nodoc:
136 137 138 |
# File 'lib/couch_foo/reflection.rb', line 136 def klass @klass ||= couch_foo.send(:compute_type, class_name) end |
#primary_key_name ⇒ Object
140 141 142 |
# File 'lib/couch_foo/reflection.rb', line 140 def primary_key_name @primary_key_name ||= [:foreign_key] || derive_primary_key_name end |
#source_reflection ⇒ Object
187 188 189 190 |
# File 'lib/couch_foo/reflection.rb', line 187 def source_reflection return nil unless through_reflection @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first end |
#source_reflection_names ⇒ Object
Gets an array of possible :through
source reflection names:
[:singularized, :pluralized]
175 176 177 |
# File 'lib/couch_foo/reflection.rb', line 175 def source_reflection_names @source_reflection_names ||= ([:source] ? [[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym } end |
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the :through
option of a HasManyThrough or HasOneThrough association. Example:
class Post < CouchFoo::Base
has_many :taggings
has_many :tags, :through => :taggings
end
= Post.reflect_on_association(:tags)
taggings_reflection = .through_reflection
167 168 169 |
# File 'lib/couch_foo/reflection.rb', line 167 def through_reflection @through_reflection ||= [:through] ? couch_foo.reflect_on_association([:through]) : false end |