Sha256: 0ba748436007e6c8754b3c2373e8842869a62ec6185d692fcd69b3825ad0e619

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module ExpectedBehavior
  module ActsAsArchival
    module AssociationOperation

      class Base
        attr_reader :model, :head_archive_number

        def initialize(model, head_archive_number)
          @model = model
          @head_archive_number = head_archive_number
        end

        def execute
          each_archivable_association do |association|
            act_on_association(association) if association_conditions_met? association
          end
        end

        protected

        def each_archivable_association
          self.model.class.reflect_on_all_associations.each do |association|
            yield(association) if archivable_association?(association)
          end
        end

        def archivable_association?(association)
          association.macro.to_s =~ /^has/ &&
            association.klass.is_archival? &&
            association.options[:through].nil?
        end

        def association_conditions_met?(association)
          true
        end

        def act_on_association(association)
          key = association.respond_to?(:foreign_key) ? association.foreign_key : association.primary_key_name
          scope = association.klass.where(key => model.id)
          act_on_archivals(scope)
        end

        def act_on_archivals(scope)
          raise NotImplementedError,
            "The #{self.class} hasn't implemented 'act_on_archivals(scope)'"
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
acts_as_archival-1.1.1 lib/expected_behavior/association_operation/base.rb
acts_as_archival-1.1.0 lib/expected_behavior/association_operation/base.rb
acts_as_archival-1.0.0 lib/expected_behavior/association_operation/base.rb
acts_as_archival-0.6.1 lib/expected_behavior/association_operation/base.rb
acts_as_archival-0.6.0 lib/expected_behavior/association_operation/base.rb
acts_as_archival-0.5.3 lib/expected_behavior/association_operation/base.rb