# == Schema Information
#
# Table name: radius_configurations
#
#  id              :integer          not null, primary key
#  radius_name     :string(255)
#  radius_ip       :string(255)
#  radius_port     :string(255)
#  radius_secret   :string(255)
#  type            :string(255)
#  organisation_id :integer
#  created_at      :datetime
#  updated_at      :datetime
#

class RadiusConfiguration < ActiveRecord::Base
  resourcify
  include PublicActivity::Model

  serialize :vendor_details, Hash

  tracked owner: ->(controller, model) { controller && controller.tracked_current_user },params:
   {:attributes => proc {|controller, model_instance| { "radius_configuration(#{model_instance.radius_name})" => model_instance.changes}}},organisation_id: ->(controller, model) { controller && controller.tracked_current_user.organisation_id },location_network_id: nil

  tracked assumed_by: proc {|controller, model| controller.user_assumed_by if controller}
   
  belongs_to :organisation

  def update_redis
    ssids = NetworkSsid.where(auth_radius_id: self.id.to_s)
    ssids = NetworkSsid.where(acc_radius_id: self.id.to_s) if ssids.blank?
    (ssids || []).each(&:update_redis)
  end

  after_create do |x|
    x.update_redis
  end

  after_update do |x|
    x.update_redis
  end

  after_destroy do |x|
    x.update_redis
  end

end
