Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow defining datetime fields for scoping on the fly

  • Loading branch information...
commit 79912a34e4538f3ea78bdf497134a985211c61e2 1 parent 6950315
@chochkov authored
Showing with 26 additions and 4 deletions.
  1. +11 −4 lib/activerecord-periodic/scopes.rb
  2. +15 −0 spec/scopes_spec.rb
View
15 lib/activerecord-periodic/scopes.rb
@@ -24,11 +24,18 @@ def self.included(klass)
if span.finite?
- column = pair.first.periods_has_time_span_scopes_column ||
- raise(::ActiveRecord::Periodic::NoColumnGiven)
+ column =
+ if pair.first.kind_of? Class
+ c = pair.first.periods_has_time_span_scopes_column
+ pair.first.arel_table[c]
+ elsif pair.first.kind_of? Symbol
+ klass.arel_table[pair.first]
+ else
+ raise ::ActiveRecord::Periodic::NoColumnGiven
+ end
- memo.where(pair.first.arel_table[column].gteq(span.beginning.to_s(:db))).
- where(pair.first.arel_table[column].lt(span.end.to_s(:db)))
+ memo.where(column.gteq(span.beginning.to_s(:db))).
+ where(column.lt(span.end.to_s(:db)))
else
memo
end
View
15 spec/scopes_spec.rb
@@ -70,3 +70,18 @@
end
end
+describe 'Scoping on custom attributes' do
+ before(:each) do
+ @ototooy = Time.now - 2.days
+ Factory(:user, :registered_on => @ototooy)
+ Factory(:user, :registered_on => @ototooy)
+ Factory(:user, :first_purchase_on => @ototooy + 1.day)
+ end
+
+ it "should let you define the DB column on the fly" do
+ expr = "the day #{@ototooy}"
+ User.span(expr).size.should eq 2
+ User.span(:first_purchase_on => "the day #{@ototooy + 1.day}").size.should eq 1
+ User.span(expr, :first_purchase_on => expr).size.should eq 0
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.