Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display translations on failed validation #2166

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/assets/stylesheets/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,18 @@ body.admin {
padding: 3px 0;
}

.fieldWithErrors {
display:block;
padding:0.2em;
textarea, input {
border:solid 1px Red !important;
}
}

.tabWithErrors {
border-color: red !important;
border-bottom-color: transparent !important;
}

}

44 changes: 26 additions & 18 deletions app/controllers/admin_public_body_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,39 @@ def index

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
Expand All @@ -43,6 +65,8 @@ def update
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
Expand All @@ -51,29 +75,13 @@ def update
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
Expand Down
12 changes: 4 additions & 8 deletions app/controllers/admin_public_body_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ def show

def new
@public_body = PublicBody.new

I18n.available_locales.each do |locale|
@public_body.translations.build(:locale => locale)
end
@public_body.build_all_translations

if params[:change_request_id]
@change_request = PublicBodyChangeRequest.find(params[:change_request_id])
Expand Down Expand Up @@ -118,17 +115,15 @@ def create
flash[:notice] = 'PublicBody was successfully created.'
redirect_to admin_body_show_url(@public_body)
else
@public_body.build_all_translations
render :action => 'new'
end
end
end

def edit
@public_body = PublicBody.find(params[:id])

I18n.available_locales.each do |locale|
@public_body.translations.find_or_initialize_by_locale(locale)
end
@public_body.build_all_translations

if params[:change_request_id]
@change_request = PublicBodyChangeRequest.find(params[:change_request_id])
Expand Down Expand Up @@ -159,6 +154,7 @@ def update
flash[:notice] = 'PublicBody was successfully updated.'
redirect_to admin_body_show_url(@public_body)
else
@public_body.build_all_translations
render :action => 'edit'
end
end
Expand Down
63 changes: 33 additions & 30 deletions app/controllers/admin_public_body_headings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
class AdminPublicBodyHeadingsController < AdminController

def new
@heading = PublicBodyHeading.new
@heading.build_all_translations
end

def create
I18n.with_locale(I18n.default_locale) do
@heading = PublicBodyHeading.new(params[:public_body_heading])
if @heading.save
flash[:notice] = 'Heading was successfully created.'
redirect_to admin_categories_url
else
@heading.build_all_translations
render :action => 'new'
end
end
end

def edit
@heading = PublicBodyHeading.find(params[:id])
render :formats => [:html]
@heading.build_all_translations
end

def update
@heading = PublicBodyHeading.find(params[:id])

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.'
flash[:notice] = 'Heading was successfully updated.'
redirect_to edit_admin_heading_path(@heading)
else
@heading.build_all_translations
render :action => 'edit'
end
end
end

def destroy
@locale = self.locale_from_params
I18n.with_locale(@locale) do
heading = PublicBodyHeading.find(params[:id])
heading.destroy
flash[:notice] = "Heading was successfully destroyed."
redirect_to admin_categories_url
end
end

def reorder
transaction = reorder_headings(params[:headings])
if transaction[:success]
Expand All @@ -35,33 +65,6 @@ def reorder_categories
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_categories_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])
heading.destroy
flash[:notice] = "Category heading was successfully destroyed."
redirect_to admin_categories_url
end
end

protected

def reorder_headings(headings)
Expand Down
45 changes: 17 additions & 28 deletions app/models/public_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PublicBody < ActiveRecord::Base
}

translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
accepts_nested_attributes_for :translations
accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?

# Default fields available for importing from CSV, in the format
# [field_name, 'short description of field (basic html allowed)']
Expand Down Expand Up @@ -152,33 +152,15 @@ def translated_versions
translations
end

def translations_attributes=(translation_attrs)
def empty_translation?(attrs)
attrs_with_values = attrs.select{ |key, value| value != '' and key.to_s != 'locale' }
attrs_with_values.empty?
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]) || PublicBody::Translation.new
t.attributes = attrs
calculate_cached_fields(t)
t.save!
end
else # Array => creating
warn "[DEPRECATION] PublicBody#translations_attributes= " \
"will no longer accept an Array as of release 0.22. " \
"Use Hash arguments instead. See " \
"spec/models/public_body_spec.rb and " \
"app/views/admin_public_body/_form.html.erb for more " \
"details."

translation_attrs.each do |attrs|
next if empty_translation?(attrs)
new_translation = PublicBody::Translation.new(attrs)
calculate_cached_fields(new_translation)
translations << new_translation
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
end

Expand Down Expand Up @@ -806,6 +788,13 @@ def self.popular_bodies(locale)

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

def request_email_if_requestable
# Request_email can be blank, meaning we don't have details
if self.is_requestable?
Expand Down
60 changes: 43 additions & 17 deletions app/models/public_body_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

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"
Expand Down Expand Up @@ -67,25 +70,48 @@ def translated_versions
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
Loading