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

Toolbar refactoring #disable_button: UserRole #552

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
12 changes: 12 additions & 0 deletions app/helpers/application_helper/button/rbac_role_delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ApplicationHelper::Button::RbacRoleDelete < ApplicationHelper::Button::Basic
needs :@record

def disabled?
@error_message = if @record.read_only
_('This Role is Read Only and can not be deleted')
elsif @record.group_count > 0
_('This Role is in use by one or more Groups and can not be deleted')
end
@error_message.present?
end
end
8 changes: 8 additions & 0 deletions app/helpers/application_helper/button/rbac_role_edit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ApplicationHelper::Button::RbacRoleEdit < ApplicationHelper::Button::Basic
needs :@record

def disabled?
@error_message = _('This Role is Read Only and can not be edited') if @record.read_only
@error_message.present?
end
end
6 changes: 4 additions & 2 deletions app/helpers/application_helper/toolbar/user_role_center.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class ApplicationHelper::Toolbar::UserRoleCenter < ApplicationHelper::Toolbar::B
:rbac_role_edit,
'pficon pficon-edit fa-lg',
t = N_('Edit this Role'),
t),
t,
:klass => ApplicationHelper::Button::RbacRoleEdit),
button(
:rbac_role_copy,
'fa fa-files-o fa-lg',
Expand All @@ -22,7 +23,8 @@ class ApplicationHelper::Toolbar::UserRoleCenter < ApplicationHelper::Toolbar::B
t = N_('Delete this Role'),
t,
:url_parms => "&refresh=y",
:confirm => N_("Are you sure you want to delete this Role?")),
:confirm => N_("Are you sure you want to delete this Role?"),
:klass => ApplicationHelper::Button::RbacRoleDelete),
]
),
])
Expand Down
8 changes: 0 additions & 8 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,6 @@ def disable_button(id)
when "rbac_user_delete"
return N_("User [Administrator] can not be deleted") if @record.userid == 'admin'
end
when "UserRole"
case id
when "rbac_role_delete"
return N_("This Role is Read Only and can not be deleted") if @record.read_only
return N_("This Role is in use by one or more Groups and can not be deleted") if @record.group_count > 0
when "rbac_role_edit"
return N_("This Role is Read Only and can not be edited") if @record.read_only
end
when "MiqTemplate"
case id
when "image_check_compliance", "miq_template_check_compliance"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe ApplicationHelper::Button::RbacRoleDelete do
let(:view_context) { setup_view_context_with_sandbox({}) }
let(:read_only) { false }
let(:record) { FactoryGirl.create(:miq_user_role, :read_only => read_only) }
let(:button) { described_class.new(view_context, {}, {'record' => record}, {}) }

describe '#calculate_properties' do
let(:setup_miq_groups) {}
before do
setup_miq_groups
button.calculate_properties
end
after(:each) { tear_down }

def tear_down
MiqGroup.delete_all
end

context 'when role is writable' do
context 'when role is not in use by any Group' do
it_behaves_like 'an enabled button'
end
context 'when role is in use by one or more Groups' do
let(:setup_miq_groups) { FactoryGirl.create(:miq_group, :miq_user_role => record) }
it_behaves_like 'a disabled button', 'This Role is in use by one or more Groups and can not be deleted'
end
end
context 'when role is read-only' do
let(:read_only) { true }
it_behaves_like 'a disabled button', 'This Role is Read Only and can not be deleted'
end
end
end
18 changes: 18 additions & 0 deletions spec/helpers/application_helper/buttons/rbac_role_edit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
describe ApplicationHelper::Button::RbacRoleEdit do
let(:view_context) { setup_view_context_with_sandbox({}) }
let(:button) { described_class.new(view_context, {}, {'record' => record}, {}) }

describe '#calculate_properties' do
let(:record) { FactoryGirl.create(:miq_user_role, :read_only => read_only) }
before { button.calculate_properties }

context 'when role is writable' do
let(:read_only) { false }
it_behaves_like 'an enabled button'
end
context 'when role is read-only' do
let(:read_only) { true }
it_behaves_like 'a disabled button', 'This Role is Read Only and can not be edited'
end
end
end