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.25.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.24.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.23.4 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.23.3 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.23.2 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.23.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.23.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.22.3 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.22.2 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.22.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.22.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.21.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.20.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.19.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.18.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.17.2 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.17.1 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.17.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.16.0 lib/eac_rails_utils/models/inequality_queries.rb
eac_rails_utils-0.15.2 lib/eac_rails_utils/models/inequality_queries.rb