aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin_public_body_category_controller.rb4
-rw-r--r--app/models/public_body_category.rb83
-rw-r--r--app/models/public_body_heading.rb7
3 files changed, 13 insertions, 81 deletions
diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_category_controller.rb
index 9380aa21b..99a4a7f3c 100644
--- a/app/controllers/admin_public_body_category_controller.rb
+++ b/app/controllers/admin_public_body_category_controller.rb
@@ -42,7 +42,7 @@ class AdminPublicBodyCategoryController < AdminController
end
added_headings.each do |heading_id|
- @category.add_to_heading(PublicBodyHeading.find(heading_id))
+ PublicBodyHeading.find(heading_id).add_category(@category)
end
end
@@ -83,7 +83,7 @@ class AdminPublicBodyCategoryController < AdminController
if @category.save
if params[:headings]
params[:headings].values.each do |heading_id|
- @category.add_to_heading(PublicBodyHeading.find(heading_id))
+ PublicBodyHeading.find(heading_id).add_category(@category)
end
end
flash[:notice] = 'Category was successfully created.'
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
index ffb32ee68..f62174389 100644
--- a/app/models/public_body_category.rb
+++ b/app/models/public_body_category.rb
@@ -24,19 +24,9 @@ class PublicBodyCategory < ActiveRecord::Base
validates_presence_of :category_tag, :message => N_('Tag can\'t be blank')
validates_presence_of :description, :message => N_('Description can\'t be blank')
- def self.load_categories
- I18n.available_locales.each do |locale|
- begin
- load "public_body_categories_#{locale}.rb"
- rescue MissingSourceFile
- end
- end
- end
- private_class_method :load_categories
-
def self.get
- load_categories if PublicBodyCategory.count < 1
-
+ # migrate from file-based public body categories
+ CategoryAndHeadingMigrator.migrate_categories_and_headings if count < 1
locale = I18n.locale.to_s || default_locale.to_s || ""
categories = CategoryCollection.new
I18n.with_locale(locale) do
@@ -64,81 +54,16 @@ class PublicBodyCategory < ActiveRecord::Base
PublicBodyCategory.find_by_sql(sql)
end
- 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
+ # Called from the old-style public_body_categories_[locale].rb data files
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
- # item is heading name
- current_heading = add_heading(list_item, locale)
- end
- end
- end
-
- def add_to_heading(heading)
- if public_body_headings.include?(heading)
- # we already have this, stop
- return
- end
- heading_link = PublicBodyCategoryLink.create(
- :public_body_category_id => self.id,
- :public_body_heading_id => heading.id
- )
+ CategoryAndHeadingMigrator.add_categories_and_headings_from_list(locale, data_list)
end
-
# Convenience methods for creating/editing translations via forms
def find_translation_by_locale(locale)
translations.find_by_locale(locale)
end
-
def translated_versions
translations
end
diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb
index 8e7a8ec3d..c38800561 100644
--- a/app/models/public_body_heading.rb
+++ b/app/models/public_body_heading.rb
@@ -58,6 +58,12 @@ class PublicBodyHeading < ActiveRecord::Base
end
end
+ def add_category(category)
+ unless public_body_categories.include?(category)
+ public_body_categories << category
+ end
+ end
+
def self.next_display_order
if max = maximum(:display_order)
max + 1
@@ -65,4 +71,5 @@ class PublicBodyHeading < ActiveRecord::Base
0
end
end
+
end