Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #59 from alphagov/whitehall_support2

Whitehall support
  • Loading branch information...
commit 71955b5abeece2a4b7e9bbcc33bc31fc98c24e25 2 parents f8345bd + e1c8f61
@fatbusinessman fatbusinessman authored
View
2  Gemfile
@@ -32,7 +32,7 @@ gem 'lograge', '~> 0.1.0'
if ENV['CONTENT_MODELS_DEV']
gem "govuk_content_models", path: '../govuk_content_models'
else
- gem "govuk_content_models", "4.7.0"
+ gem "govuk_content_models", "4.10.0"
end
if ENV['BUNDLE_DEV']
View
4 Gemfile.lock
@@ -120,7 +120,7 @@ GEM
htmlentities (~> 4)
kramdown (~> 0.13.3)
sanitize (= 2.0.3)
- govuk_content_models (4.7.0)
+ govuk_content_models (4.10.0)
bson_ext
differ
gds-api-adapters
@@ -324,7 +324,7 @@ DEPENDENCIES
gds-api-adapters (= 4.1.3)
gds-sso (= 3.0.0)
gelf
- govuk_content_models (= 4.7.0)
+ govuk_content_models (= 4.10.0)
jquery-rails (= 2.0.2)
jquery-ui-rails (= 3.0.1)
launchy
View
32 app/controllers/artefacts_controller.rb
@@ -35,7 +35,11 @@ def edit
def create
@artefact.save_as action_user
- location = admin_url_for_edition(@artefact, params.slice(:return_to))
+ if @artefact.owning_app == "publisher"
+ location = admin_url_for_edition(@artefact, params.slice(:return_to))
+ else
+ location = edit_artefact_path(@artefact.id)
+ end
respond_with @artefact, location: location
end
@@ -64,17 +68,21 @@ def update
saved = @artefact.update_attributes_as(action_user, parameters_to_use)
flash[:notice] = saved ? 'Panopticon item updated' : 'Failed to save item'
- if saved && params[:commit] == 'Save and continue editing'
- redirect_to edit_artefact_path(@artefact)
- else
- @actions = build_actions
- respond_with @artefact, status: status_to_use do |format|
- format.json do
- if saved
- render json: @artefact.to_json, status: status_to_use
- else
- render json: {"errors" => @artefact.errors.full_messages}, status: 422
- end
+ @actions = build_actions
+ respond_with @artefact, status: status_to_use do |format|
+ format.html do
+ continue_editing = (params[:commit] == 'Save and continue editing')
+ if saved && (continue_editing || (@artefact.owning_app != "publisher"))
+ redirect_to edit_artefact_path(@artefact)
+ else
+ respond_with @artefact, status: status_to_use
+ end
+ end
+ format.json do
+ if saved
+ render json: @artefact.to_json, status: status_to_use
+ else
+ render json: {"errors" => @artefact.errors.full_messages}, status: 422
end
end
end
View
16 app/helpers/application_helper.rb
@@ -1,12 +1,18 @@
module ApplicationHelper
# Set class on active navigation items
def nav_link(text, link)
+ determinant_params = ["owning_app"]
+ link_query = Rack::Utils.parse_nested_query(URI.parse(link).query)
+ query_matches = determinant_params.all? { |p| params[p] == link_query[p] }
+
recognized = Rails.application.routes.recognize_path(link)
- if recognized[:controller] == params[:controller] &&
- recognized[:action] == params[:action]
- content_tag(:li, :class => "active") do
- link_to( text, link)
- end
+ controller_matches = recognized[:controller] == params[:controller]
+ action_matches = recognized[:action] == params[:action]
+
+ if (controller_matches && action_matches && query_matches)
+ content_tag(:li, :class => "active") do
+ link_to( text, link)
+ end
else
content_tag(:li) do
link_to( text, link)
View
8 app/helpers/artefacts_helper.rb
@@ -14,4 +14,12 @@ def human_timestamp(timestamp)
def name_hint_for(artefact)
artefact.persisted? ? "This should be edited in #{artefact.owning_app}" : "A name/title for the item"
end
+
+ def non_whitehall_formats
+ Artefact::FORMATS_BY_DEFAULT_OWNING_APP.each_with_object([]) do |(owning_app, formats), result|
+ if owning_app != "whitehall"
+ result.concat(formats)
+ end
+ end
+ end
end
View
2  app/models/rummageable_artefact.rb
@@ -1,6 +1,6 @@
class RummageableArtefact
- FORMATS_NOT_TO_INDEX = %W(business_support completed_transaction)
+ FORMATS_NOT_TO_INDEX = %W(business_support completed_transaction) + Artefact::FORMATS_BY_DEFAULT_OWNING_APP["whitehall"]
def initialize(artefact)
@artefact = artefact
View
2  app/views/artefacts/_form.html.erb
@@ -37,7 +37,7 @@
<hr>
<input type="hidden" name="artefact[sections][]" value="" />
<% if artefact.new_record? %>
- <%= f.input :kind, :collection => Artefact::FORMATS.map { |s| [s.humanize, s]}, :as => :select, :class => "span6", :prompt => "Select a kind" %>
+ <%= f.input :kind, :collection => non_whitehall_formats.map { |s| [s.humanize, s]}, :as => :select, :class => "span6", :prompt => "Select a kind" %>
<% end %>
<label for="artefact[sections][]">Sections</label>
View
63 app/views/artefacts/_whitehall_form.html.erb
@@ -0,0 +1,63 @@
+<% if flash[:notice].present? %>
+ <div class="alert alert-success"><%= flash[:notice] %></div>
+<% end %>
+
+<% if artefact.errors.count > 0 %>
+ <div class="alert alert-error">
+ <ul>
+ <% artefact.errors.full_messages.each do |message| %>
+ <li><%= message %></li>
+ <% end %>
+ </ul>
+ </div>
+<% end %>
+
+<% if artefact.archived? %>
+ <div class="alert alert-error">
+ <h2>Stop! You can't edit this artefact because it has been archived.</h2>
+ </div>
+<% else %>
+ <div class="well">
+ <%= semantic_form_for(artefact, :html => { :class => '', :id => 'edit_artefact'}) do |f| %>
+ <%= f.input :owning_app, :as => :hidden %>
+ <%= f.input :state, :as => :hidden, :input_html => { :value => "live" } %>
+ <%= f.inputs do %>
+ <%= f.input :name, :input_html => { :class => "span6" }, :hint => "A name/title for the item" %>
+ <%= f.input :description, :input_html => { :class => "span6", :rows => 6 }, :as => :text %>
+ <%= f.input :slug, :input_html => { :class => "span6" }, :hint => "Inside government slugs are: <code>government/the-slug</code>. Detailed guides are simply: <code>the-slug</code>".html_safe %>
+ <%= link_to "View on site", published_url(f.object), :rel => 'external', :class => "btn btn-primary" unless f.object.new_record? %>
+
+ <hr>
+
+ <input type="hidden" name="artefact[sections][]" value="" />
+ <% if artefact.new_record? %>
+ <%= f.input :kind, :collection => Artefact::FORMATS_BY_DEFAULT_OWNING_APP["whitehall"].map { |s| [s.humanize, s]}, :as => :select, :class => "span6", :prompt => "Select a kind" %>
+ <% end %>
+
+ <label for="artefact[sections][]">Sections</label>
+ <p>The first section will be the primary section the content lives in. This will form the content breadcrumb.</p>
+ <% if artefact.section_ids.length > 0 %>
+ <% artefact.section_ids.each do | section_id | %>
+ <%= render :partial => 'artefact_section', :locals => {:section_id => section_id, :tag_collection => tag_collection} %>
+ <% end %>
+ <% else %>
+ <%= render :partial => 'artefact_section', :locals => {:section_id => nil, :tag_collection => tag_collection} %>
+ <% end %>
+
+ <button id="add-section" class="btn btn-success" type="button">Add another section</button>
+ <% end %>
+
+ <hr>
+
+ <%= f.submit :value => "Save and continue editing", :class => "btn btn-success" %>
+ <% end %>
+ </div>
+<% end %>
+
+<%= content_for :extra_javascript do %>
+ <script type="text/javascript">
+ if ($('.artefact-section').size() == 1) {
+ $('.remove-section').hide();
+ }
+ </script>
+<% end %>
View
6 app/views/artefacts/edit.html.erb
@@ -12,7 +12,11 @@
</ul>
<div class="tab-content">
<div class="tab-pane active" id="edit">
- <%= render 'form', artefact: @artefact, tag_collection: @tag_collection %>
+ <% if "whitehall" == @artefact.owning_app %>
+ <%= render 'whitehall_form', artefact: @artefact, tag_collection: @tag_collection %>
+ <% else %>
+ <%= render 'form', artefact: @artefact, tag_collection: @tag_collection %>
+ <% end %>
</div>
<div class="tab-pane" id="history">
<%= render 'history', actions: @actions %>
View
4 app/views/artefacts/index.html.erb
@@ -38,8 +38,8 @@
<tr class="<%= artefact.state %>">
<td><%= artefact.need_id %></td>
<td><%= link_to artefact.name, edit_artefact_path(artefact.id) %></td>
- <td class="icon">
- <%= image_tag "icon-#{artefact.kind.downcase}.png", :title => artefact.kind.humanize %>
+ <td>
+ <small><%= artefact.kind.humanize %></small>
<%= content_tag "span", artefact.language, :class => "language_icon lang_#{artefact.language}" %>
</td>
<td><%= artefact.primary_section %></td>
View
10 app/views/artefacts/new.html.erb
@@ -1,5 +1,9 @@
-<%= content_for :page_title, "New artefact" %>
+<%= content_for :page_title, "New #{@artefact.owning_app.nil? ? "" : @artefact.owning_app.capitalize} artefact" %>
<div class="page-header">
- <h1>Creating new artefact</h1>
+ <h1><%= yield :page_title %></h1>
</div>
-<%= render 'form', artefact: @artefact, tag_collection: @tag_collection %>
+<% if "whitehall" == @artefact.owning_app %>
+ <%= render 'whitehall_form', artefact: @artefact, tag_collection: @tag_collection %>
+<% else %>
+ <%= render 'form', artefact: @artefact, tag_collection: @tag_collection %>
+<% end %>
View
1  app/views/layouts/application.html.erb
@@ -23,6 +23,7 @@
<ul class="nav">
<%= nav_link 'All artefacts', artefacts_path %>
<%= nav_link 'Add artefact', new_artefact_path %>
+ <%= nav_link 'Add Whitehall artefact', new_artefact_path(owning_app: "whitehall") %>
</ul>
<ul class="nav pull-right">
<li class="dropdown">
View
72 test/functional/artefacts_controller_test.rb
@@ -30,6 +30,62 @@ class ArtefactsControllerTest < ActionController::TestCase
end
end
+ context "GET new" do
+
+ should "render only the non-whitehall link as active" do
+ get :new
+ assert_select "li[class~=active] a[href=/artefacts/new]"
+ assert_select "li[class~=active] a[href=/artefacts/new?owning_app=whitehall]", false
+ end
+
+ context "whitehall is the owning_app" do
+ should "render the whitehall variant of the form" do
+ get :new, owning_app: "whitehall"
+ assert_template :whitehall_form
+ end
+
+ should "display only the whitehall link as active" do
+ get :new, owning_app: "whitehall"
+ assert_select "li[class~=active] a[href=/artefacts/new]", false
+ assert_select "li[class~=active] a[href=/artefacts/new?owning_app=whitehall]"
+ end
+ end
+ end
+
+ context "POST create" do
+ context "invalid artefact" do
+ should "rerender the form" do
+ post :create, :slug => 'not/valid', :owning_app => 'smart-answers', :kind => 'smart-answer', :name => 'Whatever', :need_id => 1
+ end
+ end
+
+ should "redirect to GET edit" do
+ Artefact.any_instance.stubs(:id).returns("abc")
+ post :create, :owning_app => 'smart-answers', :slug => 'whatever', :kind => 'smart-answer', :name => 'Whatever', :need_id => 1
+
+ assert_redirected_to "/artefacts/abc/edit"
+ end
+
+ context "publisher artefact" do
+ should "redirect to publisher" do
+ Artefact.any_instance.stubs(:id).returns("abc")
+ post :create, :owning_app => 'publisher', :slug => 'whatever', :kind => 'guide', :name => 'Whatever', :need_id => 1
+
+ assert_redirected_to Plek.current.find('publisher') + "/admin/publications/abc"
+ end
+ end
+ end
+
+ context "GET edit" do
+ context "whitehall is the owning_app" do
+ should "render the whitehall variant of the form" do
+ artefact = FactoryGirl.create(:artefact, owning_app: "whitehall")
+ get :edit, id: artefact.id
+ assert_template :whitehall_form
+ end
+ end
+ end
+
context "PUT update" do
context "invalid artefact" do
should "be invalid with an empty title" do
@@ -39,6 +95,22 @@ class ArtefactsControllerTest < ActionController::TestCase
assert_template :edit
end
end
+
+ should "redirect to GET edit" do
+ artefact = FactoryGirl.create(:artefact, owning_app: "smart-answers", kind: "smart-answer")
+ put :update, :id => artefact.id, :owning_app => 'smart-answers', :slug => 'whatever', :kind => 'smart-answer', :name => 'Whatever', :need_id => 1
+
+ assert_redirected_to "/artefacts/#{artefact.id}/edit"
+ end
+
+ context "publisher is owning_app" do
+ should "redirect to GET show (which then redirects to publisher)" do
+ artefact = FactoryGirl.create(:artefact)
+ put :update, :id => artefact.id, :owning_app => 'publisher', :slug => 'whatever', :kind => 'guide', :name => 'Whatever', :need_id => 1
+
+ assert_redirected_to "/artefacts/#{artefact.id}"
+ end
+ end
end
end
View
9 test/unit/rummageable_artefact_test.rb
@@ -132,4 +132,13 @@ class RummageableArtefactTest < ActiveSupport::TestCase
refute RummageableArtefact.new(artefact).should_be_indexed?
end
+
+ test "should not index content formats managed by Whitehall" do
+ artefact = Artefact.new do |artefact|
+ artefact.state = "live"
+ artefact.kind = Artefact::FORMATS_BY_DEFAULT_OWNING_APP["whitehall"].first
+ end
+
+ refute RummageableArtefact.new(artefact).should_be_indexed?
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.