client.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class Client < ActiveRecord::Base
  2. include Perishable
  3. include SecretAccessible
  4. belongs_to :room
  5. validates_presence_of :name , :nicks , :secret , :type , :room_id
  6. validates_presence_of :name , :nicks , :secret , :type , :room_id
  7. validates_uniqueness_of :name , :secret
  8. before_validation :prepare , :on => :create
  9. after_create :prefill_messages
  10. before_destroy :destroy_room
  11. private
  12. def prepare
  13. self.name &&= URI.escape self.name
  14. self.secret = generate_secret
  15. self.room = Room.create
  16. raise ActiveRecord::RecordNotUnique , "" if (Client.find_by_name self.name ).present? ||
  17. (Client.find_by_secret self.secret).present?
  18. end
  19. def prefill_messages
  20. N_MESSAGES_QUEUED.times { self.room.messages.create PRIME_MESSAGE_PARAMS }
  21. end
  22. def destroy_room ; self.room.destroy if self.room.clients.count == 1 ; end ;
  23. def self.all_stale
  24. Perishable.all_stale.map { | id | self.find id }
  25. end
  26. def self.cleanup # NOTE: cron this
  27. Client.all_stale.each { | client | p "destroying client ##{client.id}" ; client.destroy ; }
  28. Room .all_empty.each { | room | p "destroying room ##{ room .id}" ; room .destroy ; }
  29. end
  30. end
  31. class IrcClient < Client ; end ;
  32. class SimClient < Client ; end ;