# == Schema Information
#
# Table name: config_mappings
#
#  id                :integer          not null, primary key
#  configurable_id   :integer
#  configurable_type :string(255)
#  resourceable_id   :integer
#  resourceable_type :string(255)
#  created_at        :datetime
#  updated_at        :datetime
#

class ConfigMapping < ActiveRecord::Base
  RESOURCE_MAP = {"network" => "LocationNetwork", "AP" => "RouterInventory", "tag" => "ActsAsTaggableOn::Tag"}
  belongs_to :configurable, polymorphic: true
  belongs_to :resourceable, polymorphic: true
  include PublicActivity::Model

  after_destroy :update_resource_redis

  private

    def update_resource_redis
      if resourceable_type == "Tag"
        RouterInventory.tagged_with(ActsAsTaggableOn::Tag.find_by_id(resourceable_id)).each(&:update_redis)
      else
        res = self.resourceable
        unless res.blank?
          if res.is_a? LocationNetwork
            RouterInventory.where(location_network_id: res.id).each(&:update_redis)
          elsif res.is_a? RouterInventory
            RouterInventory.find_by_id(res.id).try(:update_redis)
          end
        end
      end
    end
end
