-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8655 from alphagov/add-worldwideorg-role
Add role association to Editionable Worldwide Organisations
- Loading branch information
Showing
23 changed files
with
241 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Edition::Roles | ||
extend ActiveSupport::Concern | ||
|
||
class Trait < Edition::Traits::Trait | ||
def process_associations_before_save(edition) | ||
@edition.edition_roles.each do |association| | ||
edition.edition_roles.build(association.attributes.except("id", "edition_id")) | ||
end | ||
end | ||
end | ||
|
||
included do | ||
has_many :edition_roles, foreign_key: :edition_id, inverse_of: :edition, dependent: :destroy, autosave: true | ||
has_many :roles, through: :edition_roles | ||
|
||
add_trait Trait | ||
end | ||
|
||
def can_be_associated_with_roles? | ||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class EditionRole < ApplicationRecord | ||
belongs_to :edition | ||
belongs_to :role, inverse_of: :edition_roles | ||
|
||
validates :edition, :role, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<% required = false unless defined?(required) %> | ||
|
||
<% cache_if edition.role_ids.empty?, "#{taggable_roles_cache_digest}-design-system" do %> | ||
<%= render "components/autocomplete", { | ||
id: "edition_roles", | ||
name: "edition[role_ids][]", | ||
error_items: errors_for(edition.errors, :roles), | ||
label: { | ||
text: "Roles" + "#{' (required)' if required}", | ||
heading_size: "m", | ||
}, | ||
select: { | ||
options: taggable_roles_container, | ||
multiple: true, | ||
selected: edition.role_ids, | ||
}, | ||
data: { | ||
module: "track-select-click", | ||
track_category: "worldLocationSelection", | ||
track_label: request.path, | ||
}, | ||
} %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<ul class="roles"> | ||
<% roles.each do |role| %> | ||
<%= content_tag_for(:li, roles) do %> | ||
<%= link_to role.name, admin_role_path(anchor: "role_#{role.id}"), class: "location-link" %> | ||
<% end %> | ||
<% end %> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddEditionRoles < ActiveRecord::Migration[7.0] | ||
def change | ||
create_join_table :edition, :roles, &:timestamps | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :edition_role do | ||
edition | ||
role | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module AdminEditionRolesBehaviour | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def should_allow_association_between_roles_and(document_type) | ||
edition_class = class_for(document_type) | ||
|
||
view_test "new displays document form with roles field" do | ||
get :new | ||
|
||
assert_select "form#new_edition" do | ||
assert_select "label[for=edition_roles]", text: "Roles" | ||
assert_select "#edition_roles" do |elements| | ||
assert_equal 1, elements.length | ||
end | ||
end | ||
end | ||
|
||
test "creating should create a new document with roles" do | ||
role1 = create(:role) | ||
role2 = create(:role) | ||
attributes = controller_attributes_for(document_type) | ||
|
||
post :create, | ||
params: { | ||
edition: attributes.merge( | ||
role_ids: [role1.id, role2.id], | ||
), | ||
} | ||
|
||
assert document = edition_class.last | ||
assert_equal [role1, role2], document.roles | ||
end | ||
|
||
view_test "edit displays document form with roles field" do | ||
edition = create(document_type) # rubocop:disable Rails/SaveBang | ||
get :edit, params: { id: edition } | ||
|
||
assert_select "form#edit_edition" do | ||
assert_select "label[for=edition_roles]", text: "Roles" | ||
|
||
assert_select "#edition_roles" do |elements| | ||
assert_equal 1, elements.length | ||
end | ||
end | ||
end | ||
|
||
test "updating should save modified document attributes with roles" do | ||
role1 = create(:role) | ||
role2 = create(:role) | ||
document = create(document_type, roles: [role2]) | ||
|
||
put :update, | ||
params: { id: document, | ||
edition: { | ||
role_ids: [role1.id], | ||
} } | ||
|
||
document = document.reload | ||
assert_equal [role1], document.roles | ||
end | ||
|
||
view_test "updating a stale document should render edit page with conflicting document and its roles" do | ||
document = create(document_type) # rubocop:disable Rails/SaveBang | ||
lock_version = document.lock_version | ||
document.touch | ||
|
||
put :update, params: { id: document, edition: { lock_version: } } | ||
|
||
assert_select ".conflict" do | ||
assert_select "h2", "Roles" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "test_helper" | ||
|
||
class EditionRoleTest < ActiveSupport::TestCase | ||
include ActionDispatch::TestProcess | ||
|
||
test "should be invalid without an edition" do | ||
edition_role = build(:edition_role, edition: nil) | ||
assert_not edition_role.valid? | ||
assert edition_role.errors[:edition].present? | ||
end | ||
|
||
test "should be invalid without an role" do | ||
edition_role = build(:edition_role, role: nil) | ||
assert_not edition_role.valid? | ||
assert edition_role.errors[:role].present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters