Sha256: 1569ff8759f22f359c57a7c84e9e8b300009cee38d3242ac8f1a00c577adeef2

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module RailsScopes
  module DateScopes

    def date_scopes_names(attribute_names)
      attribute_names.each.map do |name|
        [ "#{name}_between",
          "#{name}_less_equal_than",
          "#{name}_greater_equal_than" ]
      end.flatten
    end

    def create_date_scopes_for(attribute_names)
      attribute_names.each do |attr_name|
        create_between_scope(attr_name)
        create_less_equal_scope(attr_name)
        create_greater_equal_scope(attr_name)
      end
    end

    def create_between_scope(attr_name, scope_name = nil)
      scope_name ||= "#{attr_name}_between"
      define_singleton_method(scope_name) do |start_date, end_date|
        s = format_date(start_date).beginning_of_day
        e = format_date(end_date).end_of_day
        where(attr_name => (s..e))
      end
    end

    def create_less_equal_scope(attr_name, scope_name = nil)
      scope_name ||= "#{attr_name}_less_equal_than"
      define_singleton_method(scope_name) do |date|
        where("#{attr_name.to_s} <= ?", format_date(date).end_of_day)
      end
    end

    def create_greater_equal_scope(attr_name, scope_name = nil)
      scope_name ||= "#{attr_name}_greater_equal_than"
      define_singleton_method(scope_name) do |date|
        where("#{attr_name.to_s} >= ?", format_date(date).beginning_of_day)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_scopes-0.0.3 lib/rails_scopes/date_scopes.rb