diff --git a/app/assets/javascripts/samples/sample_datatable.js.erb b/app/assets/javascripts/samples/sample_datatable.js.erb index d492cf4ba..cb2a1a000 100644 --- a/app/assets/javascripts/samples/sample_datatable.js.erb +++ b/app/assets/javascripts/samples/sample_datatable.js.erb @@ -524,6 +524,7 @@ function onClickSave() { // First fetch all the data in input fields data = { + request_url: $('#samples').data('current-uri'), sample_id: $(selectedSample).attr("id"), sample: {}, custom_fields: {}, // These fields are not currently bound to this sample diff --git a/app/controllers/samples_controller.rb b/app/controllers/samples_controller.rb index e6f893c9b..62692f103 100644 --- a/app/controllers/samples_controller.rb +++ b/app/controllers/samples_controller.rb @@ -1,5 +1,7 @@ class SamplesController < ApplicationController include InputSanitizeHelper + include ActionView::Helpers::TextHelper + include ApplicationHelper before_action :load_vars, only: [:edit, :update, :destroy, :show] before_action :load_vars_nested, only: [:new, :create] @@ -82,6 +84,8 @@ class SamplesController < ApplicationController errors[:custom_fields] << { "#{id}": scf.errors.messages } + else + sample_annotation_notification(sample, scf) end end end @@ -198,6 +202,7 @@ class SamplesController < ApplicationController scf = SampleCustomField.where("custom_field_id = ? AND sample_id = ?", id, sample.id).take if scf + old_text = scv.value # Well, client was naughty, no XMAS for him this year, update # existing SCF instead of creating new one scf.value = val @@ -207,6 +212,8 @@ class SamplesController < ApplicationController errors[:custom_fields] << { "#{id}": scf.errors.messages } + else + sample_annotation_notification(sample, scf, old_text) end else # SCF doesn't exist, create it @@ -220,6 +227,8 @@ class SamplesController < ApplicationController errors[:custom_fields] << { "#{id}": scf.errors.messages } + else + sample_annotation_notification(sample, scf) end end end @@ -235,6 +244,7 @@ class SamplesController < ApplicationController if val.empty? scf_to_delete << scf else + old_text = scf.value # SCF exists, update away scf.value = val @@ -242,6 +252,8 @@ class SamplesController < ApplicationController errors[:sample_custom_fields] << { "#{id}": scf.errors.messages } + else + sample_annotation_notification(sample, scf, old_text) end end else @@ -331,4 +343,19 @@ class SamplesController < ApplicationController :sample_group_id ) end + + def sample_annotation_notification(sample, scf, old_text = nil) + table_url = params.fetch(:request_url) { :request_url_must_be_present } + smart_annotation_notification( + old_text: (old_text if old_text), + new_text: scf.value, + title: t('notifications.sample_annotation_title', + user: current_user.full_name, + column: scf.custom_field.name, + sample: sample.name), + message: t('notifications.sample_annotation_message_html', + sample: link_to(sample.name, table_url), + column: link_to(scf.custom_field.name, table_url)) + ) + end end diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index 7179be67a..d1474e09c 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -1,6 +1,7 @@ class StepsController < ApplicationController include ActionView::Helpers::TextHelper include ApplicationHelper + include StepsActions before_action :load_vars, only: [:edit, :update, :destroy, :show] before_action :load_vars_nested, only: [:new, :create] @@ -686,293 +687,4 @@ class StepsController < ApplicationController ] ) end - - # generates notification for smart annotations - def create_annotation_notification(step) - # step description - smart_annotation_notification( - new_text: step.description, - title: t('notifications.step_description_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - # checklists - step.checklists.each do |checklist| - smart_annotation_notification( - new_text: checklist.name, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - checklist.checklist_items.each do |checklist_item| - smart_annotation_notification( - new_text: checklist_item.text, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - 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) - smart_annotation_notification( - old_text: old_description, - new_text: step.description, - title: t('notifications.step_description_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - - new_checklists.each do |e| - # generates smart annotaion if the checklist is new - add_new_checklist(step, e) if e.id.zero? - smart_annotation_notification( - new_text: e.name, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) 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 - smart_annotation_notification( - old_text: (old_checklist.name if old_checklist), - new_text: e.name, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - 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 - - smart_annotation_notification( - old_text: (old_item.text if old_item), - new_text: ci.text, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - end - end - end - - def add_new_checklist(step, checklist) - smart_annotation_notification( - new_text: checklist.name, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - - checklist.items.each do |ci| - smart_annotation_notification( - new_text: ci.text, - title: t('notifications.checklist_title', - user: current_user.full_name, - step: step.name), - message: 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) - )) - ) - end - 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 - - # def update_annotation_notification(step, updated_step) - # # step description - # smart_annotation_notification( - # old_text: - # new_text: updated_step.description, - # title: t('notifications.step_description_title'), - # message: t('notifications.step_description_message') - # ) - # # checklists - # updated_step.checklists.each do |checklist| - # smart_annotation_notification( - # - # new_text: checklist.name, - # title: t('notifications.checklist_title'), - # message: t('notifications.checklist_message') - # ) - # checklist.checklist_items.each do |checklist_item| - # smart_annotation_notification( - # new_text: checklist_item.text, - # title: t('notifications.checklist_item_title'), - # message: t('notifications.checklist_item_message') - # ) - # end - # end - # end end diff --git a/app/views/shared/_samples.html.erb b/app/views/shared/_samples.html.erb index 2652d0366..b77fc6b03 100644 --- a/app/views/shared/_samples.html.erb +++ b/app/views/shared/_samples.html.erb @@ -125,6 +125,7 @@
%{assigned_user} was added as %{role} to team %{team} by %{assigned_by_user}." unassign_user_from_team: "%{unassigned_user} was removed from team %{team} by %{unassigned_by_user}."