diff --git a/app/models/activity.rb b/app/models/activity.rb index a59f0ae1b..798d89dd3 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -32,6 +32,14 @@ class Activity < ApplicationRecord subject.nil? 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 def activity_version diff --git a/app/services/activities/create_activity_service.rb b/app/services/activities/create_activity_service.rb new file mode 100644 index 000000000..ab482b103 --- /dev/null +++ b/app/services/activities/create_activity_service.rb @@ -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 diff --git a/spec/services/activities/create_activity_service_spec.rb b/spec/services/activities/create_activity_service_spec.rb new file mode 100644 index 000000000..ea14cae26 --- /dev/null +++ b/spec/services/activities/create_activity_service_spec.rb @@ -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