aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/admin_public_body_categories_controller.rb44
-rw-r--r--app/models/public_body_category.rb60
-rw-r--r--app/views/admin_public_body_categories/_form.html.erb75
-rw-r--r--app/views/admin_public_body_categories/_locale_fields.html.erb15
-rw-r--r--spec/controllers/admin_public_body_categories_controller_spec.rb700
-rw-r--r--spec/integration/admin_public_body_category_edit_spec.rb59
-rw-r--r--spec/models/public_body_category_spec.rb140
7 files changed, 886 insertions, 207 deletions
diff --git a/app/controllers/admin_public_body_categories_controller.rb b/app/controllers/admin_public_body_categories_controller.rb
index 5e305dde3..a86171c76 100644
--- a/app/controllers/admin_public_body_categories_controller.rb
+++ b/app/controllers/admin_public_body_categories_controller.rb
@@ -7,17 +7,39 @@ class AdminPublicBodyCategoriesController < AdminController
def new
@category = PublicBodyCategory.new
- render :formats => [:html]
+ @category.build_all_translations
+ end
+
+ def create
+ I18n.with_locale(I18n.default_locale) do
+ @category = PublicBodyCategory.new(params[:public_body_category])
+ if @category.save
+ # FIXME: This can't handle failure (e.g. if a PublicBodyHeading
+ # doesn't exist)
+ if params[:headings]
+ params[:headings].values.each do |heading_id|
+ PublicBodyHeading.find(heading_id).add_category(@category)
+ end
+ end
+ flash[:notice] = 'Category was successfully created.'
+ redirect_to admin_categories_path
+ else
+ @category.build_all_translations
+ render :action => 'new'
+ end
+ end
end
def edit
@category = PublicBodyCategory.find(params[:id])
+ @category.build_all_translations
@tagged_public_bodies = PublicBody.find_by_tag(@category.category_tag)
end
def update
@category = PublicBodyCategory.find(params[:id])
@tagged_public_bodies = PublicBody.find_by_tag(@category.category_tag)
+
heading_ids = []
I18n.with_locale(I18n.default_locale) do
@@ -43,6 +65,8 @@ class AdminPublicBodyCategoriesController < AdminController
end
added_headings.each do |heading_id|
+ # FIXME: This can't handle failure (e.g. if a
+ # PublicBodyHeading doesn't exist)
PublicBodyHeading.find(heading_id).add_category(@category)
end
end
@@ -51,29 +75,13 @@ class AdminPublicBodyCategoriesController < AdminController
flash[:notice] = 'Category was successfully updated.'
redirect_to edit_admin_category_path(@category)
else
+ @category.build_all_translations
render :action => 'edit'
end
end
end
end
- def create
- I18n.with_locale(I18n.default_locale) do
- @category = PublicBodyCategory.new(params[:public_body_category])
- if @category.save
- if params[:headings]
- params[:headings].values.each do |heading_id|
- PublicBodyHeading.find(heading_id).add_category(@category)
- end
- end
- flash[:notice] = 'Category was successfully created.'
- redirect_to admin_categories_path
- else
- render :action => 'new'
- end
- end
- end
-
def destroy
@locale = self.locale_from_params
I18n.with_locale(@locale) do
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
index 198e8b737..eeb2511fa 100644
--- a/app/models/public_body_category.rb
+++ b/app/models/public_body_category.rb
@@ -10,12 +10,15 @@ require 'forwardable'
class PublicBodyCategory < ActiveRecord::Base
attr_accessible :locale, :category_tag, :title, :description,
- :translated_versions, :display_order
+ :translated_versions, :translations_attributes,
+ :display_order
has_many :public_body_category_links, :dependent => :destroy
has_many :public_body_headings, :through => :public_body_category_links
translates :title, :description
+ accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?
+
validates_uniqueness_of :category_tag, :message => 'Tag is already taken'
validates_presence_of :title, :message => "Title can't be blank"
validates_presence_of :category_tag, :message => "Tag can't be blank"
@@ -59,25 +62,48 @@ class PublicBodyCategory < ActiveRecord::Base
end
def translated_versions=(translation_attrs)
- def empty_translation?(attrs)
- attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' }
- attrs_with_values.empty?
+ warn "[DEPRECATION] PublicBodyCategory#translated_versions= will be replaced " \
+ "by PublicBodyCategory#translations_attributes= as of release 0.22"
+ self.translations_attributes = translation_attrs
+ end
+
+ def ordered_translations
+ translations.
+ select { |t| I18n.available_locales.include?(t.locale) }.
+ sort_by { |t| I18n.available_locales.index(t.locale) }
+ end
+
+ def build_all_translations
+ I18n.available_locales.each do |locale|
+ translations.build(:locale => locale) unless translations.detect{ |t| t.locale == locale }
end
- if translation_attrs.respond_to? :each_value # Hash => updating
- translation_attrs.each_value do |attrs|
- next if empty_translation?(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 empty_translation?(attrs)
- new_translation = PublicBodyCategory::Translation.new(attrs)
- translations << new_translation
- end
+ end
+
+ private
+
+ def empty_translation_in_params?(attributes)
+ attrs_with_values = attributes.select do |key, value|
+ value != '' and key.to_s != 'locale'
end
+ attrs_with_values.empty?
end
+
end
+PublicBodyCategory::Translation.class_eval do
+ with_options :if => lambda { |t| !t.default_locale? && t.required_attribute_submitted? } do |required|
+ required.validates :title, :presence => { :message => _("Title can't be blank") }
+ required.validates :description, :presence => { :message => _("Description can't be blank") }
+ end
+
+ def default_locale?
+ locale == I18n.default_locale
+ end
+ def required_attribute_submitted?
+ PublicBodyCategory.required_translated_attributes.compact.any? do |attribute|
+ !read_attribute(attribute).blank?
+ end
+ end
+
+end
diff --git a/app/views/admin_public_body_categories/_form.html.erb b/app/views/admin_public_body_categories/_form.html.erb
index 1f033ac9b..00137b9ed 100644
--- a/app/views/admin_public_body_categories/_form.html.erb
+++ b/app/views/admin_public_body_categories/_form.html.erb
@@ -1,46 +1,49 @@
-<%= error_messages_for 'category' %>
+<% if @category.errors.any? %>
+ <ul>
+ <% @category.errors.each do |attr, message| %>
+ <% unless attr.to_s.starts_with?('translation') %>
+ <li><%= message %></li>
+ <% end %>
+ <% end %>
+ </ul>
+<% end %>
+
+<% @category.ordered_translations.each do |translation| %>
+ <% if translation.errors.any? %>
+ <%= locale_name(translation.locale.to_s) || translation.locale.to_s %>
+ <ul>
+ <% translation.errors.each do |attr, message| %>
+ <li><%= message %></li>
+ <% end %>
+ </ul>
+ <% end %>
+<% end %>
<!--[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 %>
+ <% @category.ordered_translations.each do |translation| %>
+ <li>
+ <a href="#div-locale-<%= translation.locale.to_s %>" data-toggle="tab" >
+ <%= locale_name(translation.locale.to_s) || translation.locale.to_s %>
+ </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
-%>
+ <% @category.ordered_translations.each do |translation| %>
+ <% if translation.locale.to_s == I18n.default_locale.to_s %>
+ <%= fields_for('public_body_category', @category) do |t| %>
+ <%= render :partial => 'locale_fields', :locals => { :t => t, :locale => translation.locale } %>
+ <% end %>
+ <% else %>
+ <%= f.fields_for(:translations, translation, :child_index => translation.locale) do |t| %>
+ <%= render :partial => 'locale_fields', :locals => { :t => t, :locale => translation.locale } %>
+ <% end %>
+ <% end %>
+ <% end %>
</div>
</div>
diff --git a/app/views/admin_public_body_categories/_locale_fields.html.erb b/app/views/admin_public_body_categories/_locale_fields.html.erb
new file mode 100644
index 000000000..0d9a2f207
--- /dev/null
+++ b/app/views/admin_public_body_categories/_locale_fields.html.erb
@@ -0,0 +1,15 @@
+<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>
diff --git a/spec/controllers/admin_public_body_categories_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb
index 4c641bd75..1131b3c0b 100644
--- a/spec/controllers/admin_public_body_categories_controller_spec.rb
+++ b/spec/controllers/admin_public_body_categories_controller_spec.rb
@@ -1,120 +1,268 @@
require 'spec_helper'
describe AdminPublicBodyCategoriesController do
- context 'when showing the index of categories and headings' do
- render_views
- it 'shows the index page' do
+ describe :index do
+
+ it 'responds successfully' do
get :index
expect(response).to be_success
end
+
+ it 'uses the current locale by default' do
+ get :index
+ expect(assigns(:locale)).to eq(I18n.locale.to_s)
+ end
+
+ it 'sets the locale if the show_locale param is passed' do
+ get :index, :show_locale => 'es'
+ expect(assigns(:locale)).to eq('es')
+ end
+
+ it 'finds all category headings' do
+ PublicBodyHeading.destroy_all
+
+ headings = [FactoryGirl.create(:public_body_heading),
+ FactoryGirl.create(:public_body_heading)]
+
+ get :index
+
+ expect(assigns(:category_headings)).to eq(headings)
+ end
+
+ it 'finds all categories without their headings' do
+ PublicBodyHeading.destroy_all
+ PublicBodyCategory.destroy_all
+
+ without_heading = FactoryGirl.create(:public_body_category)
+
+ heading = FactoryGirl.create(:public_body_heading)
+ with_heading = FactoryGirl.create(:public_body_category)
+ PublicBodyCategoryLink.create!(:public_body_heading_id => heading.id,
+ :public_body_category_id => with_heading.id)
+
+
+ get :index
+ expect(assigns(:without_heading)).to eq([without_heading])
+ end
+
+ it 'renders the index template' do
+ get :index
+ expect(response).to render_template('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
+ describe :new do
+
+ it 'responds successfully' do
get :new
- assigns[:category].should be_a(PublicBodyCategory)
+ expect(response).to be_success
end
- it 'renders the new template' do
+ it 'builds a new PublicBodyCategory' do
get :new
- expect(response).to render_template('new')
+ expect(assigns(:category)).to be_new_record
end
+ it 'builds new translations for all locales' do
+ get :new
+
+ translations = assigns(:category).translations.map{ |t| t.locale.to_s }.sort
+ available = I18n.available_locales.map{ |l| l.to_s }.sort
+
+ expect(translations).to eq(available)
+ end
+
+ it 'renders the new template' do
+ get :new
+ expect(response).to render_template('new')
+ 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
+ describe :create do
+
+ context 'on success' do
+
+ before(:each) do
+ PublicBodyCategory.destroy_all
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :title => 'New Category',
+ :description => 'New category for testing stuff' }
+ } }
+ end
+
+ it 'creates a new category in the default locale' do
+ PublicBodyCategory.destroy_all
+
+ expect {
+ post :create, :public_body_category => @params
+ }.to change{ PublicBodyCategory.count }.from(0).to(1)
+ end
+
+ it "saves the public body category's heading associations" do
+ heading = FactoryGirl.create(:public_body_heading)
+ params = FactoryGirl.attributes_for(:public_body_category)
+
+ post :create, :public_body_category => @params,
+ :headings => { "heading_#{ heading.id }" => heading.id }
+
+ category = PublicBodyCategory.find_by_title(@params[:translations_attributes]['en'][:title])
+ expect(category.public_body_headings).to eq([heading])
+ end
+
+ it 'notifies the admin that the category was created' do
+ post :create, :public_body_category => @params
+ expect(flash[:notice]).to eq('Category was successfully created.')
+ end
+
+ it 'redirects to the categories index' do
+ post :create, :public_body_category => @params
+ expect(response).to redirect_to(admin_categories_path)
+ end
- category = PublicBodyCategory.find_by_title("New Category")
- response.should redirect_to(admin_categories_path)
end
- it "saves the public body category's heading associations" do
- heading = FactoryGirl.create(:public_body_heading)
- category_attributes = FactoryGirl.attributes_for(:public_body_category)
- post :create, {
- :public_body_category => category_attributes,
- :headings => {"heading_#{heading.id}" => heading.id}
- }
- request.flash[:notice].should include('successful')
- category = PublicBodyCategory.find_by_title(category_attributes[:title])
- category.public_body_headings.should == [heading]
+ context 'on success for multiple locales' do
+
+ before(:each) do
+ PublicBodyCategory.destroy_all
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :title => 'New Category',
+ :description => 'New category for testing stuff' },
+ 'es' => { :locale => 'es',
+ :title => 'Mi Nuevo Category',
+ :description => 'ES Description' }
+ } }
+ end
+
+ it 'saves the category' do
+ expect {
+ post :create, :public_body_category => @params
+ }.to change{ PublicBodyCategory.count }.from(0).to(1)
+ end
+
+ it 'saves the default locale translation' do
+ post :create, :public_body_category => @params
+
+ category = PublicBodyCategory.find_by_title('New Category')
+
+ I18n.with_locale(:en) do
+ expect(category.title).to eq('New Category')
+ end
+ end
+
+ it 'saves the alternative locale translation' do
+ post :create, :public_body_category => @params
+
+ category = PublicBodyCategory.find_by_title('New Category')
+
+ I18n.with_locale(:es) do
+ expect(category.title).to eq('Mi Nuevo Category')
+ end
+ end
+
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
+ context 'on failure' do
- 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"
+ it 'renders the form if creating the record was unsuccessful' do
+ post :create, :public_body_category => { :title => '' }
+ expect(response).to render_template('new')
end
- I18n.with_locale(:es) do
- category.title.should == "Mi Nuevo Category"
+
+ it 'is rebuilt with the given params' do
+ post :create, :public_body_category => { :title => 'Need a description' }
+ expect(assigns(:category).title).to eq('Need a description')
end
- response.should redirect_to(admin_categories_path)
end
- it "renders the form if creating the record was unsuccessful" do
- post :create, :public_body_category => { :title => '' }
- expect(response).to render_template('new')
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :title => 'Need a description',
+ :description => nil },
+ 'es' => { :locale => 'es',
+ :title => 'Mi Nuevo Category',
+ :description => 'ES Description' }
+ } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :create, :public_body_category => @params
+ expect(assigns(:category).title).to eq('Need a description')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :create, :public_body_category => @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:category).title).to eq('Mi Nuevo Category')
+ end
+ end
+
end
end
- context 'when editing a public body category' do
+ describe :edit do
+
before do
@category = FactoryGirl.create(:public_body_category)
I18n.with_locale('es') do
@category.title = 'Los category'
+ @category.description = 'ES Description'
@category.save!
end
end
- render_views
+ it 'responds successfully' do
+ get :edit, :id => @category.id
+ expect(response).to be_success
+ end
- it "finds the requested category" do
+ it 'finds the requested category' do
get :edit, :id => @category.id
expect(assigns[:category]).to eq(@category)
end
- it "renders the edit template" do
+ it 'builds new translations if the body does not already have a translation in the specified locale' do
get :edit, :id => @category.id
- expect(assigns[:category]).to render_template('edit')
+ expect(assigns[:category].translations.map(&:locale)).to include(:fr)
end
- it "edits a public body in another locale" do
- get :edit, { :id => @category.id, :locale => :en }
+ it 'finds the public bodies tagged with the category tag' do
+ # FIXME: I wanted to call PublicBody.destroy_all here so that we
+ # have a known DB state, but the constraints were preventing the
+ # deletion of the fixture data
+ FactoryGirl.create(:public_body, :tag_string => 'wont_be_found')
+
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec')
+ expected_bodies = [FactoryGirl.create(:public_body, :tag_string => 'spec'),
+ FactoryGirl.create(:public_body, :tag_string => 'spec')]
- # When editing a body, the controller returns all available
- # translations
- assigns[:category].find_translation_by_locale("es").title.should == 'Los category'
- response.should render_template('edit')
+ get :edit, :id => category.id
+
+ expect(assigns(:tagged_public_bodies).sort).to eq(expected_bodies.sort)
end
+
+ it 'renders the edit template' do
+ get :edit, :id => @category.id
+ expect(response).to render_template('edit')
+ end
+
end
- context 'when updating a public body category' do
+ describe :update do
before do
@heading = FactoryGirl.create(:public_body_heading)
@@ -126,109 +274,389 @@ describe AdminPublicBodyCategoriesController do
@tag = @category.category_tag
I18n.with_locale('es') do
@category.title = 'Los category'
+ @category.description = 'ES Description'
@category.save!
end
+
+ @params = { :category_tag => @category.category_tag,
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => 'es',
+ :title => @category.title(:es),
+ :description => @category.description(:es) }
+ } }
end
- render_views
+ it 'finds the category to update' do
+ post :update, :id => @category.id,
+ :public_body_category => @params
+ expect(assigns(:category)).to eq(@category)
+ end
+
+ it 'finds the public bodies tagged with the category tag' do
+ # FIXME: I wanted to call PublicBody.destroy_all here so that we
+ # have a known DB state, but the constraints were preventing the
+ # deletion of the fixture data
+ FactoryGirl.create(:public_body, :tag_string => 'wont_be_found')
+
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec')
+ expected_bodies = [FactoryGirl.create(:public_body, :tag_string => 'spec'),
+ FactoryGirl.create(:public_body, :tag_string => 'spec')]
+
+ post :update, :id => category.id,
+ :public_body_category => category.serializable_hash.except(:title, :description)
- 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"
+ expect(assigns(:tagged_public_bodies)).to eq(expected_bodies)
end
it "saves edits to a public body category's heading associations" do
- @category.public_body_headings.should == [@heading]
+ # We already have a heading from the before block. Here we're going
+ # to update to a new heading.
heading = FactoryGirl.create(:public_body_heading)
- post :update, { :id => @category.id,
- :public_body_category => { :title => "Renamed" },
- :headings => {"heading_#{heading.id}" => heading.id} }
- request.flash[:notice].should include('successful')
- pbc = PublicBodyCategory.find(@category.id)
- pbc.public_body_headings.should == [heading]
- end
-
- it "saves edits to a public body category in another locale" do
- I18n.with_locale(:es) do
- @category.title.should == 'Los category'
- post :update, {
- :id => @category.id,
- :public_body_category => {
- :title => "Category",
- :translated_versions => {
- @category.id => {:locale => "es",
- :title => "Renamed"}
+
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :title => 'Renamed' }
}
- }
- }
- request.flash[:notice].should include('successful')
+ },
+ :headings => { "heading_#{ heading.id }" => heading.id }
+
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.public_body_headings).to eq([heading])
+ end
+
+ context 'when the category has associated bodies' do
+
+ it 'does not save edits to category_tag' do
+ body = FactoryGirl.create(:public_body, :tag_string => @tag)
+
+ post :update, :id => @category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.category_tag).to eq(@tag)
end
- pbc = PublicBodyCategory.find(@category.id)
- I18n.with_locale(:es) do
- pbc.title.should == "Renamed"
+ it 'notifies the user that the category_tag could not be updated' do
+ body = FactoryGirl.create(:public_body, :tag_string => @tag)
+ msg = %Q(There are authorities associated with this category,
+ so the tag can't be renamed).squish
+
+ post :update, :id => @category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+ expect(flash[:error]).to eq(msg)
end
- I18n.with_locale(:en) do
- pbc.title.should == "Category"
+
+ it 'renders the edit action' do
+ body = FactoryGirl.create(:public_body, :tag_string => @tag)
+
+ post :update, :id => @category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+ expect(response).to render_template('edit')
end
+
end
- it "does not save edits to category_tag if the category has associated bodies" do
- body = FactoryGirl.create(:public_body, :tag_string => @tag)
- post :update, { :id => @category.id,
- :public_body_category => { :category_tag => "renamed" } }
-
- msg = "There are authorities associated with this category, so the tag can't be renamed"
- request.flash[:error].should == msg
- pbc = PublicBodyCategory.find(@category.id)
- pbc.category_tag.should == @tag
+ context 'on success' do
+
+ before(:each) do
+ @params = { :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :title => 'Renamed' }
+ }
+ }
+ }
+ end
+
+ it 'saves edits to a public body category' do
+ post :update, @params
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.title).to eq('Renamed')
+ end
+
+ it 'notifies the admin that the category was created' do
+ post :update, @params
+ expect(flash[:notice]).to eq('Category was successfully updated.')
+ end
+
+ it 'redirects to the category edit page' do
+ post :update, @params
+ expect(response).to redirect_to(edit_admin_category_path(@category))
+ end
+
+ it 'saves edits to category_tag if the category has no associated bodies' do
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'empty')
+
+ post :update, :id => category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+ category = PublicBodyCategory.find(category.id)
+ expect(category.category_tag).to eq('Renamed')
+ end
+
end
+ context 'on success for multiple locales' do
+
+ it "saves edits to a public body category in another locale" do
+ @category.title(:es).should == 'Los category'
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed',
+ :description => 'ES Description' }
+ }
+ }
+
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.title(:es)).to eq('Renamed')
+ expect(category.title(:en)).to eq(@category.title(:en))
+ end
+
+ it 'adds a new translation' do
+ @category.translation_for(:es).destroy
+ @category.reload
+
+ put :update, {
+ :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :locale => "es",
+ :title => "Example Public Body Category ES",
+ :description => @category.description(:es) }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ pbc = PublicBodyCategory.find(@category.id)
+
+ I18n.with_locale(:es) do
+ expect(pbc.title).to eq('Example Public Body Category ES')
+ end
+ end
+
+ it 'adds new translations' do
+ @category.translation_for(:es).destroy
+ @category.reload
+
+ post :update, {
+ :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :locale => "es",
+ :title => "Example Public Body Category ES",
+ :description => 'ES Description' },
+ 'fr' => { :locale => "fr",
+ :title => "Example Public Body Category FR",
+ :description => 'FR Description' }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ pbc = PublicBodyCategory.find(@category.id)
+
+ I18n.with_locale(:es) do
+ expect(pbc.title).to eq('Example Public Body Category ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(pbc.title).to eq('Example Public Body Category FR')
+ end
+ end
+
+ it 'updates an existing translation and adds a third translation' do
+ post :update, {
+ :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ # Update existing translation
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => "es",
+ :title => "Renamed Example Public Body Category ES",
+ :description => @category.description },
+ # Add new translation
+ 'fr' => { :locale => "fr",
+ :title => "Example Public Body Category FR",
+ :description => @category.description }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ pbc = PublicBodyCategory.find(@category.id)
+
+ I18n.with_locale(:es) do
+ expect(pbc.title).to eq('Renamed Example Public Body Category ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(pbc.title).to eq('Example Public Body Category FR')
+ end
+ end
+
+ it "redirects to the edit page after a successful update" do
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) }
+ } }
+
+ expect(response).to redirect_to(edit_admin_category_path(@category))
+ end
- it "save edits to category_tag if the category has no associated bodies" do
- category = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-")
- post :update, { :id => category.id,
- :public_body_category => { :category_tag => "renamed" } }
- request.flash[:notice].should include('success')
- pbc = PublicBodyCategory.find(category.id)
- pbc.category_tag.should == "renamed"
end
- it "redirects to the edit page after a successful update" do
- post :update, { :id => @category.id,
- :public_body_category => { :title => "Renamed" } }
+ context 'on failure' do
+
+ it 'renders the form if creating the record was unsuccessful' do
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => '',
+ :description => @category.description(:en) }
+ } }
+ expect(response).to render_template('edit')
+ end
+
+ it 'is rebuilt with the given params' do
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => 'Need a description',
+ :description => '' }
+ } }
+ expect(assigns(:category).title).to eq('Need a description')
+ end
- expect(response).to redirect_to(edit_admin_category_path(@category))
end
- it "re-renders the edit form after an unsuccessful update" do
- post :update, { :id => @category.id,
- :public_body_category => { :title => '' } }
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => 'Need a description',
+ :description => '' },
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Mi Nuevo Category',
+ :description => 'ES Description' }
+ } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :update, :id => @category.id,
+ :public_body_category => @params
+ expect(assigns(:category).title(:en)).to eq('Need a description')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :update, :id => @category.id,
+ :public_body_category => @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:category).title).to eq('Mi Nuevo Category')
+ end
+ end
- expect(response).to render_template('edit')
end
end
- context 'when destroying a public body category' do
- it "destroys empty public body categories" do
- pbc = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-")
- n = PublicBodyCategory.count
- post :destroy, { :id => pbc.id }
- response.should redirect_to(admin_categories_path)
- PublicBodyCategory.count.should == n - 1
+ describe :destroy do
+
+ it 'uses the current locale by default' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id
+ expect(assigns(:locale)).to eq(I18n.locale.to_s)
end
- it "destroys non-empty public body categories" do
+ it 'sets the locale if the show_locale param is passed' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id, :show_locale => 'es'
+ expect(assigns(:locale)).to eq('es')
+ end
+
+ it 'destroys empty public body categories' do
+ PublicBodyCategory.destroy_all
+
+ category = FactoryGirl.create(:public_body_category)
+
+ expect{
+ post :destroy, :id => category.id
+ }.to change{ PublicBodyCategory.count }.from(1).to(0)
+ end
+
+ it 'destroys non-empty public body categories' do
+ PublicBodyCategory.destroy_all
+
+ # FIXME: Couldn't create the PublicBodyCategory with a Factory
+ # because #authorities= doesn't exist?
+ # undefined method `authorities=' for
+ # #<PublicBodyCategory:0x7f55cbb84f70>
authority = FactoryGirl.create(:public_body)
- pbc = PublicBodyCategory.create(:title => "In-Use Category", :category_tag => "empty", :description => "-", :authorities => [authority])
- n = PublicBodyCategory.count
- post :destroy, { :id => pbc.id }
- response.should redirect_to(admin_categories_path)
- PublicBodyCategory.count.should == n - 1
+ category = PublicBodyCategory.create(:title => "In-Use Category",
+ :category_tag => "empty",
+ :description => "-",
+ :authorities => [authority])
+
+ expect{
+ post :destroy, :id => category.id
+ }.to change{ PublicBodyCategory.count }.from(1).to(0)
+ end
+
+ it 'notifies the admin that the category was destroyed' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id
+ expect(flash[:notice]).to eq('Category was successfully destroyed.')
+ end
+
+ it 'redirects to the categories index' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id
+ expect(response).to redirect_to(admin_categories_path)
end
+
end
end
diff --git a/spec/integration/admin_public_body_category_edit_spec.rb b/spec/integration/admin_public_body_category_edit_spec.rb
new file mode 100644
index 000000000..043524189
--- /dev/null
+++ b/spec/integration/admin_public_body_category_edit_spec.rb
@@ -0,0 +1,59 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl')
+
+describe 'Editing a Public Body Category' do
+ before do
+ AlaveteliConfiguration.stub!(:skip_admin_auth).and_return(false)
+
+ confirm(:admin_user)
+ @admin = login(:admin_user)
+ @category = FactoryGirl.create(:public_body_category)
+ end
+
+ it 'can edit the default locale' do
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_title__en', :with => 'New Category EN'
+ @admin.click_button 'Save'
+
+ @category.reload
+ expect(@category.title).to eq('New Category EN')
+ end
+
+ it 'can add a translation for a single locale' do
+ expect(@category.find_translation_by_locale('fr')).to be_nil
+
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_translations_attributes_fr_title__fr', :with => 'New Category FR'
+ @admin.fill_in 'public_body_category_translations_attributes_fr_description__fr', :with => 'FR Description'
+ @admin.click_button 'Save'
+
+ @category.reload
+ I18n.with_locale(:fr) do
+ expect(@category.title).to eq('New Category FR')
+ end
+ end
+
+ it 'can add a translation for multiple locales' do
+ # Add FR translation
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_translations_attributes_fr_title__fr', :with => 'New Category FR'
+ @admin.fill_in 'public_body_category_translations_attributes_fr_description__fr', :with => 'FR Description'
+ @admin.click_button 'Save'
+
+ # Add ES translation
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_translations_attributes_es_title__es', :with => 'New Category ES'
+ @admin.fill_in 'public_body_category_translations_attributes_es_description__es', :with => 'ES Description'
+ @admin.click_button 'Save'
+
+ @category.reload
+ I18n.with_locale(:fr) do
+ expect(@category.title).to eq('New Category FR')
+ end
+
+ I18n.with_locale(:es) do
+ expect(@category.title).to eq('New Category ES')
+ end
+ end
+
+end
diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb
index c16c9b8a1..297bd096a 100644
--- a/spec/models/public_body_category_spec.rb
+++ b/spec/models/public_body_category_spec.rb
@@ -34,5 +34,145 @@ describe PublicBodyCategory do
category.should_not be_valid
category.errors[:description].should == ["Description can't be blank"]
end
+
+ it 'validates the translations' do
+ category = FactoryGirl.build(:public_body_category)
+ translation = category.translations.build
+ expect(category).to_not be_valid
+ end
+
+ it 'uses the base model validation for the default locale' do
+ category = PublicBodyCategory.new
+ translation = category.translations.build(:locale => 'en',
+ :description => 'No title')
+ category.valid?
+ translation.valid?
+
+ expect(category).to have(1).error_on(:title)
+ expect(translation).to have(0).errors_on(:title)
+ end
+
end
+
+ describe :save do
+
+ it 'saves translations' do
+ category = FactoryGirl.build(:public_body_category)
+ category.translations_attributes = { :es => { :locale => 'es',
+ :title => 'El Category',
+ :description => 'Spanish description' } }
+
+ category.save
+ expect(PublicBodyCategory.find(category.id).translations.size).to eq(2)
+ end
+
+ end
+
+ describe :translations_attributes= do
+
+ context 'translation_attrs is a Hash' do
+
+ it 'does not persist translations' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations_attributes = { :es => { :locale => 'es',
+ :title => 'El Category',
+ :description => 'Spanish description' } }
+
+ expect(PublicBodyCategory.find(category.id).translations.size).to eq(1)
+ end
+
+ it 'creates a new translation' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations_attributes = { :es => { :locale => 'es',
+ :title => 'El Category',
+ :description => 'Spanish description' } }
+ category.save
+ category.reload
+ expect(category.title(:es)).to eq('El Category')
+ end
+
+ it 'updates an existing translation' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations_attributes = { 'es' => { :locale => 'es',
+ :title => 'Name',
+ :description => 'Desc' } }
+ category.save
+
+ category.translations_attributes = { 'es' => { :id => category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed',
+ :description => 'Desc' } }
+ category.save
+ expect(category.title(:es)).to eq('Renamed')
+ end
+
+ it 'updates an existing translation and creates a new translation' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations.create(:locale => 'es',
+ :title => 'Los Category',
+ :description => 'ES Description')
+
+ expect(category.translations.size).to eq(2)
+
+ category.translations_attributes = {
+ 'es' => { :id => category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :title => 'Le Category' }
+ }
+
+ expect(category.translations.size).to eq(3)
+ I18n.with_locale(:es) { expect(category.title).to eq('Renamed') }
+ I18n.with_locale(:fr) { expect(category.title).to eq('Le Category') }
+ end
+
+ it 'skips empty translations' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations.create(:locale => 'es',
+ :title => 'Los Category',
+ :description => 'ES Description')
+
+ expect(category.translations.size).to eq(2)
+
+ category.translations_attributes = {
+ 'es' => { :id => category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
+
+ expect(category.translations.size).to eq(2)
+ end
+
+ end
+ end
+
+end
+
+describe PublicBodyCategory::Translation do
+
+ it 'requires a locale' do
+ translation = PublicBodyCategory::Translation.new
+ translation.valid?
+ expect(translation.errors[:locale]).to eq(["can't be blank"])
+ end
+
+ it 'is valid if no required attributes are assigned' do
+ translation = PublicBodyCategory::Translation.new(:locale => I18n.default_locale)
+ expect(translation).to be_valid
+ end
+
+ it 'requires a title if another required attribute is assigned' do
+ translation = PublicBodyCategory::Translation.new(:description => 'spec')
+ translation.valid?
+ expect(translation.errors[:title]).to eq(["Title can't be blank"])
+ end
+
+ it 'requires a description if another required attribute is assigned' do
+ translation = PublicBodyCategory::Translation.new(:title => 'spec')
+ translation.valid?
+ expect(translation.errors[:description]).to eq(["Description can't be blank"])
+ end
+
end