Updated

app/models/concerns/deal / event_creator.rb

B
73 lines of codes
5 methods
10.9 complexity/method
3 churn
54.55 complexity
0 duplications
module Deal::EventCreator
  1. Deal::EventCreator has no descriptive comment
extend ActiveSupport::Concern included do around_create :create_deal_and_event around_update :update_deal_and_create_event def create_deal_and_event transaction do yield create_event_log('deal_opened') end end def update_deal_and_create_event transaction do yield create_event_based_on_changes end end def create_event_based_on_changes return create_event_log('deal_won') if status_previously_changed?(to: 'won') return create_event_log('deal_lost') if status_previously_changed?(to: 'lost') return create_event_log('deal_reopened') if status_previously_changed?(to: 'open') create_event_log_stage_changes if stage_previously_changed? end end private def create_event_log_stage_changes old_stage_id, new_stage_id = previous_changes['stage_id'] old_stage = Stage.find_by(id: old_stage_id) if old_stage_id new_stage = Stage.find_by(id: new_stage_id) if new_stage_id Event.create!( deal: self, kind: 'deal_stage_change', done: true, contact:, from_me: true, additional_attributes: { old_stage_id: old_stage.id, old_stage_name: old_stage.name, old_stage_pipeline_id: old_stage.pipeline.id,
  1. Deal::EventCreator#create_event_log_stage_changes calls 'old_stage.pipeline' 2 times Locations: 0 1
old_stage_pipeline_name: old_stage.pipeline.name,
  1. Deal::EventCreator#create_event_log_stage_changes calls 'old_stage.pipeline' 2 times Locations: 0 1
new_stage_id: new_stage.id, new_stage_name: new_stage.name, new_stage_pipeline_id: new_stage.pipeline.id,
  1. Deal::EventCreator#create_event_log_stage_changes calls 'new_stage.pipeline' 2 times Locations: 0 1
new_stage_pipeline_name: new_stage.pipeline.name,
  1. Deal::EventCreator#create_event_log_stage_changes calls 'new_stage.pipeline' 2 times Locations: 0 1
deal_name: name } ) end def create_event_log(log_kind) Event.create!( deal: self, kind: log_kind, done: true, from_me: true, contact:, additional_attributes: { stage_id: stage.id, stage_name: stage.name, pipeline_id: pipeline.id, pipeline_name: pipeline.name, deal_name: name } ) end end