Add service for activity creation

This commit is contained in:
Urban Rotnik 2019-02-27 07:29:53 +01:00
parent bc3d5ae87c
commit 117944117b
3 changed files with 78 additions and 0 deletions

View file

@ -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

View 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

View 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