From f71a3bbecec008b91f45ffebefcd46664ee14107 Mon Sep 17 00:00:00 2001 From: astudnev Date: Sat, 22 Feb 2020 15:27:28 +0300 Subject: [PATCH] model included as argument to all expressions --- lib/activecube/common/metrics.rb | 8 ++++---- lib/activecube/processor/table.rb | 4 ++-- lib/activecube/query/limit.rb | 2 +- lib/activecube/query/measure.rb | 10 +++++----- lib/activecube/query/or_selector.rb | 4 ++-- lib/activecube/query/ordering.rb | 2 +- lib/activecube/query/selector.rb | 10 +++++----- lib/activecube/query/slice.rb | 2 +- lib/activecube/selector.rb | 4 ++-- spec/models/metric/amount.rb | 6 +++--- spec/models/metric/count.rb | 4 ++-- spec/models/test/transfer_from_selector.rb | 4 ++-- spec/models/test/transfer_to_selector.rb | 4 ++-- 13 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/activecube/common/metrics.rb b/lib/activecube/common/metrics.rb index db2d001..dc1dd8e 100644 --- a/lib/activecube/common/metrics.rb +++ b/lib/activecube/common/metrics.rb @@ -7,13 +7,13 @@ module Metrics METHODS.each do |fname| if fname==:count - define_method fname do |arel_table, measure, cube_query| - measure.selectors.empty? ? Arel.star.count : Arel.star.countIf(measure.condition_query arel_table, cube_query) + define_method fname do |model, arel_table, measure, cube_query| + measure.selectors.empty? ? Arel.star.count : Arel.star.countIf(measure.condition_query model, arel_table, cube_query) end else - define_method fname do |arel_table, measure, cube_query| + define_method fname do |model, arel_table, measure, cube_query| column = arel_table[self.class.column_name.to_sym] - measure.selectors.empty? ? column.send(fname) : column.send(fname.to_s+'If', measure.condition_query(arel_table, cube_query)) + measure.selectors.empty? ? column.send(fname) : column.send(fname.to_s+'If', measure.condition_query(model, arel_table, cube_query)) end end diff --git a/lib/activecube/processor/table.rb b/lib/activecube/processor/table.rb index 9e0846e..548bb5b 100644 --- a/lib/activecube/processor/table.rb +++ b/lib/activecube/processor/table.rb @@ -25,7 +25,7 @@ def query cube_query query = table (cube_query.slices + cube_query.measures + cube_query.selectors + cube_query.options).each do |s| - query = s.append_query cube_query, table, query + query = s.append_query model, cube_query, table, query end query @@ -42,7 +42,7 @@ def join cube_query, left_query, right_query using(*dimension_names) cube_query.options.each do |option| - query = option.append_query cube_query, outer_table, query + query = option.append_query model, cube_query, outer_table, query end diff --git a/lib/activecube/query/limit.rb b/lib/activecube/query/limit.rb index 37a49d5..b348794 100644 --- a/lib/activecube/query/limit.rb +++ b/lib/activecube/query/limit.rb @@ -8,7 +8,7 @@ def initialize argument, option @option = option end - def append_query _cube_query, _table, query + def append_query _model, _cube_query, _table, query query.send(option,argument) end diff --git a/lib/activecube/query/measure.rb b/lib/activecube/query/measure.rb index ecf0a42..424ed7f 100644 --- a/lib/activecube/query/measure.rb +++ b/lib/activecube/query/measure.rb @@ -25,19 +25,19 @@ def alias! new_key self.class.new cube, new_key, definition, selectors, modifications end - def condition_query arel_table, cube_query + def condition_query model, arel_table, cube_query condition = nil selectors.each do |selector| condition = condition ? - condition.and(selector.expression(arel_table, cube_query)) : - selector.expression(arel_table, cube_query) + condition.and(selector.expression(model, arel_table, cube_query)) : + selector.expression(model, arel_table, cube_query) end condition end - def append_query cube_query, table, query + def append_query model, cube_query, table, query attr_alias = "`#{key.to_s}`" - expr = definition.expression table, self, cube_query + expr = definition.expression model, table, self, cube_query query.project expr.as(attr_alias) end diff --git a/lib/activecube/query/or_selector.rb b/lib/activecube/query/or_selector.rb index 6be4308..26c4c1a 100644 --- a/lib/activecube/query/or_selector.rb +++ b/lib/activecube/query/or_selector.rb @@ -9,10 +9,10 @@ def initialize selectors @selectors = selectors end - def append_query cube_query, table, query + def append_query model, cube_query, table, query expr = nil selectors.each do |s| - expr = expr ? expr.or(s.expression table, cube_query) : s.expression(table, cube_query) + expr = expr ? expr.or(s.expression model, table, cube_query) : s.expression(model, table, cube_query) end query.where(expr) end diff --git a/lib/activecube/query/ordering.rb b/lib/activecube/query/ordering.rb index 4752c10..cd125be 100644 --- a/lib/activecube/query/ordering.rb +++ b/lib/activecube/query/ordering.rb @@ -8,7 +8,7 @@ def initialize argument, direction @direction = direction end - def append_query _cube_query, _table, query + def append_query _model, _cube_query, _table, query query.order(::Arel.sql(argument.to_s).send(direction)) end diff --git a/lib/activecube/query/selector.rb b/lib/activecube/query/selector.rb index ffb0c64..8185384 100644 --- a/lib/activecube/query/selector.rb +++ b/lib/activecube/query/selector.rb @@ -13,7 +13,7 @@ def initialize operation, argument @argument = argument end - def expression left, right + def expression _model, left, right left.send(operation, right) end @@ -72,12 +72,12 @@ def alias! new_key self.class.new cube, new_key, definition, operator end - def append_query cube_query, table, query - query.where(expression table, cube_query) + def append_query model, cube_query, table, query + query.where(expression model, table, cube_query) end - def expression arel_table, cube_query - definition.expression arel_table, self, cube_query + def expression model, arel_table, cube_query + definition.expression model, arel_table, self, cube_query end def eql?(other) diff --git a/lib/activecube/query/slice.rb b/lib/activecube/query/slice.rb index b5e2063..f81b751 100644 --- a/lib/activecube/query/slice.rb +++ b/lib/activecube/query/slice.rb @@ -24,7 +24,7 @@ def dimension_class definition.class end - def append_query cube_query, table, query + def append_query _model, cube_query, table, query attr_alias = "`#{key.to_s}`" expr = field ? Arel.sql( modifier || field.definition ) : table[dimension_class.column_name] diff --git a/lib/activecube/selector.rb b/lib/activecube/selector.rb index e3dc0ee..6abd126 100644 --- a/lib/activecube/selector.rb +++ b/lib/activecube/selector.rb @@ -4,9 +4,9 @@ module Activecube class Selector extend DefinitionMethods - def expression arel_table, selector, _cube_query + def expression model, arel_table, selector, _cube_query op = selector.operator - op.expression arel_table[self.class.column_name.to_sym], op.argument + op.expression model, arel_table[self.class.column_name.to_sym], op.argument end end diff --git a/spec/models/metric/amount.rb b/spec/models/metric/amount.rb index 67f21ba..bfa47a0 100644 --- a/spec/models/metric/amount.rb +++ b/spec/models/metric/amount.rb @@ -8,11 +8,11 @@ class Amount < Activecube::Metric modifier :calculate - def expression arel_table, measure, cube_query + def expression model, arel_table, measure, cube_query if calculate = measure.modifier(:calculate) - self.send(calculate.args.first, arel_table, measure, cube_query) / Arel.sql(dict_currency_divider('currency_id')) + self.send(calculate.args.first, model, arel_table, measure, cube_query) / Arel.sql(dict_currency_divider('currency_id')) else - sum(arel_table, measure, cube_query) / Arel.sql(dict_currency_divider('currency_id')) + sum(model, arel_table, measure, cube_query) / Arel.sql(dict_currency_divider('currency_id')) end end diff --git a/spec/models/metric/count.rb b/spec/models/metric/count.rb index dbb6102..da062e3 100644 --- a/spec/models/metric/count.rb +++ b/spec/models/metric/count.rb @@ -3,8 +3,8 @@ class Count < Activecube::Metric include Activecube::Common::Metrics - def expression arel_table, measure, cube_query - count(arel_table, measure, cube_query) + def expression model, arel_table, measure, cube_query + count(model, arel_table, measure, cube_query) end end diff --git a/spec/models/test/transfer_from_selector.rb b/spec/models/test/transfer_from_selector.rb index 3c54de2..5a14cdf 100644 --- a/spec/models/test/transfer_from_selector.rb +++ b/spec/models/test/transfer_from_selector.rb @@ -5,9 +5,9 @@ class TransferFromSelector < Activecube::Selector column 'transfer_from_bin' - def expression arel_table, selector, _cube_query + def expression model, arel_table, selector, _cube_query op = selector.operator - op.expression arel_table[self.class.column_name], unhex_bin(op.argument) + op.expression model, arel_table[self.class.column_name], unhex_bin(op.argument) end end diff --git a/spec/models/test/transfer_to_selector.rb b/spec/models/test/transfer_to_selector.rb index 8a64939..c30a5f3 100644 --- a/spec/models/test/transfer_to_selector.rb +++ b/spec/models/test/transfer_to_selector.rb @@ -5,9 +5,9 @@ class TransferToSelector < Activecube::Selector column 'transfer_to_bin' - def expression arel_table, selector, _cube_query + def expression model, arel_table, selector, _cube_query op = selector.operator - op.expression arel_table[self.class.column_name], unhex_bin(op.argument) + op.expression model, arel_table[self.class.column_name], unhex_bin(op.argument) end end