aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-09-25 13:34:04 +0100
committerLouise Crow <louise.crow@gmail.com>2014-09-25 13:34:04 +0100
commita7955a74e6812133b7441c4ad2d326cfcbdc077c (patch)
treef5c7b3b5dc33e0ee236398784b572224e7df03c1
parentc04926740ea6c4b9c445010f457de619b041adbe (diff)
fixup! Use translation tables for PublicBodyCategory and PublicBodyHeading
-rw-r--r--app/models/public_body_category.rb104
-rw-r--r--spec/models/public_body_heading_spec.rb4
2 files changed, 53 insertions, 55 deletions
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
index da9dd7638..b3ef8cb77 100644
--- a/app/models/public_body_category.rb
+++ b/app/models/public_body_category.rb
@@ -64,61 +64,59 @@ class PublicBodyCategory < ActiveRecord::Base
PublicBodyCategory.find_by_sql(sql)
end
- # Called from the data files themselves
- def self.add(locale, categories)
- @heading = nil
- @heading_order = 0
- categories.each do |category|
- if category.is_a?(Array)
- #categories
- pb_category = PublicBodyCategory.find_by_category_tag(category[0])
- unless pb_category
- pb_category = PublicBodyCategory.create(
- {
- :category_tag => category[0],
- :title => category[1],
- :description => category[2]
- }
- )
- # 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.add_to_heading(@heading)
- else
- I18n.with_locale(locale) do
- pb_category.title = category[1]
- pb_category.description = category[2]
- pb_category.save
- end
- pb_category.add_to_heading(@heading)
+ def self.add_category(category_data, heading, locale)
+ tag, title, description = category_data
+ category = PublicBodyCategory.find_by_category_tag(tag)
+ if category
+ I18n.with_locale(locale) do
+ category.title = title
+ category.description = description
+ category.save
+ end
+ else
+ category = PublicBodyCategory.create(:category_tag => tag,
+ :title => title,
+ :description => description)
+
+ # add the translation if this is not the default locale
+ # (occurs when a category is not defined in default locale)
+ unless category.translations.map { |t| t.locale }.include?(locale)
+ I18n.with_locale(locale) do
+ category.title = title
+ category.description = description
+ category.save
end
+ end
+ end
+ category.add_to_heading(heading)
+ end
+
+ def self.add_heading(name, locale)
+ matching_headings = PublicBodyHeading.with_translations.where(:name => name)
+ if matching_headings.count > 0
+ heading = matching_headings.first
+ I18n.with_locale(locale) do
+ heading.name = name
+ heading.save
+ end
+ else
+ I18n.with_locale(locale) do
+ heading = PublicBodyHeading.create(:name => name)
+ end
+ end
+ heading
+ end
+
+ # Called from the data files themselves
+ def self.add(locale, data_list)
+ current_heading = nil
+ data_list.each do |list_item|
+ if list_item.is_a?(Array)
+ # item is list of category data
+ add_category(list_item, current_heading, locale)
else
- #headings
- 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
- last_heading = PublicBodyHeading.last
- if last_heading
- @heading_order = last_heading.display_order + 1
- else
- @heading_order = 1
- end
- @heading = PublicBodyHeading.create(:name => category, :display_order => @heading_order)
- end
- end
+ # item is heading name
+ current_heading = add_heading(list_item, locale)
end
end
end
diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb
index 91fd35be8..5d4fa6dd2 100644
--- a/spec/models/public_body_heading_spec.rb
+++ b/spec/models/public_body_heading_spec.rb
@@ -25,9 +25,9 @@ describe PublicBodyHeading do
it 'should use the display_order field to preserve the original data order' do
headings = PublicBodyHeading.all
headings[0].name.should eq 'Local and regional'
- headings[0].display_order.should eq 1
+ headings[0].display_order.should eq 0
headings[1].name.should eq 'Miscellaneous'
- headings[1].display_order.should eq 2
+ headings[1].display_order.should eq 1
end
end