module JamRuby
  class IcecastTemplateSocket < ActiveRecord::Base

    self.table_name = 'icecast_template_sockets'

    attr_accessible :icecast_listen_socket_id, :icecast_template_id, as: :admin

    belongs_to :socket, :class_name => "JamRuby::IcecastListenSocket", :foreign_key => 'icecast_listen_socket_id', :inverse_of => :template_sockets
    belongs_to :template, :class_name => "JamRuby::IcecastTemplate", :foreign_key => 'icecast_template_id', :inverse_of => :listen_socket_templates

    validates :socket, :presence => true
    validates :template, :presence => true

    after_save      :poke_config
    before_destroy  :poke_config

    def poke_config
      template.servers.update_all(config_changed: 1) if template
    end
  end
end