Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting accountable to user clicking custom action #15043

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/models/custom_actions/actions/responsible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@
#++

class CustomActions::Actions::Responsible < CustomActions::Actions::Base
include CustomActions::Actions::Strategies::Associated
include CustomActions::Actions::Strategies::MeAssociated

def associated
def available_principles
User
.not_locked
.select(:id, :firstname, :lastname, :type)
.ordered_by_name
.map { |u| [u.id, u.name] }
end

def apply(work_package)
work_package.responsible_id = transformed_value(values.first)
end

def required?
false
end
Expand Down
65 changes: 58 additions & 7 deletions spec/models/custom_actions/actions/responsible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
require 'spec_helper'
require_relative '../shared_expectations'
require "spec_helper"
require_relative "../shared_expectations"

RSpec.describe CustomActions::Actions::Responsible do
let(:key) { :responsible }
Expand All @@ -39,20 +39,71 @@
.to receive_message_chain(:not_locked, :select, :ordered_by_name)
.and_return(principals)

[{ value: nil, label: '-' },
[{ value: nil, label: "-" },
{ value: "current_user", label: "(Assign to executing user)" },
{ value: principals.first.id, label: principals.first.name },
{ value: principals.last.id, label: principals.last.name }]
end

it_behaves_like 'base custom action'
it_behaves_like 'associated custom action' do
describe '#allowed_values' do
it 'is the list of all users and groups' do
it_behaves_like "base custom action"
it_behaves_like "associated custom action" do
describe "#allowed_values" do
it "is the list of all users and groups" do
allowed_values

expect(instance.allowed_values)
.to eql(allowed_values)
end
end
end

describe "current_user special value" do
let(:work_package) { build_stubbed(:work_package) }
let(:user) { build_stubbed(:user) }

subject { described_class.new }

before do
subject.values = ["current_user"]
end

it "can set the value" do
expect(subject).to have_me_value
end

it "includes the value in available_values" do
expect(subject.associated)
.to include([subject.current_user_value_key, I18n.t("custom_actions.actions.assigned_to.executing_user_value")])
end

context "when logged in" do
before do
login_as user
end

it "returns nil for the current user id" do
subject.apply work_package
expect(work_package.responsible_id).to eq(user.id)
end

it "validates the me value when executing" do
errors = ActiveModel::Errors.new(CustomAction.new)
subject.validate errors
expect(errors.symbols_for(:actions)).to be_empty
end
end

context "when not logged in" do
it "returns nil for the current user id" do
subject.apply work_package
expect(work_package.responsible_id).to be_nil
end

it "validates the me value when executing" do
errors = ActiveModel::Errors.new(CustomAction.new)
subject.validate errors
expect(errors.symbols_for(:actions)).to include :not_logged_in
end
end
end
end
Loading