-
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.
Allow an item to be created or edited
This adds a new controller that inherits from the main `EditionsController` and overrides some of the default behaviour to allow our items to be edited/created. We also add a new edit/new view to override the main editions views which are already quite overloaded with logic. At the moment, creation and editing fails when sending data to the Publishing API, but this is a start.
- Loading branch information
Showing
9 changed files
with
214 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Admin::ObjectStore | ||
class ItemsController < Admin::EditionsController | ||
private | ||
|
||
def edition_class | ||
ObjectStore::Item | ||
end | ||
|
||
def permitted_edition_attributes | ||
(super << ObjectStore.field_names_for_item_type(params[:item_type])).flatten | ||
end | ||
|
||
def show_or_edit_path | ||
if params[:save].present? | ||
edit_admin_object_store_item_path(@edition.item_type, @edition) | ||
else | ||
admin_object_store_item_path @edition | ||
end | ||
end | ||
|
||
def new_edition | ||
edition = edition_class.new(item_type: params[:item_type]) | ||
edition.assign_attributes(new_edition_params) | ||
edition | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Admin | ||
module ObjectStore | ||
module ItemsHelper | ||
def edition_name(edition) | ||
edition.item_type.titleize | ||
end | ||
|
||
def render_partial_path(edition, partial_name) | ||
render partial: partial_path(edition, partial_name), object: edition, as: edition.item_type | ||
end | ||
|
||
def partial_path(edition, partial_name) | ||
dirname = edition.item_type.pluralize | ||
File.join("admin", "object_store", dirname, partial_name) | ||
end | ||
|
||
def object_store_item_form(edition) | ||
form_for edition, url: form_url_for_object_store_item(edition), as: edition.item_type do |form| | ||
yield(form) | ||
concat render("govuk_publishing_components/components/button", { | ||
text: "Save", | ||
value: "save", | ||
name: "save", | ||
data_attributes: { | ||
module: "gem-track-click", | ||
"track-category": "form-button", | ||
"track-action": "object-store-item-button", | ||
"track-label": "Save", | ||
}, | ||
}) | ||
end | ||
end | ||
|
||
def form_url_for_object_store_item(edition) | ||
admin_object_store_items_path(item_type: edition.item_type) | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
app/views/admin/object_store/email_addresses/_form.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,21 @@ | ||
<%= object_store_item_form(email_address) do |form| %> | ||
<%= render "govuk_publishing_components/components/input", { | ||
label: { | ||
text: "Title", | ||
}, | ||
heading_size: "m", | ||
value: form.object.title, | ||
name: "edition[title]", | ||
id: "edition_title", | ||
} %> | ||
|
||
<%= render "govuk_publishing_components/components/input", { | ||
label: { | ||
text: "Email address", | ||
}, | ||
heading_size: "m", | ||
value: form.object.email_address, | ||
name: "edition[email_address]", | ||
id: "edition_email_address", | ||
} %> | ||
<% 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,10 @@ | ||
<% content_for :page_title, "Object Store - Edit #{edition_name(@edition)}" %> | ||
<% content_for :context, "Object Store" %> | ||
<% content_for :title, "Edit #{edition_name(@edition)}" %> | ||
<% content_for :error_summary, render(Admin::ErrorSummaryComponent.new(object: @edition, parent_class: "edition")) %> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<%= render_partial_path(@edition, "form") %> | ||
</div> | ||
</div> |
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,10 @@ | ||
<% content_for :page_title, "Object Store - New #{edition_name(@edition)}" %> | ||
<% content_for :context, "Object Store" %> | ||
<% content_for :title, "New #{edition_name(@edition)}" %> | ||
<% content_for :error_summary, render(Admin::ErrorSummaryComponent.new(object: @edition, parent_class: "edition")) %> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<%= render_partial_path(@edition, "form") %> | ||
</div> | ||
</div> |
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,32 @@ | ||
require "test_helper" | ||
require "capybara/rails" | ||
|
||
class CreateEmailTest < ActionDispatch::IntegrationTest | ||
include Capybara::DSL | ||
|
||
setup do | ||
login_as_admin | ||
end | ||
|
||
test "allows an email object to be created" do | ||
visit new_admin_object_store_item_path(item_type: "email_address") | ||
|
||
fill_in "Title", with: "Some Title" | ||
fill_in "Email address", with: "[email protected]" | ||
click_on "Save" | ||
|
||
created_object = ObjectStore::Item.find_by(title: "Some Title") | ||
|
||
assert_not_nil created_object | ||
assert created_object.email_address, "[email protected]" | ||
end | ||
|
||
test "shows errors when missing fields blank" do | ||
visit new_admin_object_store_item_path(item_type: "email_address") | ||
|
||
click_on "Save" | ||
|
||
assert_text "Title can't be blank" | ||
assert_text "Email address can't be blank" | ||
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,22 @@ | ||
require "test_helper" | ||
require "capybara/rails" | ||
|
||
class EditEmailTest < ActionDispatch::IntegrationTest | ||
include Capybara::DSL | ||
|
||
setup do | ||
login_as_admin | ||
end | ||
|
||
test "allows an email object to be updated" do | ||
item = create(:object_store_item, item_type: "email_address", email_address: "[email protected]") | ||
visit edit_admin_object_store_item_path(item_type: "email_address", id: item.id) | ||
|
||
fill_in "Email address", with: "[email protected]" | ||
click_on "Save" | ||
|
||
item.reload | ||
|
||
assert item.email_address, "[email protected]" | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
test/unit/app/helpers/admin/object_store/items_helper_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 Admin::ObjectStore::ItemsHelperTest < ActionView::TestCase | ||
include Admin::ObjectStore::ItemsHelper | ||
|
||
test "#edition_name returns the title for an edition" do | ||
edition = build(:object_store_item, item_type: "email_address") | ||
assert_equal edition_name(edition), "Email Address" | ||
end | ||
|
||
test "#partial_path should return a path for an edition" do | ||
edition = build(:object_store_item, item_type: "email_address") | ||
path = partial_path(edition, "form") | ||
assert_equal path, "admin/object_store/email_addresses/form" | ||
end | ||
|
||
test "#render_partial_path should render the correct path with the edition" do | ||
edition = build(:object_store_item, item_type: "email_address") | ||
partial_name = "form" | ||
|
||
expects(:render).with(partial: partial_path(edition, partial_name), object: edition, as: edition.item_type) | ||
|
||
render_partial_path(edition, partial_name) | ||
end | ||
|
||
test "#object_store_item_form renders a form" do | ||
edition = build(:object_store_item, item_type: "email_address") | ||
|
||
html = object_store_item_form(edition) do |_form| | ||
concat("Some text here") | ||
end | ||
|
||
assert_includes html, "Some text here" | ||
assert_includes html, form_url_for_object_store_item(edition) | ||
assert_includes html, render("govuk_publishing_components/components/button", { | ||
text: "Save", | ||
value: "save", | ||
name: "save", | ||
data_attributes: { | ||
module: "gem-track-click", | ||
"track-category": "form-button", | ||
"track-action": "object-store-item-button", | ||
"track-label": "Save", | ||
}, | ||
}) | ||
end | ||
end |