Class: CouchFoo::Reflection::AssociationReflection

Inherits:
MacroReflection show all
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

#couch_foo

Instance Method Summary collapse

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_keyObject



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 options[:through]
    if through_reflection.nil?
      raise HasManyThroughAssociationNotFoundError.new(couch_foo.name, self)
    end
    
    if source_reflection.nil?
      raise HasManyThroughSourceAssociationNotFoundError.new(self)
    end

    if options[:source_type] && source_reflection.options[:polymorphic].nil?
      raise HasManyThroughAssociationPointlessSourceTypeError.new(couch_foo.name, self, source_reflection)
    end
    
    if source_reflection.options[:polymorphic] && options[:source_type].nil?
      raise HasManyThroughAssociationPolymorphicError.new(couch_foo.name, self, source_reflection)
    end
    
    unless [:belongs_to, :has_many].include?(source_reflection.macro) && source_reflection.options[:through].nil?
      raise HasManyThroughSourceAssociationMacroError.new(self)
    end
  end
end

#counter_cache_propertyObject



148
149
150
151
152
153
154
# File 'lib/couch_foo/reflection.rb', line 148

def counter_cache_property
  if options[:counter_cache] == true
    "#{couch_foo.name.underscore.pluralize}_count"
  elsif options[:counter_cache]
    options[:counter_cache]
  end
end

#klassObject

: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_nameObject



140
141
142
# File 'lib/couch_foo/reflection.rb', line 140

def primary_key_name
  @primary_key_name ||= options[:foreign_key] || derive_primary_key_name
end

#source_reflectionObject

Gets the source of the through reflection. It checks both a singularized and pluralized form for :belongs_to or :has_many. (The :tags association on Tagging below.)

class Post < CouchFoo::Base
  has_many :taggings
  has_many :tags, :through => :taggings
end


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_namesObject

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 ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }
end

#through_reflectionObject

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

tags_reflection = Post.reflect_on_association(:tags)
taggings_reflection = tags_reflection.through_reflection


167
168
169
# File 'lib/couch_foo/reflection.rb', line 167

def through_reflection
  @through_reflection ||= options[:through] ? couch_foo.reflect_on_association(options[:through]) : false
end