Mistři v programování na #hovnokod

Ruby #266

class FinancialEventObserver < ActiveRecord::Observer
  observe Payment, Invoice
  def before_save(model)
    event = nil        
    if model.class == Payment
      if model.new_record?        
        event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_INVOICE,
        :arguments => {:client_name => model.invoice.client.short_name, :invoice_number => model.invoice.invoice_number},
          :company_id=>model.invoice.client.company.id)
      end
    elsif model.class == Invoice
      i = Invoice.find_by_id model.id      
      if model.new_record? or i.status != model.status        
        if model.status == Invoice::Status::ESTIMATE
          event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_SEND,
        :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
            :company_id=>model.client.company.id)
        elsif model.status == Invoice::Status::APPROVED
          event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_APPROVED,
        :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
            :company_id=>model.client.company.id)
        elsif model.status == Invoice::Status::REJECTED
          event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_REJECTED,
        :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
            :company_id=>model.client.company.id)
        elsif model.status == Invoice::Status::SEND
          event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_SEND,
        :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
            :company_id=>model.client.company.id)            
        end
      elsif !model.new_record? and i.state != model.state
        if model.state == Invoice::State::DELETED
          event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_DELETED,
        :arguments => {:invoice_number => model.invoice_number},
            :company_id=>model.client.company.id)
        end
      end    
    end
    event.eventable = model.requester unless event.blank? 
    event.save unless event.blank?
    
  end
  def before_destroy(model)
    if model.class == Payment
      event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_DELETED,
        :arguments => {:invoice_number => model.invoice.invoice_number},
      :company_id=>model.invoice.client.company.id)
      event.eventable = model.requester
      event.save
    end    
  end
end

HovnoKod,