Class: MetaWhere::PolymorphicJoinAssociation

Inherits:
ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
  • Object
show all
Defined in:
lib/meta_where/join_dependency.rb

Instance Method Summary collapse

Constructor Details

#initialize(reflection, join_dependency, polymorphic_class, parent = nil) ⇒ PolymorphicJoinAssociation

Returns a new instance of PolymorphicJoinAssociation.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/meta_where/join_dependency.rb', line 67

def initialize(reflection, join_dependency, polymorphic_class, parent = nil)
  reflection.check_validity!
  @active_record = polymorphic_class
  @cached_record = {}
  @column_names_with_alias = nil
  @reflection      = reflection.clone
  @reflection.instance_variable_set :"@klass", polymorphic_class
  @join_dependency = join_dependency
  @parent          = parent
  @join_type       = Arel::InnerJoin
  @aliased_prefix  = "t#{ join_dependency.join_parts.size }"

  allocate_aliases
  @table = Arel::Table.new(
    table_name, :as => aliased_table_name, :engine => arel_engine
  )
end

Instance Method Details

#==(other) ⇒ Object



86
87
88
89
90
91
# File 'lib/meta_where/join_dependency.rb', line 86

def ==(other)
  other.class == self.class &&
  other.reflection == reflection &&
  other.active_record == active_record &&
  other.parent == parent
end

#join_belongs_to_to(relation) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/meta_where/join_dependency.rb', line 93

def join_belongs_to_to(relation)
  foreign_key = options[:foreign_key] || reflection.foreign_key
  foreign_type = options[:foreign_type] || reflection.foreign_type
  primary_key = options[:primary_key] || reflection.klass.primary_key
  join_target_table(
    relation,
    target_table[primary_key].eq(parent_table[foreign_key]).
    and(parent_table[foreign_type].eq(active_record.name))
  )
end