app/models/reserve.rb in enju_circulation-0.0.43 vs app/models/reserve.rb in enju_circulation-0.0.45

- old
+ new

@@ -1,7 +1,12 @@ # -*- encoding: utf-8 -*- class Reserve < ActiveRecord::Base + attr_accessible :manifestation_id, :item_identifier, :user_number, :expired_at + attr_accessible :manifestation_id, :item_identifier, :user_number, + :expired_at, :request_status_type, :canceled_at, :checked_out_at, + :expiration_notice_to_patron, :expiration_notice_to_library, + :as => :admin scope :hold, where('item_id IS NOT NULL') scope :not_hold, where(:item_id => nil) scope :waiting, where('canceled_at IS NULL AND expired_at > ? AND state != ?', Time.zone.now, 'completed').order('reserves.id DESC') scope :completed, where('checked_out_at IS NOT NULL') scope :canceled, where('canceled_at IS NOT NULL') @@ -29,11 +34,10 @@ validate :available_for_reservation?, :on => :create before_validation :set_item_and_manifestation, :on => :create before_validation :set_expired_at before_validation :set_request_status, :on => :create - attr_protected :user_id attr_accessor :user_number, :item_identifier state_machine :initial => :pending do before_transition :pending => :requested, :do => :do_request before_transition [:pending, :requested, :retained] => :retained, :do => :retain @@ -92,11 +96,12 @@ end end end def do_request - self.update_attributes({:request_status_type => RequestStatusType.where(:name => 'In Process').first}) + self.assign_attributes({:request_status_type => RequestStatusType.where(:name => 'In Process').first}, :as => :admin) + self.save! end def manifestation_must_include_item unless item_id.blank? item = Item.find(item_id) rescue nil @@ -114,43 +119,51 @@ sender = User.find(1) unless sender # TODO: システムからのメッセージの発信者 Reserve.transaction do case state when 'requested' message_template_to_patron = MessageTemplate.localized_template('reservation_accepted_for_patron', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => user, :message_template => message_template_to_patron) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => user, :message_template => message_template_to_patron}, :as => :admin) request.save_message_body(:manifestations => Array[manifestation], :user => user) request.sm_send_message! # 受付時は即時送信 message_template_to_library = MessageTemplate.localized_template('reservation_accepted_for_library', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => sender, :message_template => message_template_to_library) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => sender, :message_template => message_template_to_library}, :as => :admin) request.save_message_body(:manifestations => Array[manifestation], :user => user) request.sm_send_message! # 受付時は即時送信 when 'canceled' message_template_to_patron = MessageTemplate.localized_template('reservation_canceled_for_patron', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => user, :message_template => message_template_to_patron) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => user, :message_template => message_template_to_patron}, :as => :admin) request.save_message_body(:manifestations => Array[manifestation], :user => user) request.sm_send_message! # キャンセル時は即時送信 message_template_to_library = MessageTemplate.localized_template('reservation_canceled_for_library', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => sender, :message_template => message_template_to_library) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => sender, :message_template => message_template_to_library}, :as => :admin) request.save_message_body(:manifestations => Array[manifestation], :user => user) request.sm_send_message! # キャンセル時は即時送信 when 'expired' message_template_to_patron = MessageTemplate.localized_template('reservation_expired_for_patron', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => user, :message_template => message_template_to_patron) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => user, :message_template => message_template_to_patron}, :as => :admin) request.save_message_body(:manifestations => Array[manifestation], :user => user) request.sm_send_message! self.update_attribute(:expiration_notice_to_patron, true) message_template_to_library = MessageTemplate.localized_template('reservation_expired_for_library', sender.locale) - request = MessageRequest.create!(:sender => sender, :receiver => sender, :message_template => message_template_to_library) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => sender, :message_template => message_template_to_library}, :as => :admin) request.save_message_body(:manifestations => Array[manifestation], :user => sender) request.sm_send_message! when 'retained' message_template_for_patron = MessageTemplate.localized_template('item_received_for_patron', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => user, :message_template => message_template_for_patron) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => user, :message_template => message_template_for_patron}, :as => :admin) request.save_message_body(:manifestations => Array[item.manifestation], :user => user) request.sm_send_message! message_template_for_library = MessageTemplate.localized_template('item_received_for_library', user.locale) - request = MessageRequest.create!(:sender => sender, :receiver => sender, :message_template => message_template_for_library) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => sender, :message_template => message_template_for_library}, :as => :admin) request.save_message_body(:manifestations => Array[item.manifestation], :user => user) request.sm_send_message! else raise 'status not defined' end @@ -160,18 +173,20 @@ def self.send_message_to_library(status, options = {}) sender = User.find(1) # TODO: システムからのメッセージの発信者 case status when 'expired' message_template_to_library = MessageTemplate.localized_template('reservation_expired_for_library', sender.locale) - request = MessageRequest.create!(:sender => sender, :receiver => sender, :message_template => message_template_to_library) + request = MessageRequest.new + request.assign_attributes({:sender => sender, :receiver => sender, :message_template => message_template_to_library}, :as => :admin) request.save_message_body(:manifestations => options[:manifestations]) self.not_sent_expiration_notice_to_library.each do |reserve| reserve.update_attribute(:expiration_notice_to_library, true) end #when 'canceled' # message_template_to_library = MessageTemplate.localized_template('reservation_canceled_for_library', sender.locale) - # request = MessageRequest.create!(:sender => sender, :receiver => sender, :message_template => message_template_to_library) + # request = MessageRequest.new + # request.assign_attributes({:sender => sender, :receiver => sender, :message_template => message_template_to_library}, :as => :admin) # request.save_message_body(:manifestations => self.not_sent_expiration_notice_to_library.collect(&:manifestation)) # self.not_sent_cancel_notice_to_library.each do |reserve| # reserve.update_attribute(:expiration_notice_to_library, true) # end else @@ -217,35 +232,39 @@ end private def retain # TODO: 「取り置き中」の状態を正しく表す - self.update_attributes!({:request_status_type => RequestStatusType.where(:name => 'In Process').first, :checked_out_at => Time.zone.now}) + self.assign_attributes({:request_status_type => RequestStatusType.where(:name => 'In Process').first, :checked_out_at => Time.zone.now}, :as => :admin) + self.save! end def expire - self.update_attributes!({:request_status_type => RequestStatusType.where(:name => 'Expired').first, :canceled_at => Time.zone.now}) + self.assign_attributes({:request_status_type => RequestStatusType.where(:name => 'Expired').first, :canceled_at => Time.zone.now}, :as => :admin) + self.save! logger.info "#{Time.zone.now} reserve_id #{self.id} expired!" reserve = next_reservation if reserve reserve.item = item reserve.sm_retain! reserve.send_message end end def cancel - self.update_attributes!({:request_status_type => RequestStatusType.where(:name => 'Cannot Fulfill Request').first, :canceled_at => Time.zone.now}) + self.assign_attributes({:request_status_type => RequestStatusType.where(:name => 'Cannot Fulfill Request').first, :canceled_at => Time.zone.now}, :as => :admin) + self.save! reserve = next_reservation if reserve reserve.item = item reserve.sm_retain! reserve.send_message end end def checkout - self.update_attributes!({:request_status_type => RequestStatusType.where(:name => 'Available For Pickup').first, :checked_out_at => Time.zone.now}) + self.assign_attributes({:request_status_type => RequestStatusType.where(:name => 'Available For Pickup').first, :checked_out_at => Time.zone.now}, :as => :admin) + self.save! end if defined?(EnjuInterLibraryLoan) has_one :inter_library_loan end