lib/better_ar.rb in better_ar-0.0.3 vs lib/better_ar.rb in better_ar-0.0.4

- old
+ new

@@ -1,27 +1,29 @@ module BetterAr module ClassMethods # Breaks down the hash to do a ActiveRecord::Relation query # - # example: User.all(:age => 10, :limit => 2, :offset => 3, :order => :name) + # example: User.all(:age => 10, :limit! => 2, :offset! => 3, :order! => :name) # # is the same as: User.where(:age => 10).limit(2).offset(3).order(:name) # # if the key :conditions is present it will fall back to legacy # + # any key with the '!' at the end will be assumed to be a sql operator. The key should match either an ActiveRecord::Relation instance method or an ARel predicate. + # # @param [Hash] # Optional # @return [ActiveRecord::Relation] def all(opts = {}) relation = scoped.clone unless opts.key?(:conditions) unless opts.empty? - [:order, :limit, :offset].each do |predicate| + opts.keys.select { |key| key.to_s =~ /!$/ }.each do |predicate| if value = opts.delete(predicate) - relation = relation.send(predicate, value) + relation = relation.send(predicate.to_s.sub('!',''), value) end end relation.where(opts) end @@ -43,15 +45,15 @@ # is the same as: User.where(:age => 10, :name => 'Brian').limit(1).first # # if the key :conditions is present it will fall back to legacy # # @param [Hash] - # Optional + # Optional follows same convention as .all # @return [ActiveRecord::Base] def first(opts = {}) unless opts.key?(:conditions) - all(opts.merge(:limit => 1)).first + all(opts.merge(:limit! => 1)).first else scoped.first(opts) end end @@ -62,10 +64,10 @@ # is the same as: User.where(:age => 20).count # # if the key :conditions is present it will fall back to legacy # # @param [Hash] - # Optional + # Optional follows same convention as .all # @return [Integer] def count(opts = {}) unless opts.key?(:conditions) all(opts).count else