Sha256: 86ce6a62f1c6911d31d844c78d446df7b62e058ab3bb31ae8169a2b18b5a0961

Contents?: true

Size: 1.1 KB

Versions: 41

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module EacRailsUtils
  module Models
    # == Example:
    #
    # Note: model Product has a attribute "foo" Date, Time or Number:
    #
    #   class Product
    #     include ::EacRailsUtils::Models::InequalityQueries
    #
    #     add_inequality_queries(:foo)
    #   end
    #
    # This add the following scopes:
    #
    #   Product.by_foo_gt(value) # Equivalent to Product.where("foo > ?", value)
    #   Product.by_foo_gteq(value) # Equivalent to Product.where("foo >= ?", value)
    #   Product.by_foo_lt(value) # Equivalent to Product.where("foo < ?", value)
    #   Product.by_foo_lteq(value) # Equivalent to Product.where("foo <= ?", value)
    module InequalityQueries
      class << self
        def included(base)
          base.extend(ClassMethods)
        end
      end

      module ClassMethods
        def add_inequality_queries(attribute)
          %w[gt gteq lt lteq].each do |ineq|
            scope "by_#{attribute}_#{ineq}", lambda { |v|
              where(arel_table[attribute].send(ineq, v))
            }
          end
        end
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
eac_rails_utils-0.15.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.15.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.14.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.14.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.13.5 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.13.4 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.13.3 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.13.2 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.13.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.13.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.12.3 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.12.2 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.12.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.12.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.11.6 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.11.5 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.11.4 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.11.3 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.11.2 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.11.1 lib/eac_rails_utils/models/inequality_queries.rb