Skip to content
Browse files

Re-implement by_year method using ARel

  • Loading branch information...
1 parent 8432bf0 commit c9fbcf9eccd07a5e5e56f23620a8020d329d4b32 @radar committed Feb 17, 2012
Showing with 53 additions and 29 deletions.
  1. +32 −4 lib/by_star.rb
  2. +21 −25 spec/by_star/by_year_spec.rb
View
36 lib/by_star.rb
@@ -1,12 +1,40 @@
module ByStar
- def by_year
- time = Time.zone.now
- where("created_at >= ? AND created_at <= ?",
- time.beginning_of_year, time.end_of_year)
+
+ def by_star_field(field=nil)
+ @by_star_field ||= field
+ @by_star_field || "#{self.table_name}.created_at"
+ end
+
+ def by_year(*args)
+ options = args.extract_options!.symbolize_keys!
+ time = args.first || Time.zone.now
+ klass = case time
+ when ActiveSupport::TimeWithZone
+ Time
+ else
+ time.class
+ end
+
+ send("by_year_#{klass}", time, options)
end
private
+ def by_year_Time(time, options={})
+ field = options[:field] || by_star_field
+ scope = where("#{field} >= ? AND #{field} <= ?",
+ time.beginning_of_year, time.end_of_year)
+ scope = scope.order(options[:order]) if options[:order]
+ scope
+ end
+
+ def by_year_String_or_Fixnum(year, options={})
+ time = "#{year.to_s}-01-01 00:00:00".to_time
+ by_year_Time(time, options)
+ end
+ alias_method :by_year_String, :by_year_String_or_Fixnum
+ alias_method :by_year_Fixnum, :by_year_String_or_Fixnum
+
end
ActiveRecord::Base.send :extend, ByStar
View
46 spec/by_star/by_year_spec.rb
@@ -1,52 +1,48 @@
require 'spec_helper'
-describe "by year" do
+describe "by_year" do
+ def posts_count(*args)
+ find_posts(*args).count
+ end
+
+ def find_posts(*args)
+ options = args.extract_options!
+ Post.by_year(args.first, options)
+ end
+
+ let(:this_years_posts) { 22 }
+
it "should be able to find all the posts in the current year" do
- Post.by_year.count.should eql(22)
+ posts_count.should eql(this_years_posts)
end
it "should be able to find if given a string" do
- size(Time.zone.now.year.to_s).should eql(this_years_posts)
+ posts_count(Time.zone.now.year.to_s).should eql(this_years_posts)
end
it "should be able to find a single post from last year" do
- size(Time.zone.now.year-1).should eql(2)
+ posts_count(Time.zone.now.year-1).should eql(2)
end
it "knows what last year's posts were" do
- find(Time.zone.now.year-1).map(&:text).should eql(["Last year", "End of last year"])
- end
-
- it "should error when given an invalid year" do
- if RUBY_VERSION < "1.8.7"
- lambda { find(1901) }.should raise_error(ByStar::ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039)")
- lambda { find(2039) }.should raise_error(ByStar::ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039)")
- else
- find(1456).should be_empty
- find(1901).should be_empty
- find(2039).should be_empty
- end
+ find_posts(Time.zone.now.year-1).map(&:text).should eql(["Last year", "End of last year"])
end
it "should be able to use an alternative field (string)" do
- Event.by_year(nil, :field => "start_time").size.should eql(8)
+ Event.by_year(nil, :field => "start_time").count.should eql(8)
end
it "should be able to use an alternative field (symbol)" do
- Event.by_year(nil, :field => :start_time).size.should eql(8)
+ Event.by_year(nil, :field => :start_time).count.should eql(8)
end
it "should not have to specify the field when using by_star_field" do
- Event.by_year.size.should eql(8)
- end
-
- it "should be able to use an alternative field (symbol) with directional searching" do
- stub_time
- Event.past(nil, :field => :start_time).size.should eql(1)
+ Event.by_year.count.should eql(8)
end
it "should be able to order the result set" do
- find(Time.zone.now.year, :order => "created_at DESC").first.text.should eql("That's it!")
+ scope = find_posts(Time.zone.now.year, :order => "created_at DESC")
+ scope.order_values.should == ["created_at DESC"]
end
end

0 comments on commit c9fbcf9

Please sign in to comment.
Something went wrong with that request. Please try again.