module JamRuby
  class IcecastServerSocket < ActiveRecord::Base

    self.table_name = 'icecast_server_sockets'

    attr_accessible :icecast_listen_socket_id, :icecast_server_id, as: :admin

    belongs_to :socket, :class_name => "JamRuby::IcecastListenSocket", :foreign_key => 'icecast_listen_socket_id', :inverse_of => :server_sockets
    belongs_to :server, :class_name => "JamRuby::IcecastServer", :foreign_key => 'icecast_server_id', :inverse_of => :listen_socket_servers

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

    after_save      :poke_config
    before_destroy  :poke_config

    def poke_config
      server.update_attribute(:config_changed, 1) if server
    end
  end
end