Created
February 24, 2015 16:15
-
-
Save oleglukashev/bd16d0b7096a86cd3f66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Reception < ActiveRecord::Base | |
STATES = ['processing', 'refused', 'finished'] | |
belongs_to :user | |
has_many :comments, as: :commentable, dependent: :destroy | |
has_many :reception_items, inverse_of: :reception, dependent: :destroy | |
accepts_nested_attributes_for :reception_items | |
# TODO: remove this when reception form will be finished | |
attr_accessor :name | |
validates :state, presence: true, inclusion: {in: STATES} | |
scope :with_filter, -> (filter) { where(state: filter) if filter.present? } | |
scope :search, -> (search) { includes(:user).where('users.lastname ILIKE :search OR users.email ILIKE :search OR receptions.id = :number', search: "%#{search}%", number: search) if search.present? } | |
scope :valid, -> { where.not(user_id: nil) } | |
scope :old_invalid, -> { where('receptions.user_id = ? AND receptions.created_at < ?', nil, 1.week.ago) } | |
scope :got_after, -> (start) { where('receptions.created_at <= ?', start) if start.present? } | |
scope :got_before, -> (finish) { where('receptions.created_at >= ?', finish) if finish.present? } | |
after_create :send_submit_mail | |
after_update :send_submit_mail, if: :user_id_changed? | |
state_machine :state, initial: :processing do | |
event :refuse do | |
transition :processing => :refused | |
end | |
event :finish do | |
transition :processing => :finished | |
end | |
end | |
def self.build_with_items(params = {}) | |
if params.blank? | |
reception = new | |
reception.reception_items.build | |
else | |
reception = new params | |
end | |
reception.reception_items.each(&:build_pictures) | |
reception | |
end | |
def processed_completion | |
self_reception_items = self.reception_items | |
taken_items_count = self_reception_items.select(&:taken).size | |
if taken_items_count.zero? | |
'refused' | |
else | |
'finished' | |
end | |
end | |
def things_state | |
unless self.refused? | |
rec_items = self.reception_items | |
if rec_items.select{|item| item.taken==true}.size == rec_items.size | |
'approved' | |
else | |
'finished' | |
end | |
else | |
'refused' | |
end | |
end | |
def send_submit_mail | |
InfoMail::ReceptionsMailer.delay.submit(self.user_id, self.id) if self.user_id.present? && !ENV["mails_import_mode"].present? | |
end | |
def send_processing_mail | |
if self.processing? && self.mail_sent_at.blank? | |
if self.processed_completion == 'refused' | |
InfoMail::ReceptionsMailer.delay_for(15.minutes).refused(self.user_id, self.id) | |
else | |
InfoMail::ReceptionsMailer.delay_for(15.minutes).taken(self.user_id, self.id) | |
end | |
self.update_attribute(:mail_sent_at, Time.current + 15.minutes) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment