Sha256: 70c6c8330f13717944cf6cee75e0279f9a9f5725fb6b23b96e2a381c2f33a608

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 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] and
          target_class = reflection.klass and
          target_class.send(scope_name).scope(:find)[:conditions]
      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

3 entries across 3 versions & 1 rubygems

Version Path
hobo-0.8.8 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.8.6 lib/hobo/scopes/association_proxy_extensions.rb
hobo-0.8.7 lib/hobo/scopes/association_proxy_extensions.rb