Created
April 8, 2013 13:47
-
-
Save kiela/5336881 to your computer and use it in GitHub Desktop.
Some ActiveRecord extensions which are helpful for me in a couple of projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveRecordExtension | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def from_current(period, options = { column: 'created_at' }) | |
raise "Invalid options in #{self.to_s}\##{__method__.to_s}" unless [:year, :quarter, :month, :week, :day].include? period | |
start_method = "beginning_of_#{period.to_s}" | |
end_method = "end_of_#{period.to_s}" | |
where(options[:column].to_s => Date.today.send(start_method)..Date.today.send(end_method)) | |
end | |
def between_dates(options = {}) | |
options = { | |
from: Date.today.beginning_of_day, | |
to: Date.today.end_of_day, | |
column: 'created_at' | |
}.merge options | |
options[:from] = Date.parse(options[:from]) if options[:from].is_a? String | |
options[:to] = Date.parse(options[:to]) if options[:to].is_a? String | |
where(options[:column].to_s => options[:from]..options[:to]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment