71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/calculated_attributes/model_methods.rb', line 71
def calculated(*args)
projections = arel.projections
args = args.flat_map do |arg|
case arg
when Symbol then [[arg, []]]
when Hash then arg.to_a
end
end
args.each do |attribute, arguments|
lam = klass.calculated.calculated[attribute] || klass.base_class.calculated.calculated[attribute]
sql = lam.call(*arguments)
sql = klass.send(:sanitize_sql, *sql) if sql.is_a?(Array)
new_projection =
if sql.is_a?(String)
Arel.sql("(#{sql})").as(attribute.to_s)
elsif sql.respond_to? :to_sql
Arel.sql("(#{sql.to_sql})").as(attribute.to_s)
else
sql.as(attribute.to_s)
end
new_projection.calculated_attr!
projections.push new_projection
end
select(projections)
end
|