Sha256: 848dd86fa93a3c23fda22c13b9c3d2fb65b2e830ab965dcd6fe3973b04744758

Contents?: true

Size: 2 KB

Versions: 19

Compression:

Stored size: 2 KB

Contents

# Add support for :scope => :my_scope to has_many and belongs_to

module Hobo

  module Scopes

    AssociationProxyExtensions = classy_module do

      def scope_conditions(reflection)
        scope_name = reflection.options[:scope]
        return nil if scope_name.nil?
        target_class = reflection.klass
        return nil if target_class.nil?
        if scope_name.respond_to? :map
          combine_conditions(*(scope_name.map {|scope| target_class.send(*scope).scope(:find)[:conditions]}))
        else
          target_class.send(scope_name).scope(:find)[:conditions]
        end
      end


      def combine_conditions(*conditions)
        parts = conditions.compact.map { |c| "(#{sanitize_sql c})" }
        parts.empty? ? nil : parts.join(" AND ")
      end


      def conditions_with_hobo_scopes
        scope_conditions = self.scope_conditions(@reflection)
        unscoped_conditions = conditions_without_hobo_scopes
        combine_conditions(scope_conditions, unscoped_conditions)
      end
      alias_method_chain :conditions, :hobo_scopes

    end

    HasManyThroughAssociationExtensions = classy_module do

      def build_conditions_with_hobo_scopes
        scope_conditions         = self.scope_conditions(@reflection)
        through_scope_conditions = self.scope_conditions(@reflection.through_reflection)
        unscoped_conditions = build_conditions_without_hobo_scopes
        combine_conditions(scope_conditions, through_scope_conditions, unscoped_conditions)
      end
      alias_method_chain :build_conditions, :hobo_scopes
      public :conditions
      
    end
    
    AssociationCollectionExtensions = classy_module do
      
      def proxy_respond_to_with_automatic_scopes?(method, include_priv = false)
        proxy_respond_to_without_automatic_scopes?(method, include_priv) ||
          (@reflection.klass.create_automatic_scope(method) if @reflection.klass.respond_to?(:create_automatic_scope))
      end
      alias_method_chain :proxy_respond_to?, :automatic_scopes
      
    end

  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
hobo-1.1.0 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.0.3 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.1.0.pre4 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.1.0.pre3 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.1.0.pre2 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.1.0.pre0 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.0.2 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.0.1 lib/hobo/scopes/association_proxy_extensions.rb
hobo-1.0.0 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.106 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.105 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.104 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.103 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.102 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.101 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.100 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.9.0 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.8.10 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.8.9 lib/hobo/scopes/association_proxy_extensions.rb