Sha256: 3a8cad633d7e42b766684e8a0837e5ee0852375794fd52fff0f7ad2ed9afc70a

Contents?: true

Size: 1008 Bytes

Versions: 4

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

module Eac
  # == Example:
  #
  # Note: model Product has a attribute "foo" Date, Time or Number:
  #
  #   class Product
  #     include ::Eac::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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eac_rails_utils-0.10.1 lib/eac/inequality_queries.rb
eac_rails_utils-0.10.0 lib/eac/inequality_queries.rb
eac_rails_utils-0.9.1 lib/eac/inequality_queries.rb
eac_rails_utils-0.9.0 lib/eac/inequality_queries.rb