Permalink
Browse files

Separate out by_year methods into ByYear module

  • Loading branch information...
1 parent c9fbcf9 commit 5f02642dc9ba10be45b6ea39fe9d479a5e9fd58c @radar committed Feb 17, 2012
Showing with 36 additions and 30 deletions.
  1. +3 −30 lib/by_star.rb
  2. +33 −0 lib/by_star/by_year.rb
View
@@ -1,40 +1,13 @@
+require 'by_star/by_year'
+
module ByStar
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
-
+ include ByYear
end
ActiveRecord::Base.send :extend, ByStar
@@ -0,0 +1,33 @@
+module ByStar
+ module ByYear
+ 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
+end

0 comments on commit 5f02642

Please sign in to comment.