diff options
-rw-r--r-- | app/models/public_body_category.rb | 64 | ||||
-rw-r--r-- | app/models/public_body_heading.rb | 5 | ||||
-rw-r--r-- | db/migrate/20140716131107_create_category_translation_tables.rb | 151 | ||||
-rw-r--r-- | spec/models/public_body_category_spec.rb | 38 |
4 files changed, 235 insertions, 23 deletions
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb index 26186bb60..1a88c9d97 100644 --- a/app/models/public_body_category.rb +++ b/app/models/public_body_category.rb @@ -3,7 +3,6 @@ # Table name: public_body_categories # # id :integer not null, primary key -# locale :string # title :text not null # category_tag :text not null # description :text not null @@ -16,18 +15,24 @@ class PublicBodyCategory < ActiveRecord::Base has_and_belongs_to_many :public_body_headings + translates :title, :description + + validates_uniqueness_of :category_tag, :message => N_("Tag is already taken") + def self.get locale = I18n.locale.to_s || default_locale.to_s || "" - headings = PublicBodyHeading.find_all_by_locale(locale) categories = CategoryCollection.new - headings.each do |heading| - categories << heading.name - heading.public_body_categories.each do |category| - categories << [ - category.category_tag, - category.title, - category.description - ] + I18n.with_locale(locale) do + headings = PublicBodyHeading.all + headings.each do |heading| + categories << heading.name + heading.public_body_categories.each do |category| + categories << [ + category.category_tag, + category.title, + category.description + ] + end end end categories @@ -35,25 +40,50 @@ class PublicBodyCategory < ActiveRecord::Base # Called from the data files themselves def self.add(locale, categories) - heading = nil + @heading = nil categories.each do |category| if category.is_a?(Array) #categories - unless PublicBodyCategory.find_by_locale_and_category_tag(locale, category[0]) - pb_category = PublicBodyCategory.new( + pb_category = PublicBodyCategory.find_by_category_tag(category[0]) + unless pb_category + pb_category = PublicBodyCategory.create( { - :locale => locale, :category_tag => category[0], :title => category[1], :description => category[2] } ) - pb_category.public_body_headings << heading - pb_category.save + # add the translation if this is not the default locale + # (occurs when a category is not defined in default locale) + unless pb_category.translations.map { |t| t.locale }.include?(locale) + I18n.with_locale(locale) do + pb_category.title = category[1] + pb_category.description = category[2] + pb_category.save + end + end + pb_category.public_body_headings << @heading + else + I18n.with_locale(locale) do + pb_category.title = category[1] + pb_category.description = category[2] + pb_category.save + end end else #headings - heading = PublicBodyHeading.find_or_create_by_locale_and_name(locale, category) + matching_headings = PublicBodyHeading.with_translations.where(:name => category) + if matching_headings.count > 0 + @heading = matching_headings.first + I18n.with_locale(locale) do + @heading.name = category + @heading.save + end + else + I18n.with_locale(locale) do + @heading = PublicBodyHeading.create(:name => category) + end + end end end end diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb index e9c854a7d..74f7a4374 100644 --- a/app/models/public_body_heading.rb +++ b/app/models/public_body_heading.rb @@ -3,10 +3,13 @@ # Table name: public_body_headings # # id :integer not null, primary key -# locale :string # name :text not null # class PublicBodyHeading < ActiveRecord::Base has_and_belongs_to_many :public_body_categories + + translates :name + + validates_uniqueness_of :name, :message => N_("Name is already taken") end diff --git a/db/migrate/20140716131107_create_category_translation_tables.rb b/db/migrate/20140716131107_create_category_translation_tables.rb new file mode 100644 index 000000000..813a46c65 --- /dev/null +++ b/db/migrate/20140716131107_create_category_translation_tables.rb @@ -0,0 +1,151 @@ +class CreateCategoryTranslationTables < ActiveRecord::Migration + def up + default_locale = I18n.locale.to_s + + fields = {:title => :text, + :description => :text} + PublicBodyCategory.create_translation_table!(fields) + + # copy current values across to the default locale + PublicBodyCategory.where(:locale => default_locale).each do |category| + category.translated_attributes.each do |a, default| + value = category.read_attribute(a) + unless value.nil? + category.send(:"#{a}=", value) + end + end + category.save! + end + + # copy current values across to the non-default locale(s) + PublicBodyCategory.where('locale != ?', default_locale).each do |category| + default_category = PublicBodyCategory.find_by_category_tag_and_locale(category.category_tag, default_locale) + I18n.with_locale(category.locale) do + category.translated_attributes.each do |a, default| + value = category.read_attribute(a) + unless value.nil? + if default_category + default_category.send(:"#{a}=", value) + else + category.send(:"#{a}=", value) + end + end + category.delete if default_category + end + end + if default_category + default_category.save! + category.delete + else + category.save! + end + end + + fields = { :name => :text } + PublicBodyHeading.create_translation_table!(fields) + + # copy current values across to the default locale + PublicBodyHeading.where(:locale => default_locale).each do |heading| + heading.translated_attributes.each do |a, default| + value = category.read_attribute(a) + unless value.nil? + heading.send(:"#{a}=", value) + end + end + heading.save! + end + + # copy current values across to the non-default locale(s) + PublicBodyHeading.where('locale != ?', default_locale).each do |heading| + default_heading = PublicBodyHeading.find_by_name_and_locale(heading.name, default_locale) + I18n.with_locale(category.locale) do + heading.translated_attributes.each do |a, default| + value = heading.read_attribute(a) + unless value.nil? + if default_heading + default_heading.send(:"#{a}=", value) + else + heading.send(:"#{a}=", value) + end + end + heading.delete if default_heading + end + end + if default_heading + default_heading.save! + heading.delete + else + heading.save! + end + end + + # finally, drop the old locale column from both tables + remove_column :public_body_headings, :locale + remove_column :public_body_categories, :locale + + # and set category_tag to be unique + add_index :public_body_categories, :category_tag, :unique => true + end + + def down + # reinstate the columns + add_column :public_body_categories, :locale, :string + add_column :public_body_headings, :locale, :string + + # drop the index + remove_index :public_body_categories, :category_tag + + # restore the data + new_categories = [] + PublicBodyCategory.all.each do |category| + category.locale = category.translation.locale.to_s + I18n.available_locales.each do |locale| + if locale.to_s != category.locale + translation = category.translations.find_by_locale(locale) + if translation + new_cat = category.dup + category.translated_attributes.each do |a, _| + value = translation.read_attribute(a) + new_cat.send(:"#{a}=", value) + end + new_cat.locale = locale.to_s + new_categories << new_cat + end + else + category.save! + end + end + end + new_categories.each do |cat| + cat.save! + end + + new_headings = [] + PublicBodyHeading.all.each do |heading| + heading.locale = heading.translation.locale.to_s + I18n.available_locales.each do |locale| + if locale.to_s != heading.locale + new_heading = heading.dup + translation = heading.translations.find_by_locale(locale) + if translation + heading.translated_attributes.each do |a, _| + value = translation.read_attribute(a) + new_heading.send(:"#{a}=", value) + end + new_heading.locale = locale.to_s + new_headings << new_heading + end + else + heading.save! + end + end + end + new_headings.each do |heading| + heading.save! + end + + # drop the translation tables + PublicBodyCategory.drop_translation_table! + PublicBodyHeading.drop_translation_table! + end +end
\ No newline at end of file diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb index 9d1105084..9ec99e395 100644 --- a/spec/models/public_body_category_spec.rb +++ b/spec/models/public_body_category_spec.rb @@ -18,7 +18,6 @@ describe PublicBodyCategory do end describe 'when asked for categories with headings' do - it 'should return a list of headings as plain strings, each followed by n tag specifications as lists in the form: ["tag_to_use_as_category", "Sub category title", "Instance description"]' do @@ -30,24 +29,53 @@ describe PublicBodyCategory do "miscellaneous"]] PublicBodyCategory::get().with_headings().should == expected_categories end - end describe 'when asked for headings' do - it 'should return a list of headings' do PublicBodyCategory::get().headings().should == ['Local and regional', 'Miscellaneous'] end - end describe 'when asked for tags by headings' do - it 'should return a hash of tags keyed by heading' do PublicBodyCategory::get().by_heading().should == {'Local and regional' => ['local_council'], 'Miscellaneous' => ['other']} end + end + + describe 'when asked for categories with description' do + it 'should return a list of tag specifications as lists in the form: + ["tag_to_use_as_category", "Sub category title", "Instance description"]' do + expected_categories = [ + ["local_council", "Local councils", "a local council"], + ["other", "Miscellaneous", "miscellaneous"] + ] + PublicBodyCategory::get().with_description().should == expected_categories + end + end + + describe 'when asked for tags' do + it 'should return a list of tags' do + PublicBodyCategory::get().tags().should == ["local_council", "other"] + end + end + describe 'when asked for categories by tag' do + it 'should return a hash of categories keyed by tag' do + PublicBodyCategory::get().by_tag().should == { + "local_council" => "Local councils", + "other" => "Miscellaneous" + } + end end + describe 'when asked for singular_by_tag' do + it 'should return a hash of category descriptions keyed by tag' do + PublicBodyCategory::get().singular_by_tag().should == { + "local_council" => "a local council", + "other" => "miscellaneous" + } + end + end end
\ No newline at end of file |