Class: BigRecord::BrReflection::BrAssociationReflection

Inherits:
MacroReflectionAbstract show all
Defined in:
lib/big_record/br_reflection.rb

Overview

Holds all the meta-data about an association as it was specified in the Big Record class.

Instance Method Summary

Instance Method Details

- (Object) association_foreign_key



71
72
73
# File 'lib/big_record/br_reflection.rb', line 71

def association_foreign_key
  @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
end

- (Object) check_validity!



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/big_record/br_reflection.rb', line 107

def check_validity!
  if options[:through]
    if through_reflection.nil?
      raise HasManyThroughAssociationNotFoundError.new(big_record.name, self)
    end

    if source_reflection.nil?
      raise HasManyThroughSourceAssociationNotFoundError.new(self)
    end

    if options[:source_type] && source_reflection.options[:polymorphic].nil?
      raise HasManyThroughAssociationPointlessSourceTypeError.new(big_record.name, self, source_reflection)
    end

    if source_reflection.options[:polymorphic] && options[:source_type].nil?
      raise HasManyThroughAssociationPolymorphicError.new(big_record.name, self, source_reflection)
    end

    unless [:belongs_to_big_record, :has_many_big_records].include?(source_reflection.macro) && source_reflection.options[:through].nil?
      raise HasManyThroughSourceAssociationMacroError.new(self)
    end
  end
end

- (Object) counter_cache_column



75
76
77
78
79
80
81
# File 'lib/big_record/br_reflection.rb', line 75

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

- (Object) klass



49
50
51
# File 'lib/big_record/br_reflection.rb', line 49

def klass
  @klass ||= big_record.send(:compute_type, class_name)
end

- (Object) primary_key_name



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/big_record/br_reflection.rb', line 57

def primary_key_name
  return @primary_key_name if @primary_key_name
  case
    when macro == :belongs_to_big_record
      @primary_key_name = options[:foreign_key] || class_name.foreign_key
    when macro == :belongs_to_many
      @primary_key_name = options[:foreign_key] || "#{big_record.default_column_prefix}#{class_name.tableize}_ids"
    when options[:as]
      @primary_key_name = options[:foreign_key] || "#{big_record.default_column_prefix}#{options[:as]}_id"
    else
      @primary_key_name = options[:foreign_key] || big_record.name.foreign_key
  end
end

- (Object) source_reflection

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
    has_many :tags, :through => :taggings
  end


102
103
104
105
# File 'lib/big_record/br_reflection.rb', line 102

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

- (Object) source_reflection_names

Gets an array of possible :through source reflection names

[singularized, pluralized]



91
92
93
# File 'lib/big_record/br_reflection.rb', line 91

def source_reflection_names
  @source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }
end

- (Object) table_name



53
54
55
# File 'lib/big_record/br_reflection.rb', line 53

def table_name
  @table_name ||= klass.table_name
end

- (Object) through_reflection



83
84
85
# File 'lib/big_record/br_reflection.rb', line 83

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