Netzke::ActiveRecord::RelationExtensions

Public Instance Methods

extend_with(*params) click to toggle source
    # File lib/netzke/active_record/relation_extensions.rb, line 5
 5:       def extend_with(*params)
 6:         scope = params.shift
 7:         case scope.class.name
 8:         when "Symbol" # model's scope
 9:           self.send(scope, *params)
10:         when "String" # SQL query or SQL query with params (e.g. ["created_at < ?", 1.day.ago])
11:           params.empty? ? self.where(scope) : self.where([scope, *params])
12:         when "Array"
13:           self.extend_with(*scope)
14:         when "Hash"   # conditions hash
15:           self.where(scope)
16:         when "Proc"   # receives a relation, must return a relation
17:           scope.call(self)
18:         else
19:           raise ArgumentError, "Wrong parameter type for ActiveRecord::Relation#extend_with"
20:         end
21:       end
extend_with_netzke_conditions(cond) click to toggle source

Non-destructively extends itself whith a hash of double-underscore’d conditions, where the last part “__” is MetaWhere operator (which is required), e.g.:

    {:role__name__like => "%admin"}
    # File lib/netzke/active_record/relation_extensions.rb, line 26
26:       def extend_with_netzke_conditions(cond)
27:         cond.each_pair.inject(self) do |r, (k,v)|
28:           assoc, method, *operator = k.to_s.split("__")
29:           operator.empty? ? r.where(assoc.to_sym.send(method) => v) : r.where(assoc.to_sym => {method.to_sym.send(operator.last) => v}).joins(assoc.to_sym)
30:         end
31:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.