diff options
-rw-r--r-- | app/controllers/admin_public_body_category_controller.rb | 4 | ||||
-rw-r--r-- | app/models/public_body_category.rb | 83 | ||||
-rw-r--r-- | app/models/public_body_heading.rb | 7 | ||||
-rw-r--r-- | config/initializers/alaveteli.rb | 1 | ||||
-rw-r--r-- | lib/category_and_heading_migrator.rb | 85 | ||||
-rw-r--r-- | spec/models/public_body_category_spec.rb | 4 |
6 files changed, 101 insertions, 83 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 diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index 850b6ec97..18cce188d 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -53,6 +53,7 @@ require 'theme' require 'xapian_queries' require 'date_quarter' require 'public_body_csv' +require 'category_and_heading_migrator' AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales, AlaveteliConfiguration::default_locale) diff --git a/lib/category_and_heading_migrator.rb b/lib/category_and_heading_migrator.rb new file mode 100644 index 000000000..93b8fc3b2 --- /dev/null +++ b/lib/category_and_heading_migrator.rb @@ -0,0 +1,85 @@ +module CategoryAndHeadingMigrator + + # This module migrates data from public_body_categories_[locale].rb files + # into PublicBodyHeading and PublicBodyCategory models + + # Load all the data from public_body_categories_[locale].rb files. + def self.migrate_categories_and_headings + @first_locale = true + I18n.available_locales.each do |locale| + begin + load "public_body_categories_#{locale}.rb" + rescue MissingSourceFile + end + @first_locale = false + end + end + + # Load the categories and headings for a locale + def self.add_categories_and_headings_from_list(locale, data_list) + # set the counter for headings loaded from this locale + @@locale_heading_display_order = 0 + 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, @first_locale) + end + end + end + + def self.add_category(category_data, heading, locale) + tag, title, description = category_data + category = PublicBodyCategory.find_by_category_tag(tag) + if category + add_category_in_locale(category, title, description, locale) + 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) + add_category_in_locale(category, title, description, locale) + end + end + heading.add_category(category) + end + + def self.add_category_in_locale(category, title, description, locale) + I18n.with_locale(locale) do + category.title = title + category.description = description + category.save + end + end + + def self.add_heading(name, locale, first_locale) + heading = PublicBodyHeading.with_translations.where(:name => name).first + + # For multi-locale installs, we assume that all public_body_[locale].rb files + # use the same headings in the same order, so we add translations to the heading + # that was in the same position in the list loaded from other public_body_[locale].rb + # files. + if heading.nil? && !@first_locale + heading = PublicBodyHeading.where(:display_order => @@locale_heading_display_order).first + end + + if heading + I18n.with_locale(locale) do + heading.name = name + heading.save + end + else + I18n.with_locale(locale) do + heading = PublicBodyHeading.create(:name => name) + end + end + @@locale_heading_display_order += 1 + heading + end +end diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb index b7ef63ac3..3f6fbe5ec 100644 --- a/spec/models/public_body_category_spec.rb +++ b/spec/models/public_body_category_spec.rb @@ -45,9 +45,9 @@ describe PublicBodyCategory do context "requesting data" do - it 'should call load_categories if categories are not already loaded' do + it 'should migrate categories if categories are not already loaded' do PublicBodyCategory.stub!(:count).and_return(0) - PublicBodyCategory.should_receive(:load_categories) + CategoryAndHeadingMigrator.should_receive(:migrate_categories_and_headings) PublicBodyCategory::get() end |