diff options
17 files changed, 749 insertions, 4 deletions
diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_category_controller.rb new file mode 100644 index 000000000..635e664ee --- /dev/null +++ b/app/controllers/admin_public_body_category_controller.rb @@ -0,0 +1,55 @@ +class AdminPublicBodyCategoryController < AdminController + def index + @locale = self.locale_from_params + @category_headings = PublicBodyHeading.all + end + + def new + @category = PublicBodyCategory.new + render :formats => [:html] + end + + def edit + @category = PublicBodyCategory.find(params[:id]) + @tagged_public_bodies = PublicBody.find_by_tag(@category.category_tag) + end + + def update + I18n.with_locale(I18n.default_locale) do + @category = PublicBodyCategory.find(params[:id]) + if @category.update_attributes(params[:public_body_category]) + flash[:notice] = 'Category was successfully updated.' + end + render :action => 'edit' + end + end + + def create + I18n.with_locale(I18n.default_locale) do + @category = PublicBodyCategory.new(params[:public_body_category]) + if @category.save + flash[:notice] = 'Category was successfully created.' + redirect_to admin_category_index_url + else + render :action => 'new' + end + end + end + + def destroy + @locale = self.locale_from_params + I18n.with_locale(@locale) do + category = PublicBodyCategory.find(params[:id]) + + if PublicBody.find_by_tag(category.category_tag).count > 0 + flash[:notice] = "There are authorities associated with this category, so can't destroy it" + redirect_to admin_category_edit_url(category) + return + end + + category.destroy + flash[:notice] = "Category was successfully destroyed." + redirect_to admin_category_index_url + end + end +end diff --git a/app/controllers/admin_public_body_heading_controller.rb b/app/controllers/admin_public_body_heading_controller.rb new file mode 100644 index 000000000..43d8e329c --- /dev/null +++ b/app/controllers/admin_public_body_heading_controller.rb @@ -0,0 +1,54 @@ +class AdminPublicBodyHeadingController < AdminController + def index + redirect_to admin_category_index_url + end + + def edit + @heading = PublicBodyHeading.find(params[:id]) + render :formats => [:html] + end + + def update + I18n.with_locale(I18n.default_locale) do + @heading = PublicBodyHeading.find(params[:id]) + if @heading.update_attributes(params[:public_body_heading]) + flash[:notice] = 'Category heading was successfully updated.' + end + render :action => 'edit' + end + end + + def new + @heading = PublicBodyHeading.new + render :formats => [:html] + end + + def create + I18n.with_locale(I18n.default_locale) do + @heading = PublicBodyHeading.new(params[:public_body_heading]) + if @heading.save + flash[:notice] = 'Category heading was successfully created.' + redirect_to admin_category_index_url + else + render :action => 'new' + end + end + end + + def destroy + @locale = self.locale_from_params() + I18n.with_locale(@locale) do + heading = PublicBodyHeading.find(params[:id]) + + if heading.public_body_categories.count > 0 + flash[:notice] = "There are categories associated with this heading, so can't destroy it" + redirect_to admin_heading_edit_url(heading) + return + end + + heading.destroy + flash[:notice] = "Category heading was successfully destroyed." + redirect_to admin_category_index_url + end + end +end diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb index 1a88c9d97..c0d8e07c6 100644 --- a/app/models/public_body_category.rb +++ b/app/models/public_body_category.rb @@ -11,13 +11,15 @@ require 'forwardable' class PublicBodyCategory < ActiveRecord::Base - attr_accessible :locale, :category_tag, :title, :description + attr_accessible :locale, :category_tag, :title, :description, :translated_versions has_and_belongs_to_many :public_body_headings translates :title, :description - - validates_uniqueness_of :category_tag, :message => N_("Tag is already taken") + validates_uniqueness_of :category_tag, :message => N_('Tag is already taken') + validates_presence_of :title, :message => N_('Title can\'t be blank') + validates_presence_of :category_tag, :message => N_('Tag can\'t be blank') + validates_presence_of :description, :message => N_('Description can\'t be blank') def self.get locale = I18n.locale.to_s || default_locale.to_s || "" @@ -88,6 +90,37 @@ class PublicBodyCategory < ActiveRecord::Base end end + # Convenience methods for creating/editing translations via forms + def find_translation_by_locale(locale) + self.translations.find_by_locale(locale) + end + + def skip?(attrs) + valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values + return valueless.length == 0 + end + + def translated_versions + translations + end + + def translated_versions=(translation_attrs) + if translation_attrs.respond_to? :each_value # Hash => updating + translation_attrs.each_value do |attrs| + next if skip?(attrs) + t = translation_for(attrs[:locale]) || PublicBodyCategory::Translation.new + t.attributes = attrs + t.save! + end + else # Array => creating + translation_attrs.each do |attrs| + next if skip?(attrs) + new_translation = PublicBodyCategory::Translation.new(attrs) + translations << new_translation + end + end + end + private def self.load_categories() I18n.available_locales.each do |locale| diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb index 74f7a4374..6c7d645da 100644 --- a/app/models/public_body_heading.rb +++ b/app/models/public_body_heading.rb @@ -11,5 +11,37 @@ class PublicBodyHeading < ActiveRecord::Base translates :name - validates_uniqueness_of :name, :message => N_("Name is already taken") + validates_uniqueness_of :name, :message => N_('Name is already taken') + validates_presence_of :name, :message => N_('Name can\'t be blank') + + # Convenience methods for creating/editing translations via forms + def find_translation_by_locale(locale) + self.translations.find_by_locale(locale) + end + + def translated_versions + translations + end + + def translated_versions=(translation_attrs) + def skip?(attrs) + valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values + return valueless.length == 0 + end + + if translation_attrs.respond_to? :each_value # Hash => updating + translation_attrs.each_value do |attrs| + next if skip?(attrs) + t = translation_for(attrs[:locale]) || PublicBodyHeading::Translation.new + t.attributes = attrs + t.save! + end + else # Array => creating + translation_attrs.each do |attrs| + next if skip?(attrs) + new_translation = PublicBodyHeading::Translation.new(attrs) + translations << new_translation + end + end + end end diff --git a/app/views/admin_general/_admin_navbar.html.erb b/app/views/admin_general/_admin_navbar.html.erb index 5cc740f70..dcf55dc50 100644 --- a/app/views/admin_general/_admin_navbar.html.erb +++ b/app/views/admin_general/_admin_navbar.html.erb @@ -10,6 +10,7 @@ <li><%= link_to 'Stats', admin_stats_path %></li> <li><%= link_to 'Debug', admin_debug_path %></li> <li><%= link_to 'Authorities', admin_body_list_path %></li> + <li><%= link_to 'Categories', admin_category_index_path %></li> <li><%= link_to 'Requests', admin_request_list_path %></li> <li><%= link_to 'Users', admin_user_list_path %></li> <li><%= link_to 'Tracks', admin_track_list_path %></li> diff --git a/app/views/admin_public_body_category/_form.html.erb b/app/views/admin_public_body_category/_form.html.erb new file mode 100644 index 000000000..91f6285de --- /dev/null +++ b/app/views/admin_public_body_category/_form.html.erb @@ -0,0 +1,55 @@ +<%= error_messages_for 'category' %> + +<!--[form:public_body_category]--> + +<div id="div-locales"> + <ul class="locales nav nav-tabs"> + <% I18n.available_locales.each_with_index do |locale, i| %> + <li><a href="#div-locale-<%=locale.to_s%>" data-toggle="tab" ><%=locale_name(locale.to_s) || "Default locale"%></a></li> + <% end %> + </ul> + <div class="tab-content"> +<% + I18n.available_locales.each do |locale| + if locale==I18n.default_locale # The default locale is submitted as part of the bigger object... + prefix = 'public_body_category' + object = @category + else # ...but additional locales go "on the side" + prefix = "public_body_category[translated_versions][]" + object = @category.new_record? ? + PublicBodyCategory::Translation.new : + @category.find_translation_by_locale(locale.to_s) || PublicBodyCategory::Translation.new + end +%> + <%= fields_for prefix, object do |t| %> + <div class="tab-pane" id="div-locale-<%=locale.to_s%>"> + <div class="control-group"> + <%= t.hidden_field :locale, :value => locale.to_s %> + <label for="<%= form_tag_id(t.object_name, :title, locale) %>" class="control-label">Title</label> + <div class="controls"> + <%= t.text_field :title, :id => form_tag_id(t.object_name, :title, locale), :class => "span4" %> + </div> + </div> + <div class="control-group"> + <label for="<%= form_tag_id(t.object_name, :description, locale) %>" class="control-label">Description</label> + <div class="controls"> + <%= t.text_field :description, :id => form_tag_id(t.object_name, :description, locale), :class => "span4" %> + </div> + </div> + </div> + <% + end +end +%> + </div> +</div> + +<h3>Common Fields</h3> + +<div class="control-group"> + <label for="public_body_category_category_tag" class="control-label">Category tag</label> + <div class="controls"> + <%= f.text_field :category_tag, :class => "span4" %> + </div> +</div> +<!--[eoform:public_body_category]--> diff --git a/app/views/admin_public_body_category/_one_list.html.erb b/app/views/admin_public_body_category/_one_list.html.erb new file mode 100644 index 000000000..7303ff803 --- /dev/null +++ b/app/views/admin_public_body_category/_one_list.html.erb @@ -0,0 +1,43 @@ +<div class="accordion" id="categories"> + <% for heading in category_headings %> + <h3> + <%= link_to(heading.name, admin_heading_edit_path(heading), :title => "view full details")%> + </h3> + <div> + <% for category in heading.public_body_categories %> + <div class="accordion-group"> + <div class="accordion-heading accordion-toggle row"> + <span class="item-title span6"> + <a href="#category_<%=category.id%>" data-toggle="collapse" data-parent="requests"><%= chevron_right %></a> + + <%= link_to(category.title, admin_category_edit_path(category), :title => "view full details")%> + </span> + <span class="item-metadata span6"> + <span class="label label-info tag"> + <%= category.category_tag %> + </span> + </span> + </div> + <div id="category_<%=category.id%>" class="item-detail accordion-body collapse row"> + <div> + <span class="span6"> + <b>name</b> + </span> + <span class="span6"> + <%= h category.title %> + </span> + </div> + <div> + <span class="span6"> + <b>description</b> + </span> + <span class="span6"> + <%= h category.description %> + </span> + </div> + </div> + <% end %> + </div> + </div> + <% end %> +</div>
\ No newline at end of file diff --git a/app/views/admin_public_body_category/edit.html.erb b/app/views/admin_public_body_category/edit.html.erb new file mode 100644 index 000000000..c1e522cac --- /dev/null +++ b/app/views/admin_public_body_category/edit.html.erb @@ -0,0 +1,30 @@ +<h1><%=@title%></h1> + +<div class="row"> + <div class="span8"> + <div id="public_body_category_form"> + <%= form_for @category, :url => admin_category_update_path(@category), :html => { :class => "form form-horizontal" } do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> + <div class="form-actions"> + <%= f.submit 'Save', :accesskey => 's', :class => "btn btn-success" %></p> + </div> + <% end %> + </div> +</div> + +<div class="row"> + <div class="span8 well"> + <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + </div> +</div> + +<% if @tagged_public_bodies.empty? %> + <div class="row"> + <div class="span8"> + <%= form_tag(admin_category_destroy_path(@category), :class => "form form-inline") do %> + <%= hidden_field_tag(:public_body_id, { :value => @category.id } ) %> + <%= submit_tag "Destroy #{@category.title}", :title => @category.title, :class => "btn btn-danger" %> (this is permanent!) + <% end %> + </div> + </div> +<% end %>
\ No newline at end of file diff --git a/app/views/admin_public_body_category/index.html.erb b/app/views/admin_public_body_category/index.html.erb new file mode 100644 index 000000000..ca3a4b0ff --- /dev/null +++ b/app/views/admin_public_body_category/index.html.erb @@ -0,0 +1,16 @@ +<% @title = 'Listing public authority categories' %> + +<h1><%=@title%></h1> + +<div class="btn-toolbar"> + <div class="btn-group"> + <%= link_to 'New category', admin_category_new_path, :class => "btn btn-primary" %> + </div> + <div class="btn-group"> + <%= link_to 'New category heading', admin_heading_new_path, :class => "btn" %> + </div> +</div> + +<h2>All categories</h2> + +<%= render :partial => 'one_list', :locals => { :category_headings => @category_headings, :table_name => 'exact' } %> diff --git a/app/views/admin_public_body_category/new.html.erb b/app/views/admin_public_body_category/new.html.erb new file mode 100644 index 000000000..0a016182f --- /dev/null +++ b/app/views/admin_public_body_category/new.html.erb @@ -0,0 +1,21 @@ +<% @title = 'New category' %> + +<h1><%=@title%></h1> +<div class="row"> + <div class="span8"> + <div id="public_category_form"> + <%= form_for @category, :as => :public_body_category, :url => admin_category_create_path, :html => {:class => "form form-horizontal"} do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> + + <div class="form-actions"> + <%= f.submit "Create", :class => "btn btn-primary" %> + </div> + <% end %> + <div class="row"> + <div class="span8 well"> + <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + </div> + </div> + </div> + </div> +</div> diff --git a/app/views/admin_public_body_heading/_form.html.erb b/app/views/admin_public_body_heading/_form.html.erb new file mode 100644 index 000000000..d4e914ca1 --- /dev/null +++ b/app/views/admin_public_body_heading/_form.html.erb @@ -0,0 +1,41 @@ +<%= error_messages_for 'heading' %> + +<!--[form:public_body_heading]--> + +<div id="div-locales"> + <ul class="locales nav nav-tabs"> + <% I18n.available_locales.each_with_index do |locale, i| %> + <li><a href="#div-locale-<%=locale.to_s%>" data-toggle="tab" ><%=locale_name(locale.to_s) || "Default locale"%></a></li> + <% end %> + </ul> + <div class="tab-content"> +<% + for locale in I18n.available_locales do + if locale==I18n.default_locale # The default locale is submitted as part of the bigger object... + prefix = 'public_body_heading' + object = @heading + else # ...but additional locales go "on the side" + prefix = "public_body_heading[translated_versions][]" + object = @heading.new_record? ? + PublicBodyHeading::Translation.new : + @heading.find_translation_by_locale(locale.to_s) || PublicBodyHeading::Translation.new + end +%> + <%= fields_for prefix, object do |t| %> + <div class="tab-pane" id="div-locale-<%=locale.to_s%>"> + <div class="control-group"> + <%= t.hidden_field :locale, :value => locale.to_s %> + <label for="<%= form_tag_id(t.object_name, :name, locale) %>" class="control-label">Name</label> + <div class="controls"> + <%= t.text_field :name, :id => form_tag_id(t.object_name, :name, locale), :class => "span4" %> + </div> + </div> + </div> + <% + end +end +%> + </div> +</div> + +<!--[eoform:public_body_heading]--> diff --git a/app/views/admin_public_body_heading/edit.html.erb b/app/views/admin_public_body_heading/edit.html.erb new file mode 100644 index 000000000..df5ae7d0e --- /dev/null +++ b/app/views/admin_public_body_heading/edit.html.erb @@ -0,0 +1,30 @@ +<h1><%=@title%></h1> + +<div class="row"> + <div class="span8"> + <div id="public_body_heading_form"> + <%= form_for @heading, :url => admin_heading_update_path(@heading), :html => { :class => "form form-horizontal" } do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> + <div class="form-actions"> + <%= f.submit 'Save', :accesskey => 's', :class => "btn btn-success" %></p> + </div> + <% end %> + </div> +</div> + +<div class="row"> + <div class="span8 well"> + <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + </div> +</div> + +<% if @heading.public_body_categories.empty? %> + <div class="row"> + <div class="span8"> + <%= form_tag(admin_heading_destroy_path(@heading), :class => "form form-inline") do %> + <%= hidden_field_tag(:public_body_heading_id, { :value => @heading.id } ) %> + <%= submit_tag "Destroy #{@heading.name}", :name => @heading.name, :class => "btn btn-danger" %> (this is permanent!) + <% end %> + </div> + </div> +<% end %>
\ No newline at end of file diff --git a/app/views/admin_public_body_heading/new.html.erb b/app/views/admin_public_body_heading/new.html.erb new file mode 100644 index 000000000..22cb88901 --- /dev/null +++ b/app/views/admin_public_body_heading/new.html.erb @@ -0,0 +1,21 @@ +<% @title = 'New category heading' %> + +<h1><%=@title%></h1> +<div class="row"> + <div class="span8"> + <div id="public_heading_form"> + <%= form_for @heading, :as => :public_body_heading, :url => admin_heading_create_path, :html => {:class => "form form-horizontal"} do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> + + <div class="form-actions"> + <%= f.submit "Create", :class => "btn btn-primary" %> + </div> + <% end %> + <div class="row"> + <div class="span8 well"> + <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + </div> + </div> + </div> + </div> +</div> diff --git a/config/routes.rb b/config/routes.rb index 9f426fabf..a97631979 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -178,6 +178,24 @@ Alaveteli::Application.routes.draw do match '/admin/body/mass_tag_add' => 'admin_public_body#mass_tag_add', :as => :admin_body_mass_tag_add #### + #### AdminPublicBodyCategory controller + match '/admin/category' => 'admin_public_body_category#index', :as => :admin_category_index + match '/admin/category/new' => 'admin_public_body_category#new', :as => :admin_category_new + match '/admin/category/edit/:id' => 'admin_public_body_category#edit', :as => :admin_category_edit + match '/admin/category/update/:id' => 'admin_public_body_category#update', :as => :admin_category_update + match '/admin/category/create' => 'admin_public_body_category#create', :as => :admin_category_create + match '/admin/category/destroy/:id' => 'admin_public_body_category#destroy', :as => :admin_category_destroy + #### + + #### AdminPublicBodyHeading controller + match '/admin/category_heading' => 'admin_public_body_heading#index' + match '/admin/category_heading/new' => 'admin_public_body_heading#new', :as => :admin_heading_new + match '/admin/category_heading/edit/:id' => 'admin_public_body_heading#edit', :as => :admin_heading_edit + match '/admin/category_heading/update/:id' => 'admin_public_body_heading#update', :as => :admin_heading_update + match '/admin/category_heading/create' => 'admin_public_body_heading#create', :as => :admin_heading_create + match '/admin/category_heading/destroy/:id' => 'admin_public_body_heading#destroy', :as => :admin_heading_destroy + #### + #### AdminPublicBodyChangeRequest controller match '/admin/change_request/edit/:id' => 'admin_public_body_change_requests#edit', :as => :admin_change_request_edit match '/admin/change_request/update/:id' => 'admin_public_body_change_requests#update', :as => :admin_change_request_update diff --git a/lib/public_body_categories_es.rb b/lib/public_body_categories_es.rb new file mode 100644 index 000000000..4559a5708 --- /dev/null +++ b/lib/public_body_categories_es.rb @@ -0,0 +1,20 @@ +# The PublicBodyCategories structure works like this: +# [ +# "Main category name", +# [ "tag_to_use_as_category", "Sub category title", "sentence that can describes things in this subcategory" ], +# [ "another_tag", "Second sub category title", "another descriptive sentence for things in this subcategory"], +# "Another main category name", +# [ "another_tag_2", "Another sub category title", "another descriptive sentence"] +# ]) +# +# DO NOT EDIT THIS FILE! It should be overridden in a custom theme. +# See doc/THEMES.md for more info + +PublicBodyCategories.add(:es, [ + "Silly ministries", + [ "useless_agency", "Los useless ministries", "el useless ministry" ], + [ "lonely_agency", "Los lonely agencies", "el lonely agency"], + "Popular agencies", + [ "popular_agency", "Los popular agencies", "el lonely agency"], + [ "spanish_agency", "Los random example", "el random example"] +]) diff --git a/spec/controllers/admin_public_body_category_controller_spec.rb b/spec/controllers/admin_public_body_category_controller_spec.rb new file mode 100644 index 000000000..d65a85d9e --- /dev/null +++ b/spec/controllers/admin_public_body_category_controller_spec.rb @@ -0,0 +1,143 @@ +require 'spec_helper' + +describe AdminPublicBodyCategoryController do + context 'when showing the index of categories and headings' do + render_views + + it 'shows the index page' do + get :index + end + end + + context 'when showing the form for a new public body category' do + it 'should assign a new public body category to the view' do + get :new + assigns[:category].should be_a(PublicBodyCategory) + end + end + + context 'when creating a public body category' do + it "creates a new public body category in one locale" do + n = PublicBodyCategory.count + post :create, { + :public_body_category => { + :title => 'New Category', + :category_tag => 'new_test_category', + :description => 'New category for testing stuff' + } + } + PublicBodyCategory.count.should == n + 1 + + category = PublicBodyCategory.find_by_title("New Category") + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + end + + it 'creates a new public body category with multiple locales' do + n = PublicBodyCategory.count + post :create, { + :public_body_category => { + :title => 'New Category', + :category_tag => 'new_test_category', + :description => 'New category for testing stuff', + :translated_versions => [{ :locale => "es", + :title => "Mi Nuevo Category" }] + } + } + PublicBodyCategory.count.should == n + 1 + + category = PublicBodyCategory.find_by_title("New Category") + category.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] + I18n.with_locale(:en) do + category.title.should == "New Category" + end + I18n.with_locale(:es) do + category.title.should == "Mi Nuevo Category" + end + + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + end + end + + context 'when editing a public body category' do + before do + PublicBodyCategory.load_categories + @category = PublicBodyCategory.find_by_title("Useless ministries") + end + + render_views + + it "edits a public body category" do + get :edit, :id => @category.id + end + + it "edits a public body in another locale" do + get :edit, {:id => @category.id, :locale => :en} + + # When editing a body, the controller returns all available translations + assigns[:category].find_translation_by_locale("es").title.should == 'Los useless ministries' + response.should render_template('edit') + end + end + + context 'when updating a public body category' do + before do + PublicBodyCategory.load_categories + @category = PublicBodyCategory.find_by_title("Useless ministries") + end + + it "saves edits to a public body category" do + post :update, { :id => @category.id, + :public_body_category => { :title => "Renamed" } } + request.flash[:notice].should include('successful') + pbc = PublicBodyCategory.find(@category.id) + pbc.title.should == "Renamed" + end + + it "saves edits to a public body category in another locale" do + I18n.with_locale(:es) do + @category.title.should == 'Los useless ministries' + post :update, { + :id => @category.id, + :public_body_category => { + :title => "Useless ministries", + :translated_versions => { + @category.id => {:locale => "es", + :title => "Renamed"} + } + } + } + request.flash[:notice].should include('successful') + end + + pbc = PublicBodyCategory.find(@category.id) + I18n.with_locale(:es) do + pbc.title.should == "Renamed" + end + I18n.with_locale(:en) do + pbc.title.should == "Useless ministries" + end + end + end + + context 'when destroying a public body category' do + before do + PublicBodyCategory.load_categories + end + + it "does not destroy a public body category that has associated bodies" do + category = PublicBodyCategory.find_by_title("Useless ministries") + n = PublicBodyCategory.count + post :destroy, { :id => category.id } + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'edit', :id => category.id) + PublicBodyCategory.count.should == n + end + + it "destroys an empty public body category" do + pbc = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-") + n = PublicBodyCategory.count + post :destroy, { :id => pbc.id } + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + PublicBodyCategory.count.should == n - 1 + end + end +end diff --git a/spec/controllers/admin_public_body_heading_controller_spec.rb b/spec/controllers/admin_public_body_heading_controller_spec.rb new file mode 100644 index 000000000..3d0850322 --- /dev/null +++ b/spec/controllers/admin_public_body_heading_controller_spec.rb @@ -0,0 +1,132 @@ +require 'spec_helper' + +describe AdminPublicBodyHeadingController do + context 'when showing the index of categories and headings' do + render_views + + it 'redirect to the category list page from the index' do + get :index + response.should redirect_to :admin_category_index + end + end + + context 'when showing the form for a new public body category' do + it 'should assign a new public body heading to the view' do + get :new + assigns[:heading].should be_a(PublicBodyHeading) + end + end + + context 'when creating a public body heading' do + it "creates a new public body heading in one locale" do + n = PublicBodyHeading.count + post :create, { + :public_body_heading => { + :name => 'New Heading' + } + } + PublicBodyHeading.count.should == n + 1 + + heading = PublicBodyHeading.find_by_name("New Heading") + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + end + + it 'creates a new public body heading with multiple locales' do + n = PublicBodyHeading.count + post :create, { + :public_body_heading => { + :name => 'New Heading', + :translated_versions => [{ :locale => "es", + :name => "Mi Nuevo Heading" }] + } + } + PublicBodyHeading.count.should == n + 1 + + heading = PublicBodyHeading.find_by_name("New Heading") + heading.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] + I18n.with_locale(:en) do + heading.name.should == "New Heading" + end + I18n.with_locale(:es) do + heading.name.should == "Mi Nuevo Heading" + end + + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + end + end + + context 'when editing a public body heading' do + before do + PublicBodyCategory.load_categories + @heading= PublicBodyHeading.find_by_name("Silly ministries") + end + + render_views + + it "edits a public body heading" do + get :edit, :id => @heading.id + end + end + + context 'when updating a public body heading' do + before do + PublicBodyCategory.load_categories + @heading = PublicBodyHeading.find_by_name("Silly ministries") + end + + it "saves edits to a public body heading" do + post :update, { :id => @heading.id, + :public_body_heading => { :name => "Renamed" } } + request.flash[:notice].should include('successful') + found_heading = PublicBodyHeading.find(@heading.id) + found_heading.name.should == "Renamed" + end + + it "saves edits to a public body heading in another locale" do + I18n.with_locale(:es) do + @heading.name.should == 'Silly ministries' + post :update, { + :id => @heading.id, + :public_body_heading => { + :name => "Silly ministries", + :translated_versions => { + @heading.id => {:locale => "es", + :name => "Renamed"} + } + } + } + request.flash[:notice].should include('successful') + end + + heading = PublicBodyHeading.find(@heading.id) + I18n.with_locale(:es) do + heading.name.should == "Renamed" + end + I18n.with_locale(:en) do + heading.name.should == "Silly ministries" + end + end + end + + context 'when destroying a public body heading' do + before do + PublicBodyCategory.load_categories + end + + it "does not destroy a public body heading that has associated categories" do + heading = PublicBodyHeading.find_by_name("Silly ministries") + n = PublicBodyHeading.count + post :destroy, { :id => heading.id } + response.should redirect_to(:controller=>'admin_public_body_heading', :action=>'edit', :id => heading.id) + PublicBodyHeading.count.should == n + end + + it "destroys an empty public body heading" do + heading = PublicBodyHeading.create(:name => "Empty Heading") + n = PublicBodyHeading.count + post :destroy, { :id => heading.id } + response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + PublicBodyHeading.count.should == n - 1 + end + end +end |