class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me has_one :patron has_one :user_has_role has_one :role, :through => :user_has_role belongs_to :user_group belongs_to :required_role, :class_name => 'Role', :foreign_key => 'required_role_id' has_many :checkouts, :dependent => :nullify has_many :reserves, :dependent => :destroy has_many :reserved_manifestations, :through => :reserves, :source => :manifestation has_many :checkout_stat_has_users has_many :user_checkout_stats, :through => :checkout_stat_has_users has_many :reserve_stat_has_users has_many :user_reserve_stats, :through => :reserve_stat_has_users has_many :baskets, :dependent => :destroy belongs_to :library before_destroy :check_item_before_destroy extend FriendlyId friendly_id :username def check_item_before_destroy # TODO: 貸出記録を残す場合 if checkouts.size > 0 raise 'This user has items still checked out.' end end def reset_checkout_icalendar_token self.checkout_icalendar_token = Devise.friendly_token end def delete_checkout_icalendar_token self.checkout_icalendar_token = nil end def checked_item_count checkout_count = {} CheckoutType.all.each do |checkout_type| # 資料種別ごとの貸出中の冊数を計算 checkout_count[:"#{checkout_type.name}"] = self.checkouts.count_by_sql([" SELECT count(item_id) FROM checkouts WHERE item_id IN ( SELECT id FROM items WHERE checkout_type_id = ? ) AND user_id = ? AND checkin_id IS NULL", checkout_type.id, self.id] ) end return checkout_count end def reached_reservation_limit?(manifestation) return true if self.user_group.user_group_has_checkout_types.available_for_carrier_type(manifestation.carrier_type).where(:user_group_id => self.user_group.id).collect(&:reservation_limit).max.to_i <= self.reserves.waiting.size false end def has_role?(role_in_question) return false unless role return true if role.name == role_in_question case role.name when 'Administrator' return true when 'Librarian' return true if role_in_question == 'User' else false end end if defined?(EnjuMessage) has_many :sent_messages, :foreign_key => 'sender_id', :class_name => 'Message' has_many :received_messages, :foreign_key => 'receiver_id', :class_name => 'Message' def send_message(status, options = {}) MessageRequest.transaction do request = MessageRequest.new request.sender = User.find(1) request.receiver = self request.message_template = MessageTemplate.localized_template(status, self.locale) request.save_message_body(options) request.sm_send_message! end end end end