Sha256: 23a1d1df576363972bd61f93e492f7ee9650404edc04c45c6ab86b1a8dcdbf19

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module ActiveRecord
  module Associations
    class Association #:nodoc:
      def scoped
        # Rails implementation just returns target_scope.merge(association_scope)
        sc = target_scope.merge(association_scope)

        # Hobo adds in scopes declared on the association, e.g. has_many ..... :scope => :foo
        if (declared_scope = options[:scope])
          if declared_scope.is_a? Array
            declared_scope.inject(sc) { |result, element| result.merge(klass.send(element)) }
          elsif declared_scope.is_a? Hash
            method = declared_scope.keys.first
            arg = declared_scope.values.first
            sc.merge(klass.send(method, arg))
          else
            # It's just a symbol -- the name of a scope
            sc.merge(klass.send(declared_scope))
          end
        else
          sc
        end
      end

      private

      def raise_on_type_mismatch!(record)
        # Don't complain if the interface type of a polymorphic association doesn't exist
        klass = @reflection.klass rescue nil
        unless klass.nil? || record.is_a?(klass)
          raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.klass} expected, got #{record.class}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hobo-2.1.0.pre1 lib/hobo/extensions/active_record/associations/association.rb