mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-31 12:09:17 +08:00
Add service for activity creation
This commit is contained in:
parent
bc3d5ae87c
commit
117944117b
3 changed files with 78 additions and 0 deletions
|
@ -32,6 +32,14 @@ class Activity < ApplicationRecord
|
||||||
subject.nil?
|
subject.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def message_items
|
||||||
|
values['message_items'].with_indifferent_access.merge(user: owner.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def html_message
|
||||||
|
I18n.t "activities.#{type_of}", message_items
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def activity_version
|
def activity_version
|
||||||
|
|
46
app/services/activities/create_activity_service.rb
Normal file
46
app/services/activities/create_activity_service.rb
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Activities
|
||||||
|
class CreateActivityService
|
||||||
|
extend Service
|
||||||
|
|
||||||
|
attr_reader :errors, :activity
|
||||||
|
|
||||||
|
def initialize(activity_type:,
|
||||||
|
owner:,
|
||||||
|
team:,
|
||||||
|
project:,
|
||||||
|
subject:,
|
||||||
|
message_items: {})
|
||||||
|
@activity = Activity.new
|
||||||
|
@activity.type_of = activity_type
|
||||||
|
@activity.owner = owner
|
||||||
|
@activity.team = team
|
||||||
|
@activity.subject = subject
|
||||||
|
@activity.project = project if project
|
||||||
|
@activity.values = { message_items: message_items }
|
||||||
|
|
||||||
|
@errors = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
generate_breadcrumbs
|
||||||
|
@activity.save
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def succeed?
|
||||||
|
@errors.none?
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def generate_breadcrumbs
|
||||||
|
@activity.values[:breadcrumbs] = [
|
||||||
|
{ team: { id: @activity.team.id, value: @activity.team.name } },
|
||||||
|
{ project: { id: @activity.project.id, value: @activity.project.name } }
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
24
spec/services/activities/create_activity_service_spec.rb
Normal file
24
spec/services/activities/create_activity_service_spec.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Activities::CreateActivityService do
|
||||||
|
let(:user) { create :user }
|
||||||
|
let(:team) { create :team, :with_members }
|
||||||
|
let(:project) do
|
||||||
|
create :project, team: team, user_projects: []
|
||||||
|
end
|
||||||
|
let(:service_call) do
|
||||||
|
Activities::CreateActivityService
|
||||||
|
.call(activity_type: :create_project,
|
||||||
|
owner: user,
|
||||||
|
subject: project,
|
||||||
|
team: team,
|
||||||
|
project: project,
|
||||||
|
message_items: { project: project.id })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'adds new activiy in DB' do
|
||||||
|
expect { service_call }.to(change { Activity.count })
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue