scinote-web/app/services/create_my_module_service.rb

57 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class CreateMyModuleService
def initialize(user, team, params)
@params = params
@my_module_params = params[:my_module] || {}
@user = user
@team = team
end
def call
new_my_module = nil
ActiveRecord::Base.transaction do
2020-02-25 20:49:35 +08:00
unless @params[:experiment].class == Experiment
@params[:experiment][:project] = @params[:project]
@params[:experiment] = CreateExperimentService.new(@user, @team, @params[:experiment]).call
end
unless @params[:experiment]&.errors&.empty?
new_my_module = @params[:experiment]
raise ActiveRecord::Rollback
end
@my_module_params[:x] ||= 0
@my_module_params[:y] ||= 0
@my_module_params[:name] ||= I18n.t('create_task_service.default_task_name')
2020-02-25 20:49:35 +08:00
@my_module = @params[:experiment].my_modules.new(@my_module_params)
new_pos = @my_module.get_new_position
@my_module.x = new_pos[:x]
@my_module.y = new_pos[:y]
2020-05-20 14:59:07 +08:00
@my_module.created_by = @user
@my_module.last_modified_by = @user
@my_module.save!
create_my_module_activity
2020-03-30 21:16:14 +08:00
@my_module.assign_user(@user)
new_my_module = @my_module
end
new_my_module
end
private
def create_my_module_activity
Activities::CreateActivityService
.call(activity_type: :create_module,
owner: @user,
team: @team,
2020-02-25 20:49:35 +08:00
project: @params[:experiment].project,
subject: @my_module,
message_items: { my_module: @my_module.id })
end
end