Module: Immortal

Defined in:
lib/immortal.rb,
lib/immortal/has_many_through_mortal_association.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: HasManyThroughMortalAssociation

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/immortal.rb', line 5

def self.included(base)
  base.send :extend, ClassMethods
  base.send :include, InstanceMethods
  base.class_eval do
    class << self
      # In has_many :through => join_model we have to explicitly add
      # the 'not deleted' scope, otherwise it will take all the rows
      # from the join model
      def has_many_mortal(association_id, options = {}, &extension)
        has_many_immortal(association_id, options, &extension).tap do
          if options[:through] and reflections[options[:through]] and reflections[options[:through]].class_name.classify.constantize.arel_table[:deleted]
            reflection = reflect_on_association(association_id)
            collection_reader_method(reflection, Immortal::HasManyThroughMortalAssociation)
            collection_accessor_methods(reflection, Immortal::HasManyThroughMortalAssociation, false)
          end
        end
      end

      alias_method :has_many_immortal, :has_many
      alias_method :has_many, :has_many_mortal

      alias :mortal_delete_all :delete_all
      alias :delete_all :immortal_delete_all
    end
  end
end