From b398bb75162be33b47f66b81058134776505cbe7 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 6 Apr 2017 13:06:20 +0200 Subject: [PATCH] add step annotations methods in steps_action concerns --- app/controllers/concerns/steps_actions.rb | 152 ++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 app/controllers/concerns/steps_actions.rb diff --git a/app/controllers/concerns/steps_actions.rb b/app/controllers/concerns/steps_actions.rb new file mode 100644 index 000000000..56c7ef952 --- /dev/null +++ b/app/controllers/concerns/steps_actions.rb @@ -0,0 +1,152 @@ +module StepsActions + extend ActiveSupport::Concern + # generates notification for smart annotations + def create_annotation_notification(step) + # step description + step_description_annotation(step) + # checklists + step.checklists.each do |checklist| + checklist_name_annotation(step, checklist) + checklist.checklist_items.each do |checklist_item| + checklist_item_annotation(step, checklist_item) + end + end + end + + def fetch_new_checklists_data + checklists = [] + new_checklists = step_params[:checklists_attributes] + + if new_checklists + new_checklists.each do |e| + list = PreviouseChecklist.new( + e.second[:id].to_i, + e.second[:name] + ) + if e.second[:checklist_items_attributes] + e.second[:checklist_items_attributes].each do |el| + list.add_checklist( + PreviouseChecklistItem.new(el.second[:id].to_i, el.second[:text]) + ) + end + end + checklists << list + end + end + checklists + end + + def fetch_old_checklists_data(step) + checklists = [] + if step.checklists + step.checklists.each do |e| + list = PreviouseChecklist.new( + e.id, + e.name + ) + e.checklist_items.each do |el| + list.add_checklist( + PreviouseChecklistItem.new(el.id, el.text) + ) + end + checklists << list + end + end + checklists + end + + def update_annotation_notification(step, + old_description, + new_checklists, + old_checklists) + step_description_annotation(step, old_description) + new_checklists.each do |e| + # generates smart annotaion if the checklist is new + add_new_checklist(step, e) if e.id.zero? + checklist_name_annotation(step, e) unless e.id + # else check if checklist is not deleted and generates + # new notifications + next unless old_checklists.map(&:id).include?(e.id) + old_checklist = old_checklists.select { |i| i.id == e.id }.first + checklist_name_annotation(step, e, old_checklist.name) + e.items.each do |ci| + old_list = old_checklists.select { |i| i.id == e.id }.first + old_item = old_list.items.select { |i| i.id == ci.id }.first if old_list + + checklist_item_annotation(step, ci, old_item.text) + end + end + end + + def add_new_checklist(step, checklist) + checklist_name_annotation(step, checklist) + checklist.items.each do |ci| + checklist_item_annotation(step, ci) + end + end + + def checklist_item_annotation(step, checklist_item, old_text = nil) + smart_annotation_notification( + old_text: old_text, + new_text: checklist_item.text, + title: t('notifications.checklist_title', + user: current_user.full_name, + step: step.name), + message: annotation_message(step) + ) + end + + def checklist_name_annotation(step, checklist, old_text = nil) + smart_annotation_notification( + old_text: old_text, + new_text: checklist.name, + title: t('notifications.checklist_title', + user: current_user.full_name, + step: step.name), + message: annotation_message(step) + ) + end + + def step_description_annotation(step, old_text = nil) + smart_annotation_notification( + old_text: old_text, + new_text: step.description, + title: t('notifications.step_description_title', + user: current_user.full_name, + step: step.name), + message: annotation_message(step) + ) + end + + def annotation_message(step) + return t('notifications.step_annotation_message_html', + project: link_to( + step.my_module.experiment.project.name, + project_url(step.my_module.experiment.project) + ), + my_module: link_to( + step.my_module.name, + protocols_my_module_url(step.my_module) + ), + step: link_to( + step.name, + protocols_my_module_url(step.my_module) + )) if @protocol.in_module? + + t('notifications.protocol_step_annotation_message_html', + protocol: link_to( + @protocol.name, protocol_steps_url(@protocol) + )) + end + + PreviouseChecklistItem = Struct.new(:id, :text) + PreviouseChecklist = Struct.new(:id, :name, :items) do + def initialize(id, name, items = []) + super(id, name, items) + end + + def add_checklist(item) + items << item + end + end +end