Commit 9df6106df71c27340b69b9140f7793b2ca6ce2f9
1 parent
234cdc2c
Exists in
master
and in
4 other branches
Seperate file for calculations
Showing
4 changed files
with
30 additions
and
28 deletions
Show diff stats
lib/groupdate/active_record.rb
... | ... | @@ -0,0 +1,26 @@ |
1 | +module Groupdate | |
2 | + class Calculations | |
3 | + attr_reader :relation | |
4 | + | |
5 | + def initialize(relation) | |
6 | + @relation = relation | |
7 | + end | |
8 | + | |
9 | + def include?(method) | |
10 | + # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb | |
11 | + ActiveRecord::Calculations.method_defined?(method) || custom_calculations.include?(method) | |
12 | + end | |
13 | + | |
14 | + def custom_calculations | |
15 | + return [] if !model.respond_to?(:groupdate_calculation_methods) | |
16 | + model.groupdate_calculation_methods | |
17 | + end | |
18 | + | |
19 | + private | |
20 | + | |
21 | + def model | |
22 | + return if !relation.respond_to?(:klass) | |
23 | + relation.klass | |
24 | + end | |
25 | + end | |
26 | +end | ... | ... |
lib/groupdate/series.rb
... | ... | @@ -27,29 +27,4 @@ module Groupdate |
27 | 27 | nil |
28 | 28 | end |
29 | 29 | end |
30 | - | |
31 | - class Calculations | |
32 | - attr_reader :relation | |
33 | - | |
34 | - def initialize(relation) | |
35 | - @relation = relation | |
36 | - end | |
37 | - | |
38 | - def include?(method) | |
39 | - # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb | |
40 | - ActiveRecord::Calculations.method_defined?(method) || custom_calculations.include?(method) | |
41 | - end | |
42 | - | |
43 | - def custom_calculations | |
44 | - return [] if !model.respond_to?(:groupdate_calculation_methods) | |
45 | - model.groupdate_calculation_methods | |
46 | - end | |
47 | - | |
48 | - private | |
49 | - | |
50 | - def model | |
51 | - return if !relation.respond_to?(:klass) | |
52 | - relation.klass | |
53 | - end | |
54 | - end | |
55 | 30 | end | ... | ... |
test/test_helper.rb
... | ... | @@ -361,9 +361,9 @@ module TestDatabase |
361 | 361 | # custom model calculation methods |
362 | 362 | |
363 | 363 | def test_custom_model_calculation_method |
364 | - create_user "2014-05-01", 1 | |
365 | - create_user "2014-05-01", 2 | |
366 | - create_user "2014-05-03", 3 | |
364 | + create_user "2014-05-01" | |
365 | + create_user "2014-05-01" | |
366 | + create_user "2014-05-03" | |
367 | 367 | |
368 | 368 | expected = { |
369 | 369 | Date.parse("2014-05-01") => 2, | ... | ... |