Skip to content

Commit 7dcc903

Browse files
author
Keith Gabryelski
committed
Merge pull request #37 from eslavich/setting-table_alias-is-persistent
from_partition and from_partition_without_alias should return a relation...
2 parents fcbbeec + d2f9f43 commit 7dcc903

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

lib/partitioned/partitioned_base.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,20 @@ def self.sql_adapter
106106
@sql_adapter = self::SqlAdapter.new(self) unless @sql_adapter.present?
107107
return @sql_adapter
108108
end
109+
110+
def self.arel_table_from_key_values(partition_key_values, as = nil)
111+
@arel_tables ||= {}
112+
new_arel_table = @arel_tables[[partition_key_values, as]]
113+
114+
unless new_arel_table
115+
arel_engine_hash = {:engine => self.arel_engine, :as => as}
116+
new_arel_table = Arel::Table.new(self.partition_table_name(*partition_key_values), arel_engine_hash)
117+
@arel_tables[[partition_key_values, as]] = new_arel_table
118+
end
109119

120+
return new_arel_table
121+
end
122+
110123
#
111124
# In activerecord 3.0 we need to supply an Arel::Table for the key value(s) used
112125
# to determine the specific child table to access.
@@ -115,13 +128,8 @@ def self.sql_adapter
115128
# @param [String] as (nil) the name of the table associated with this Arel::Table
116129
# @return [Arel::Table] the generated Arel::Table
117130
def self.dynamic_arel_table(values, as = nil)
118-
@arel_tables ||= {}
119131
key_values = self.partition_key_values(values)
120-
new_arel_table = @arel_tables[key_values]
121-
arel_engine_hash = {:engine => self.arel_engine}
122-
arel_engine_hash[:as] = as unless as.blank?
123-
new_arel_table = Arel::Table.new(self.partition_table_name(*key_values), arel_engine_hash)
124-
return new_arel_table
132+
return arel_table_from_key_values(key_values, as)
125133
end
126134

127135
#
@@ -131,9 +139,8 @@ def self.dynamic_arel_table(values, as = nil)
131139
# @param [String] as (nil) the name of the table associated with the Arel::Table
132140
# @return [Arel::Table] the generated Arel::Table
133141
def dynamic_arel_table(as = nil)
134-
symbolized_attributes = attributes.symbolize_keys
135-
key_values = Hash[*self.class.partition_keys.map{|name| [name,symbolized_attributes[name]]}.flatten]
136-
return self.class.dynamic_arel_table(key_values, as)
142+
key_values = self.class.partition_key_values(attributes)
143+
return self.class.arel_table_from_key_values(key_values, as)
137144
end
138145

139146
#
@@ -149,10 +156,9 @@ def dynamic_arel_table(as = nil)
149156
#
150157
# @param [*Array<Object>] partition_field the field values to partition on
151158
# @return [Hash] the scoping
152-
def self.from_partition(*partition_field)
153-
table_alias_name = partition_table_alias_name(*partition_field)
154-
from("#{partition_table_name(*partition_field)} AS #{table_alias_name}").
155-
tap{|relation| relation.table.table_alias = table_alias_name}
159+
def self.from_partition(*partition_key_values)
160+
table_alias_name = partition_table_alias_name(*partition_key_values)
161+
return ActiveRecord::Relation.new(self, self.arel_table_from_key_values(partition_key_values, table_alias_name))
156162
end
157163

158164
#
@@ -177,10 +183,8 @@ def self.from_partition(*partition_field)
177183
#
178184
# @param [*Array<Object>] partition_field the field values to partition on
179185
# @return [Hash] the scoping
180-
def self.from_partition_without_alias(*partition_field)
181-
table_alias_name = partition_table_name(*partition_field)
182-
from(table_alias_name).
183-
tap{|relation| relation.table.table_alias = table_alias_name}
186+
def self.from_partition_without_alias(*partition_key_values)
187+
return ActiveRecord::Relation.new(self, self.arel_table_from_key_values(partition_key_values, nil))
184188
end
185189

186190
#
@@ -305,7 +309,7 @@ def self.configurator_dsl
305309
# A reasonable alias for this table
306310
#
307311
partition.table_alias_name lambda {|model, *partition_key_values|
308-
return model.configurator.parent_table_name(*partition_key_values).gsub('.', '_')
312+
return model.table_name
309313
}
310314

311315
#

0 commit comments

Comments
 (0)