-
Notifications
You must be signed in to change notification settings - Fork 194
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 #9557 from alphagov/content-modelling/627-filter-b…
…y-type Content modelling/627 filter by type and lead organisation
- Loading branch information
Showing
13 changed files
with
232 additions
and
25 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...ents/content_block_manager/content_block/document/index/filter_options_component.html.erb
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,35 @@ | ||
<%= form_with url: helpers.content_block_manager.content_block_manager_content_block_documents_path, method: :get do %> | ||
<%= render "govuk_publishing_components/components/input", { | ||
label: { | ||
text: "Keyword", | ||
bold: true, | ||
}, | ||
hint: 'For example, "driving standards"', | ||
name: "keyword", | ||
id: "keyword_filter", | ||
value: !@filters.nil? && @filters[:keyword], | ||
} %> | ||
<%= render "govuk_publishing_components/components/checkboxes", { | ||
heading: "Content block type", | ||
heading_size: "s", | ||
no_hint_text: true, | ||
id: "block_type", | ||
name: "block_type[]", | ||
items: items_for_block_type, | ||
} %> | ||
<%= render "components/select_with_search", { | ||
id: "lead_organisation", | ||
name: "lead_organisation", | ||
label: "Lead organisation", | ||
heading_size: "s", | ||
include_blank: true, | ||
options: options_for_lead_organisation, | ||
} %> | ||
<%= render "govuk_publishing_components/components/button", { | ||
text: "View results", | ||
margin_bottom: 4, | ||
} %> | ||
<% end %> |
28 changes: 28 additions & 0 deletions
28
...components/content_block_manager/content_block/document/index/filter_options_component.rb
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,28 @@ | ||
class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent < ViewComponent::Base | ||
include ActionView::Helpers::RecordTagHelper | ||
def initialize(filters:) | ||
@filters = filters | ||
end | ||
|
||
private | ||
|
||
def items_for_block_type | ||
ContentBlockManager::ContentBlock::Schema.valid_schemas.map do |schema_name| | ||
{ | ||
label: schema_name.humanize, | ||
value: schema_name, | ||
checked: !@filters.nil? && @filters[:block_type]&.include?(schema_name), | ||
} | ||
end | ||
end | ||
|
||
def options_for_lead_organisation | ||
helpers.taggable_organisations_container.map do |name, id| | ||
{ | ||
text: name, | ||
value: id, | ||
selected: !@filters.nil? && @filters[:lead_organisation] == id.to_s, | ||
} | ||
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
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
12 changes: 12 additions & 0 deletions
12
...ls/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb
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,12 @@ | ||
module ContentBlockManager | ||
module ContentBlock::Document::Scopes::SearchableByLeadOrganisation | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
scope :with_lead_organisation, | ||
lambda { |id| | ||
joins(latest_edition: :edition_organisation).where("content_block_edition_organisations.organisation_id = :id", id:) | ||
} | ||
end | ||
end | ||
end |
17 changes: 0 additions & 17 deletions
17
..._manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb
This file was deleted.
Oops, something went wrong.
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
47 changes: 47 additions & 0 deletions
47
...ock_manager/test/components/content_block/document/index/filter_options_component_test.rb
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,47 @@ | ||
require "test_helper" | ||
|
||
class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponentTest < ViewComponent::TestCase | ||
test "adds value of keyword to text input from filter" do | ||
render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( | ||
filters: { keyword: "ministry defense" }, | ||
)) | ||
|
||
assert_selector "input[name='keyword'][value='ministry defense']" | ||
end | ||
|
||
test "renders checkbox items for all valid schemas" do | ||
ContentBlockManager::ContentBlock::Schema.expects(:valid_schemas).returns(%w[email_address postal_address]) | ||
render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( | ||
filters: {}, | ||
)) | ||
|
||
assert_selector "input[type='checkbox'][name='block_type[]'][value='email_address']" | ||
assert_selector "input[type='checkbox'][name='block_type[]'][value='postal_address']" | ||
end | ||
|
||
test "checks checkbox items if checked in filters" do | ||
ContentBlockManager::ContentBlock::Schema.expects(:valid_schemas).returns(%w[email_address postal_address]) | ||
render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( | ||
filters: { block_type: %w[email_address] }, | ||
)) | ||
|
||
assert_selector "input[type='checkbox'][name='block_type[]'][value='email_address'][checked]" | ||
assert_selector "input[type='checkbox'][name='block_type[]'][value='postal_address']" | ||
end | ||
|
||
test "selects organisation if selected in filters" do | ||
helper_mock = mock | ||
ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.any_instance.stubs(:helpers).returns(helper_mock) | ||
helper_mock.stubs(:content_block_manager).returns(helper_mock) | ||
helper_mock.stubs(:content_block_manager_content_block_documents_path).returns("path") | ||
helper_mock.stubs(:taggable_organisations_container).returns( | ||
[["Department of Placeholder", 1], ["Ministry of Example", 2]], | ||
) | ||
render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( | ||
filters: { lead_organisation: "2" }, | ||
)) | ||
|
||
assert_selector "select[name='lead_organisation']" | ||
assert_selector "option[selected='selected'][value=2]" | ||
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
20 changes: 20 additions & 0 deletions
20
.../app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb
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,20 @@ | ||
require "test_helper" | ||
|
||
class ContentBlockManager::SearchableByLeadOrganisationTest < ActiveSupport::TestCase | ||
extend Minitest::Spec::DSL | ||
|
||
describe ".with_lead_organisation" do | ||
test "finds documents with lead organisation on latest edition" do | ||
matching_organisation = create(:organisation, id: "1234") | ||
document_with_org = create(:content_block_document, :email_address) | ||
_edition_with_org = create(:content_block_edition, | ||
:email_address, | ||
document: document_with_org, | ||
organisation: matching_organisation) | ||
document_without_org = create(:content_block_document, :email_address) | ||
_edition_without_org = create(:content_block_edition, :email_address, document: document_without_org) | ||
_document_without_latest_edition = create(:content_block_document, :email_address) | ||
assert_equal [document_with_org], ContentBlockManager::ContentBlock::Document.with_lead_organisation("1234") | ||
end | ||
end | ||
end |