Skip to content

Commit

Permalink
model included as argument to all expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
astudnev committed Feb 22, 2020
1 parent e406828 commit f71a3bb
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions lib/activecube/common/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/activecube/processor/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion lib/activecube/query/limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lib/activecube/query/measure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/activecube/query/or_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/activecube/query/ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lib/activecube/query/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/activecube/query/slice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions lib/activecube/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/models/metric/amount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/models/metric/count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/models/test/transfer_from_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/models/test/transfer_to_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f71a3bb

Please sign in to comment.