diff options
Diffstat (limited to 'app')
4 files changed, 123 insertions, 71 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> |