From 53456988ba3a245847f6d6626571922fd74bad28 Mon Sep 17 00:00:00 2001 From: aignatov-bio <47317017+aignatov-bio@users.noreply.github.com> Date: Mon, 19 Jul 2021 21:33:28 +0200 Subject: [PATCH] Add bioEddie activities and user licenses [SCI-5845][SCI-5842] (#3411) * Add bio eddie licens to users [SCI-5845] * Add activities for BioEddie [SCI-5842] --- app/assets/javascripts/sitewide/bio_eddie.js | 2 + .../bio_eddie_assets_controller.rb | 7 +- app/controllers/concerns/bio_eddie_actions.rb | 105 ++++++++++++++++++ app/controllers/steps_controller.rb | 4 + app/services/bio_eddie_service.rb | 4 +- .../bio_eddie/_create_bio_eddie_li.html.erb | 24 ++-- app/views/shared/_bio_eddie_modal.html.erb | 2 +- .../shared/file_preview/_content.html.erb | 3 +- config/initializers/extends.rb | 17 ++- config/locales/global_activities/en.yml | 20 ++++ 10 files changed, 168 insertions(+), 20 deletions(-) create mode 100644 app/controllers/concerns/bio_eddie_actions.rb diff --git a/app/assets/javascripts/sitewide/bio_eddie.js b/app/assets/javascripts/sitewide/bio_eddie.js index 0f4106f77..2b91e211d 100644 --- a/app/assets/javascripts/sitewide/bio_eddie.js +++ b/app/assets/javascripts/sitewide/bio_eddie.js @@ -128,6 +128,7 @@ var bioEddieEditor = (function() { bioEddieModal.data('update-url', updateUrl); bioEddieModal.find('.file-name input').val(name); bioEddieModal.modal('show'); + } }; }()); @@ -148,5 +149,6 @@ var bioEddieEditor = (function() { this.dataset.moleculeDescription, this.dataset.updateUrl ); + $.post(this.dataset.editUrl); }); }()); diff --git a/app/controllers/bio_eddie_assets_controller.rb b/app/controllers/bio_eddie_assets_controller.rb index d282031c0..836e6fa5f 100644 --- a/app/controllers/bio_eddie_assets_controller.rb +++ b/app/controllers/bio_eddie_assets_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class BioEddieAssetsController < ApplicationController + include BioEddieActions include ActiveStorage::SetCurrent before_action :load_vars, except: :create @@ -12,6 +13,8 @@ class BioEddieAssetsController < ApplicationController def create asset = BioEddieService.create_molecule(bio_eddie_params, current_user, current_team) + create_create_bio_eddie_activity(asset, current_user) + if asset && bio_eddie_params[:object_type] == 'Step' render json: { html: render_to_string(partial: 'assets/asset.html.erb', locals: { @@ -29,6 +32,8 @@ class BioEddieAssetsController < ApplicationController def update asset = BioEddieService.update_molecule(bio_eddie_params, current_user, current_team) + create_edit_bio_eddie_activity(asset, current_user, :finish_editing) + if asset render json: { url: rails_representation_url(asset.medium_preview), id: asset.id, @@ -39,7 +44,7 @@ class BioEddieAssetsController < ApplicationController end def start_editing - # Activity here + create_edit_bio_eddie_activity(@asset, current_user, :start_editing) end private diff --git a/app/controllers/concerns/bio_eddie_actions.rb b/app/controllers/concerns/bio_eddie_actions.rb new file mode 100644 index 000000000..c264968ba --- /dev/null +++ b/app/controllers/concerns/bio_eddie_actions.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +module BioEddieActions + extend ActiveSupport::Concern + + private + + def create_edit_bio_eddie_activity(asset, current_user, started_editing) + action = case started_editing + when :start_editing + t('activities.file_editing.started') + when :finish_editing + t('activities.file_editing.finished') + end + return unless bio_eddie_asset_validation(asset) + + bio_eddie_find_target_object(asset, current_user, 'edit', action) + end + + def create_create_bio_eddie_activity(asset, current_user) + return unless bio_eddie_asset_validation(asset) + + bio_eddie_find_target_object(asset, current_user, 'create') + end + + def create_delete_bio_eddie_activity(asset, current_user) + return unless bio_eddie_asset_validation(asset) + + bio_eddie_find_target_object(asset, current_user, 'delete') + end + + def bio_eddie_asset_validation(asset) + asset && asset.file.metadata[:asset_type] == 'bio_eddie' + end + + def bio_eddie_asset_type(asset, klass) + return true if asset.step_asset&.step.instance_of?(klass) + return true if asset.result_asset&.result.instance_of?(klass) + + false + end + + def bio_eddie_find_target_object(asset, current_user, activity_type, action = nil) + if bio_eddie_asset_type(asset, Step) + bio_eddie_step_activity(asset, current_user, activity_type, action) + elsif bio_eddie_asset_type(asset, Result) + bio_eddie_result_activity(asset, current_user, activity_type, action) + end + end + + def bio_eddie_step_activity(asset, current_user, activity, action = nil) + step = asset.step_asset&.step + asset_type = 'asset_name' + protocol = step&.protocol + + return unless step && protocol + + default_step_items = + { step: step.id, + step_position: { id: step.id, value_for: 'position_plus_one' }, + asset_type => { id: asset.id, value_for: 'file_name' } } + + default_step_items[:action] = action if action + if protocol.in_module? + project = protocol.my_module.experiment.project + team = project.team + type_of = "#{activity}_molecule_on_step".to_sym + message_items = { my_module: protocol.my_module.id } + else + type_of = "#{activity}_molecule_on_step_in_repository".to_sym + team = protocol.team + message_items = { protocol: protocol.id } + end + message_items = default_step_items.merge(message_items) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + subject: protocol, + team: team, + project: project, + message_items: message_items) + end + + def bio_eddie_result_activity(asset, current_user, activity, action = nil) + result = asset.result_asset&.result + asset_type = 'asset_name' + my_module = result&.my_module + + return unless result && my_module + + message_items = { + result: result.id, + asset_type => { id: asset.id, value_for: 'file_name' } + } + + message_items[:action] = action if action + Activities::CreateActivityService + .call(activity_type: "#{activity}_molecule_on_step_on_result".to_sym, + owner: current_user, + subject: result, + team: my_module.experiment.project.team, + project: my_module.experiment.project, + message_items: message_items) + end +end diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index aa7d75161..1bb3d22f1 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -533,6 +533,10 @@ class StepsController < ApplicationController marvin_js_assets_attributes: %i( id _destroy + ), + bio_eddie_assets_attributes: %i( + id + _destroy ) ) end diff --git a/app/services/bio_eddie_service.rb b/app/services/bio_eddie_service.rb index 5f1de71de..7e3527160 100644 --- a/app/services/bio_eddie_service.rb +++ b/app/services/bio_eddie_service.rb @@ -6,8 +6,8 @@ class BioEddieService ApplicationSettings.instance.values['bio_eddie_url'] end - def enabled? - url.present? + def enabled?(current_user) + url.present? && current_user.settings.fetch('bio_eddie_enabled', false) end def create_molecule(params, current_user, current_team) diff --git a/app/views/assets/bio_eddie/_create_bio_eddie_li.html.erb b/app/views/assets/bio_eddie/_create_bio_eddie_li.html.erb index 9cf91bd64..0282bf679 100644 --- a/app/views/assets/bio_eddie/_create_bio_eddie_li.html.erb +++ b/app/views/assets/bio_eddie/_create_bio_eddie_li.html.erb @@ -1,11 +1,13 @@ -
  • - - <%= image_tag 'icon_small/bio_eddie.png' %> - <%= t('bio_eddie.new_button') %> - -
  • +<% if BioEddieService.enabled?(current_user) %> +
  • + + <%= image_tag 'icon_small/bio_eddie.png' %> + <%= t('bio_eddie.new_button') %> + +
  • +<% end %> diff --git a/app/views/shared/_bio_eddie_modal.html.erb b/app/views/shared/_bio_eddie_modal.html.erb index 4ab9d12e6..c9addd8bc 100644 --- a/app/views/shared/_bio_eddie_modal.html.erb +++ b/app/views/shared/_bio_eddie_modal.html.erb @@ -1,4 +1,4 @@ -<% if BioEddieService.enabled? %> +<% if BioEddieService.enabled?(current_user) %>