lib/better_ar.rb in better_ar-0.0.6 vs lib/better_ar.rb in better_ar-0.0.7
- old
+ new
@@ -1,107 +1,5 @@
-module BetterAr
+module BetterAr; end
- # Breaks down the hash to do a {ActiveRecord::Relation} query
- #
- # 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.
- #
- # Implicit joins are supported.
- # example:
- # User.all(:records => {:name => 'test'})
- #
- # is the same as:
- # User.joins(:records).where(:records => {:name => 'test'})
- #
- # @param [Hash]
- # Optional
- # @return [ActiveRecord::Relation]
- def all(opts = {})
- if opts.empty?
- super()
- elsif opts.key?(:conditions)
- super(opts)
- else
- relation = clone
-
- unless opts.key?(:conditions)
- unless opts.empty?
- opts.keys.select { |key| key.to_s =~ /!$/ }.each do |predicate|
- if value = opts.delete(predicate)
- relation = relation.send(predicate.to_s.sub('!',''), value)
- end
- end
-
- reflect_on_all_associations.map(&:name).each do |name|
- if opts.key?(name)
- relation = relation.joins(name)
- end
- end
-
- relation.where(opts)
- end
-
- if opts.empty?
- relation
- else
- relation.where(opts)
- end
- end
- end
- end
-
- # Forces a limit of 1 on the collection
- #
- # example:
- # User.first(:age => 10, :name => 'Brian')
- #
- # 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 follows same convention as {#all}
- # @return [ActiveRecord::Base]
- def first(opts = {})
- if opts.empty?
- super()
- elsif opts.key?(:conditions)
- super(opts)
- else
- all(opts.merge(:limit! => 1)).first
- end
- end
-
- # Does a count on the query
- #
- # example:
- # User.count(:age => 20)
- #
- # is the same as:
- # User.where(:age => 20).count
- #
- # if the key :conditions is present it will fall back to legacy
- #
- # @param [Hash]
- # Optional follows same convention as {#all}
- # @return [Integer]
- def count(opts = {})
- if opts.empty?
- super()
- elsif opts.key?(:conditions)
- super(opts)
- else
- all(opts).count
- end
- end
-
-end
-
-ActiveRecord::Relation.send(:include, BetterAr)
+require 'better_ar/finder_methods'
+require 'better_ar/calculations'
+require 'better_ar/association_collection'