Module: ActiveRecord::QueryMethods

Defined in:
lib/activerecord_outer_joins/modifications.rb

Instance Method Summary collapse

Instance Method Details

#build_arelObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/activerecord_outer_joins/modifications.rb', line 52

def build_arel
  arel = Arel::SelectManager.new(table.engine, table)

  build_joins(arel, joins_values.flatten) unless joins_values.empty?

  unless outer_joins_values.empty?
    build_outer_joins(arel, outer_joins_values.flatten)
  end
  collapse_wheres(arel, (where_values - ['']).uniq)

  arel.having(*having_values.uniq.reject(&:blank?)) unless having_values.empty?

  arel.take(connection.sanitize_limit(limit_value)) if limit_value
  arel.skip(offset_value.to_i) if offset_value

  arel.group(*group_values.uniq.reject(&:blank?)) unless group_values.empty?

  build_order(arel)

  build_select(arel, select_values.uniq)

  arel.distinct(distinct_value)
  arel.from(build_from) if from_value
  arel.lock(lock_value) if lock_value

  arel
end

#build_join_query(manager, buckets, join_type = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/activerecord_outer_joins/modifications.rb', line 118

def build_join_query(manager, buckets, join_type=nil)
  association_joins         = buckets[:association_join] || []
  stashed_association_joins = buckets[:stashed_join] || []
  join_nodes                = (buckets[:join_node] || []).uniq
  string_joins              = (buckets[:string_join] || []).map(&:strip).uniq

  join_list = join_nodes + custom_join_ast(manager, string_joins)

  join_dependency = ActiveRecord::Associations::JoinDependency.new(
    @klass,
    association_joins,
    join_list,
    join_type
  )


  join_dependency.graft(*stashed_association_joins)

  @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty?

  join_dependency.join_associations.each do |association|
    association.join_to(manager)
  end

  manager.join_sources.concat(join_list)
  manager
end

#build_joins(manager, joins) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/activerecord_outer_joins/modifications.rb', line 98

def build_joins(manager, joins)
  buckets = joins.group_by do |join|
    case join
    when String
      :string_join
    when Hash, Symbol, Array
      :association_join
    when ActiveRecord::Associations::JoinDependency::JoinAssociation
      :stashed_join
    when Arel::Nodes::Join
      :join_node
    else
      raise 'unknown class: %s' % join.class.name
    end
  end

  build_join_query(manager, buckets, Arel::InnerJoin)

end

#build_outer_joins(manager, outer_joins) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/activerecord_outer_joins/modifications.rb', line 80

def build_outer_joins(manager, outer_joins)
  buckets = outer_joins.group_by do |join|
    case join
    when Hash, Symbol, Array
      :association_join
    when ActiveRecord::Associations::JoinDependency::JoinAssociation
      :stashed_join
    when Arel::Nodes::Join
      :join_node
    else
      raise 'unknown class: %s' % join.class.name
    end
  end

  build_join_query(manager, buckets, Arel::OuterJoin)

end

#outer_joins(*args) ⇒ Object



28
29
30
31
# File 'lib/activerecord_outer_joins/modifications.rb', line 28

def outer_joins(*args)
  check_if_method_has_arguments!("outer_joins", args)
  spawn.outer_joins!(*args.compact.flatten)
end

#outer_joins!(*args) ⇒ Object

:nodoc:



33
34
35
36
# File 'lib/activerecord_outer_joins/modifications.rb', line 33

def outer_joins!(*args) # :nodoc:
  self.outer_joins_values += args
  self
end