From 042a33f2c7c8e2f271ce0dfd60e52b8f80e75d0b Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Tue, 16 Aug 2016 08:49:24 +0200 Subject: [PATCH] Add activity for moving --- app/controllers/experiments_controller.rb | 43 ++++++++++++++--------- app/models/activity.rb | 3 +- app/models/tag.rb | 10 ++++++ config/locales/en.yml | 1 + 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 13d301544..73d17ce53 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -93,7 +93,7 @@ class ExperimentsController < ApplicationController # GET: clone_modal_experiment_path(id) def clone_modal - @projects = projects_with_role_above_user(true) + @projects = @experiment.projects_with_role_above_user(current_user) respond_to do |format| format.json do render json: { @@ -111,7 +111,7 @@ class ExperimentsController < ApplicationController # Try to clone the experiment success = true - if projects_with_role_above_user(true).include?(project) + if @experiment.projects_with_role_above_user(current_user).include?(project) cloned_experiment = @experiment.deep_clone_to_project(current_user, project) success = cloned_experiment.valid? @@ -144,7 +144,7 @@ class ExperimentsController < ApplicationController # GET: move_modal_experiment_path(id) def move_modal - @projects = projects_with_role_above_user(false) + @projects = @experiment.moveable_projects(current_user) respond_to do |format| format.json do render json: { @@ -158,8 +158,31 @@ class ExperimentsController < ApplicationController # POST: move_experiment(id) def move + project = Project.find_by_id(params[:experiment][:project_id]) + old_project = @experiment.project + + # Try to move the experiment success = true + if @experiment.moveable_projects(current_user).include?(project) + success = @experiment.move_to_project(project) + else + success = false + end + if success + Activity.create( + type_of: :move_experiment, + project: project, + user: current_user, + message: I18n.t( + "activities.move_experiment", + user: current_user.full_name, + experiment: @experiment.name, + project_new: project.name, + project_original: old_project.name + ) + ) + flash[:success] = t('experiments.move.success_flash', experiment: @experiment.name) redirect_to canvas_experiment_path(@experiment) @@ -230,18 +253,4 @@ class ExperimentsController < ApplicationController action_name.in?(%w(index archive)) ? 'main' : 'fluid' end - # Get projects where user is either owner or user in the same organization - # as this experiment - # - include_this_project: whether to include project from @experiment - def projects_with_role_above_user(include_this_project) - organization = @experiment.project.organization - projects = organization.projects.where(archived: false) - projects = projects - .where.not(id: @experiment.project.id) unless include_this_project - - current_user.user_projects - .where(project: projects) - .where('role < 2') - .map(&:project) - end end diff --git a/app/models/activity.rb b/app/models/activity.rb index 96d06ced8..506fbb0bc 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -27,7 +27,8 @@ class Activity < ActiveRecord::Base :add_comment_to_result, :archive_result, :edit_result, - :clone_experiment + :clone_experiment, + :move_experiment ] validates :type_of, presence: true diff --git a/app/models/tag.rb b/app/models/tag.rb index dcf285a21..e774c1494 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -43,4 +43,14 @@ class Tag < ActiveRecord::Base .offset((page - 1) * SEARCH_LIMIT) end end + + def deep_clone_to_project(project) + Tag.create( + name: name, + color: color, + created_by: created_by, + last_modified_by: last_modified_by, + project: project + ) + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index acd2e222f..222aa4e96 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1005,6 +1005,7 @@ en: edit_text_result: "%{user} edited text result %{result}." edit_table_result: "%{user} edited table result %{result}." clone_experiment: "%{user} cloned %{experiment_new} from %{experiment_original}." + move_experiment: "%{user} moved experiment %{experiment} from project %{project_original} to project %{project_new}." user_my_modules: new: