Sha256: de6e51bfaf01b7cced83e6862b1c65702f7dcaefbee2fb775e6ca691a932c6c7

Contents?: true

Size: 788 Bytes

Versions: 12

Compression:

Stored size: 788 Bytes

Contents

# Add support for :scope => :my_scope to associations

ActiveRecord::Associations::ThroughAssociationScope.class_eval do

  def construct_scope_with_scope
    s = construct_scope_without_scope
    s[:find][:scope] = @reflection.options[:scope]
    s
  end
  alias_method_chain :construct_scope, :scope

end

ActiveRecord::SpawnMethods.class_eval do

  def apply_finder_options_with_scope(options)
    scopes = []
    Array.wrap(options.delete(:scope)).each do |s|
      if s.is_a?(Hash)
        s.each_pair{|k,v| scopes << [k,v] }
      else
        scopes << [s]
      end
    end
    relation = apply_finder_options_without_scope(options)
    return relation if scopes.empty?
    scopes.inject(relation) {|r, s| r.send *s }
  end
  alias_method_chain :apply_finder_options, :scope

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hobo-1.3.0.pre22 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre21 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre20 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre19 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre18 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre16 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre15 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre14 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre13 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre12 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre11 lib/hobo/extensions/active_record/scopes.rb
hobo-1.3.0.pre10 lib/hobo/extensions/active_record/scopes.rb