Module: ReferenceTracking::ActionController::Purging

Defined in:
lib/reference_tracking/action_controller.rb

Constant Summary collapse

SKIP_ATTRIBUTES =
%w(id created_at updated_at)

Instance Method Summary collapse

Instance Method Details

#purge(objects) ⇒ Object



68
69
70
71
# File 'lib/reference_tracking/action_controller.rb', line 68

def purge(objects)
  tags = objects.map { |object| purge_tags_for(object)  }.flatten
  response.headers[purging_options[:header]] = tags
end

#purge_associations_for(object) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/reference_tracking/action_controller.rb', line 87

def purge_associations_for(object)
  superclasses = purge_super_classes_for(object)
  object.class.reflect_on_all_associations(:belongs_to).map do |association|
    if owner = object.send(association.name)
      owner.class.reflect_on_all_associations.map do |association|
        [owner, association.name] if superclasses.include?(association.class_name)
      end
    end
  end.flatten.compact.in_groups_of(2)
end

#purge_super_classes_for(object) ⇒ Object



98
99
100
# File 'lib/reference_tracking/action_controller.rb', line 98

def purge_super_classes_for(object)
  object.class.ancestors.select { |klass| klass < ActiveRecord::Base }.map(&:name)
end

#purge_tags_for(object) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/reference_tracking/action_controller.rb', line 73

def purge_tags_for(object)
  case params[:action]
  when 'create', 'destroy'
    tags = purge_associations_for(object).map do |owner, method|
      ReferenceTracking.to_tag(owner, method)
    end
    tags << ReferenceTracking.to_tag(object) if params[:action] == 'destroy'
    tags
  when 'update'
    methods = object.previous_changes.keys & object.attributes.keys - SKIP_ATTRIBUTES
    methods.map { |method| ReferenceTracking.to_tag(object, method) }
  end
end