cherring-rangetastic 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +9 -1
- data/Rakefile +1 -1
- data/lib/rangetastic.rb +26 -1
- data/rangetastic.gemspec +2 -2
- data/spec/rangetastic_spec.rb +13 -0
- metadata +2 -2
data/README.markdown
CHANGED
@@ -23,4 +23,12 @@ You can then chain the between scope with any named_scope call you make:
|
|
23
23
|
|
24
24
|
Order.fulfilled.between(1.week.ago, 10.minutes.ago, "fulfilled_on")
|
25
25
|
|
26
|
-
|
26
|
+
Now you can also access this with the following syntax for any field ending with _on that is on the Model you are calling from like so.
|
27
|
+
|
28
|
+
Order.fulfilled.ordered_between(1.week.ago, 10.minutes.ago)
|
29
|
+
|
30
|
+
But if that field isn't on the Model or isn't an _on field then it will raise the standard NoMethodError.
|
31
|
+
|
32
|
+
And if you (or someone nasty!) try to use a field that is not whitelisted, it will raise ActiveRecord::StatementInvalid
|
33
|
+
|
34
|
+
Thanks to spraints for some inspiration.
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('rangetastic', '0.
|
5
|
+
Echoe.new('rangetastic', '0.2.0') do |p|
|
6
6
|
p.description = "Chain a date range to any named_scope on any date field with specified white listed fields"
|
7
7
|
p.url = "http://github.com/cherring/rangetastic"
|
8
8
|
p.author = "Chris Herring"
|
data/lib/rangetastic.rb
CHANGED
@@ -6,11 +6,36 @@ module Rangetastic
|
|
6
6
|
|
7
7
|
module ClassMethods
|
8
8
|
def acts_as_rangetastic(options = {:fields => []})
|
9
|
+
@fields = options[:fields]
|
9
10
|
named_scope :between, lambda{ |start_date, end_date, fieldname|
|
10
|
-
field =
|
11
|
+
field = @fields.include?(fieldname) ? fieldname : raise(ActiveRecord::StatementInvalid)
|
11
12
|
{ :conditions => ["#{field} >= ? AND #{field} <= ?", start_date, end_date] }
|
12
13
|
}
|
13
14
|
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def method_missing(symbol, *args, &block)
|
18
|
+
if (columns_hash.has_key? field(symbol)) || @fields.has_key?(field(symbol))
|
19
|
+
make_scope(symbol, field(symbol), *args)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def field(symbol)
|
26
|
+
"#{symbol.to_s.gsub("_between","")}_on"
|
27
|
+
end
|
28
|
+
|
29
|
+
def make_scope(scope, field, *args)
|
30
|
+
named_scope scope, lambda{ |start_date, end_date|
|
31
|
+
{ :conditions => ["#{field} BETWEEN ? AND ?", start_date, end_date] }
|
32
|
+
}
|
33
|
+
call_scope(scope, *args)
|
34
|
+
end
|
35
|
+
|
36
|
+
def call_scope(scope, *args)
|
37
|
+
self.send(scope, args[0], args[1])
|
38
|
+
end
|
14
39
|
end
|
15
40
|
end
|
16
41
|
|
data/rangetastic.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{rangetastic}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Chris Herring"]
|
9
|
-
s.date = %q{2009-07-
|
9
|
+
s.date = %q{2009-07-03}
|
10
10
|
s.description = %q{Chain a date range to any named_scope on any date field with specified white listed fields}
|
11
11
|
s.email = %q{chris.herring.iphone@gmail.com}
|
12
12
|
s.extra_rdoc_files = ["lib/rangetastic.rb", "README.markdown"]
|
data/spec/rangetastic_spec.rb
CHANGED
@@ -26,4 +26,17 @@ describe Order do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
it "should have 5 orders fulfilled that were ordered no more than 10 days ago - 3" do
|
30
|
+
Order.fulfilled.ordered_between(10.days.ago, 1.day.ago).size.should == 5
|
31
|
+
end
|
32
|
+
|
33
|
+
%w(ordered shipped fulfilled).each do |field|
|
34
|
+
it "should be able to access #{field}_between" do
|
35
|
+
lambda{ Order.send("#{field}_between", 1.day.ago, Date.today) }.should_not raise_error(NoMethodError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should raise no method error when a field doesn't exist" do
|
40
|
+
lambda{ Order.eaten_between(1.day.ago, Date.today) }.should raise_error(NoMethodError)
|
41
|
+
end
|
29
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cherring-rangetastic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Herring
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-03 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|