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.11.0 lib/eac_rails_utils/models/inequality_queries.rb