Sha256: 29cb5383e65e794a3e70d43f9913a2d742148af758629725e3c5384044991486

Contents?: true

Size: 937 Bytes

Versions: 9

Compression:

Stored size: 937 Bytes

Contents

module Netzke
  module Basepack
    module ActiveRecord
      module RelationExtensions
        def extend_with(*params)
          scope = params.shift
          case scope.class.name
          when "Symbol" # model's scope
            self.send(scope, *params)
          when "String" # SQL query or SQL query with params (e.g. ["created_at < ?", 1.day.ago])
            params.empty? ? self.where(scope) : self.where([scope, *params])
          when "Array"
            self.extend_with(*scope)
          when "Hash"  # conditions hash
            self.where(scope)
          when "ActiveSupport::HashWithIndifferentAccess" # conditions hash
            self.where(scope)
          when "Proc"   # receives a relation, must return a relation
            scope.call(self)
          else
            raise ArgumentError, "Wrong parameter type for ActiveRecord::Relation#extend_with"
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
netzke-basepack-0.10.0 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.9.0 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.10.0.rc2 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.9.0.rc1 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.8.4 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.8.3 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.8.2 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.8.1 lib/netzke/basepack/active_record/relation_extensions.rb
netzke-basepack-0.8.0 lib/netzke/basepack/active_record/relation_extensions.rb