birddog 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -204,8 +204,8 @@ module Birddog
204
204
  private :define_common_aggregates_for
205
205
 
206
206
  def _common_average_for(model)
207
- define_static_scope(:_birddog_average) do |field, value|
208
- field_name = "average_#{field}"
207
+ define_static_scope(:_birddog_average) do |field, field_alias, value|
208
+ field_name = "#{field_alias}#{field}"
209
209
  scope = { :select => @model.arel_table[field].average.as(field_name) }
210
210
  warn_common_aggregate_conditional if conditional?(value)
211
211
  scope
@@ -214,8 +214,8 @@ module Birddog
214
214
  private :_common_average_for
215
215
 
216
216
  def _common_sum_for(model)
217
- define_static_scope(:_birddog_sum) do |field, value|
218
- field_name = "sum_#{field}"
217
+ define_static_scope(:_birddog_sum) do |field, field_alias, value|
218
+ field_name = "#{field_alias}#{field}"
219
219
  scope = { :select => @model.arel_table[field].sum.as(field_name) }
220
220
  warn_common_aggregate_conditional if conditional?(value)
221
221
  scope
@@ -224,8 +224,8 @@ module Birddog
224
224
  private :_common_sum_for
225
225
 
226
226
  def _common_minimum_for(model)
227
- define_static_scope(:_birddog_minimum) do |field, value|
228
- field_name = "minimum_#{field}"
227
+ define_static_scope(:_birddog_minimum) do |field, field_alias, value|
228
+ field_name = "#{field_alias}#{field}"
229
229
  scope = { :select => @model.arel_table[field].minimum.as(field_name) }
230
230
  warn_common_aggregate_conditional if conditional?(value)
231
231
  scope
@@ -234,8 +234,8 @@ module Birddog
234
234
  private :_common_minimum_for
235
235
 
236
236
  def _common_maximum_for(model)
237
- define_static_scope(:_birddog_maximum) do |field, value|
238
- field_name = "maximum_#{field}"
237
+ define_static_scope(:_birddog_maximum) do |field, field_alias, value|
238
+ field_name = "#{field_alias}#{field}"
239
239
  scope = { :select => @model.arel_table[field].maximum.as(field_name) }
240
240
  warn_common_aggregate_conditional if conditional?(value)
241
241
  scope
@@ -246,33 +246,38 @@ module Birddog
246
246
  def aggregate_scope_for(model, key, value)
247
247
  aggregate_scope = nil
248
248
  field_name = nil
249
+ field_alias = nil
249
250
  key = key.to_s
250
251
 
251
252
  case
252
- when key =~ /^(average_)([a-zA-Z_]*)/ && @averagable.include?($2.to_sym) then
253
+ when key =~ /^(average_|avg_)([a-zA-Z_]*)/ && @averagable.include?($2.to_sym) then
253
254
  aggregate_scope = "_birddog_average"
255
+ field_alias = $1
254
256
  field_name = $2
255
257
  when key =~ /^(sum_)([a-zA-Z_]*)/ && @sumable.include?($2.to_sym) then
256
258
  aggregate_scope = "_birddog_sum"
259
+ field_alias = $1
257
260
  field_name = $2
258
- when key =~ /^(minimum_)([a-zA-Z_]*)/ && @minimumable.include?($2.to_sym) then
261
+ when key =~ /^(minimum_|min_)([a-zA-Z_]*)/ && @minimumable.include?($2.to_sym) then
259
262
  aggregate_scope = "_birddog_minimum"
263
+ field_alias = $1
260
264
  field_name = $2
261
- when key =~ /^(maximum_)([a-zA-Z_]*)/ && @maximumable.include?($2.to_sym) then
265
+ when key =~ /^(maximum_|max_)([a-zA-Z_]*)/ && @maximumable.include?($2.to_sym) then
262
266
  aggregate_scope = "_birddog_maximum"
267
+ field_alias = $1
263
268
  field_name = $2
264
269
  end
265
270
 
266
- [aggregate_scope, field_name]
271
+ [field_name, field_alias, aggregate_scope]
267
272
  end
268
273
  private :aggregate_scope_for
269
274
 
270
275
  def scope_for(model, key, value)
271
- scope_name, field = aggregate_scope_for(model, key, value)
276
+ field, field_alias, scope_name = aggregate_scope_for(model, key, value)
272
277
  scope_name = scope_name_for(key) unless scope_name
273
278
 
274
279
  if model.respond_to?(scope_name)
275
- send_params = [field, value].compact
280
+ send_params = [field, field_alias, value].compact
276
281
  model.__send__(scope_name, *send_params)
277
282
  else
278
283
  model.scoped
@@ -1,3 +1,3 @@
1
1
  module Birddog
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -17,13 +17,23 @@ describe Birddog::Birddog do
17
17
 
18
18
  it "creates a query on the column desired when scope is average_*" do
19
19
  sql = Product.scopes_for_query("average_value").to_sql
20
- sql.must_match(/value/)
20
+ sql.must_match(/average_value/)
21
+ end
22
+
23
+ it "creates a query on the column desired when scope is avg_*" do
24
+ sql = Product.scopes_for_query("avg_value").to_sql
25
+ sql.must_match(/avg_value/)
21
26
  end
22
27
 
23
28
  it "calculates the average and returns it in the result set" do
24
29
  avg = Product.scopes_for_query("average_value").first
25
30
  avg.average_value.must_equal((100 + 200 + 10) / 3.0)
26
31
  end
32
+
33
+ it "calculates the avg and returns it in the result set" do
34
+ avg = Product.scopes_for_query("avg_value").first
35
+ avg.avg_value.must_equal((100 + 200 + 10) / 3.0)
36
+ end
27
37
  end
28
38
 
29
39
  describe "minimumable" do
@@ -33,13 +43,23 @@ describe Birddog::Birddog do
33
43
 
34
44
  it "creates a query on the column desired when scope is minimum_*" do
35
45
  sql = Product.scopes_for_query("minimum_value").to_sql
36
- sql.must_match(/minimum/)
46
+ sql.must_match(/minimum_value/)
47
+ end
48
+
49
+ it "creates a query on the column desired when scope is min_*" do
50
+ sql = Product.scopes_for_query("min_value").to_sql
51
+ sql.must_match(/min_value/)
37
52
  end
38
53
 
39
54
  it "calculates the minimum and returns it in the result set" do
40
55
  avg = Product.scopes_for_query("minimum_value").first
41
56
  avg.minimum_value.must_equal(10)
42
57
  end
58
+
59
+ it "calculates the min and returns it in the result set" do
60
+ avg = Product.scopes_for_query("min_value").first
61
+ avg.min_value.must_equal(10)
62
+ end
43
63
  end
44
64
 
45
65
  describe "maximumable" do
@@ -49,13 +69,23 @@ describe Birddog::Birddog do
49
69
 
50
70
  it "creates a query on the column desired when scope is maximum_*" do
51
71
  sql = Product.scopes_for_query("maximum_value").to_sql
52
- sql.must_match(/maximum/)
72
+ sql.must_match(/maximum_value/)
73
+ end
74
+
75
+ it "creates a query on the column desired when scope is max_*" do
76
+ sql = Product.scopes_for_query("max_value").to_sql
77
+ sql.must_match(/max_value/)
53
78
  end
54
79
 
55
80
  it "calculates the maximum and returns it in the result set" do
56
81
  avg = Product.scopes_for_query("maximum_value").first
57
82
  avg.maximum_value.must_equal(200)
58
83
  end
84
+
85
+ it "calculates the max and returns it in the result set" do
86
+ avg = Product.scopes_for_query("max_value").first
87
+ avg.max_value.must_equal(200)
88
+ end
59
89
  end
60
90
 
61
91
  describe "sumable" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: birddog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &12855260 !ruby/object:Gem::Requirement
16
+ requirement: &12901960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *12855260
24
+ version_requirements: *12901960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &12854640 !ruby/object:Gem::Requirement
27
+ requirement: &12901400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *12854640
35
+ version_requirements: *12901400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3-ruby
38
- requirement: &12854040 !ruby/object:Gem::Requirement
38
+ requirement: &12900740 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *12854040
46
+ version_requirements: *12900740
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &12853440 !ruby/object:Gem::Requirement
49
+ requirement: &12899920 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *12853440
57
+ version_requirements: *12899920
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: chronic
60
- requirement: &12852700 !ruby/object:Gem::Requirement
60
+ requirement: &12899480 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *12852700
68
+ version_requirements: *12899480
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activerecord
71
- requirement: &12852260 !ruby/object:Gem::Requirement
71
+ requirement: &12899060 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *12852260
79
+ version_requirements: *12899060
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: activesupport
82
- requirement: &12851700 !ruby/object:Gem::Requirement
82
+ requirement: &12898400 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *12851700
90
+ version_requirements: *12898400
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: squeel
93
- requirement: &12850900 !ruby/object:Gem::Requirement
93
+ requirement: &12897540 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *12850900
101
+ version_requirements: *12897540
102
102
  description: Seeeeeeeee Readme
103
103
  email:
104
104
  - brandonsdewitt@gmail.com
@@ -139,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
139
  version: '0'
140
140
  segments:
141
141
  - 0
142
- hash: -1855020977643402553
142
+ hash: -3806611019993035456
143
143
  required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  none: false
145
145
  requirements:
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  segments:
150
150
  - 0
151
- hash: -1855020977643402553
151
+ hash: -3806611019993035456
152
152
  requirements: []
153
153
  rubyforge_project: birddog
154
154
  rubygems_version: 1.8.10