aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlizconlan <liz@mysociety.org>2014-07-11 17:46:49 +0100
committerLouise Crow <louise.crow@gmail.com>2014-09-22 12:39:01 +0100
commit6186bc07c36abb1ebe153fb4eb9967d8ece5e414 (patch)
treefa8842b414a18dab04341ba67d62fdb79518072d
parentb44fe3aba891e4e7de26a1ca237d04eba8329760 (diff)
Replace existing PublicBodyCategories functionality with db models PublicBodyCategory and PublicBodyHeading
-rw-r--r--app/controllers/admin_public_body_controller.rb2
-rw-r--r--app/models/public_body_category.rb125
-rw-r--r--app/models/public_body_heading.rb12
-rw-r--r--config/initializers/alaveteli.rb6
-rw-r--r--db/migrate/20140710094405_create_public_body_headings_and_categories.rb27
-rw-r--r--lib/public_body_categories.rb60
-rw-r--r--spec/models/public_body_category_spec.rb (renamed from spec/lib/public_body_categories_spec.rb)19
-rw-r--r--spec/models/public_body_heading_spec.rb13
8 files changed, 197 insertions, 67 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb
index 120419a27..f7a80476c 100644
--- a/app/controllers/admin_public_body_controller.rb
+++ b/app/controllers/admin_public_body_controller.rb
@@ -4,8 +4,6 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
-require "public_body_categories"
-
class AdminPublicBodyController < AdminController
def index
list
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
new file mode 100644
index 000000000..26186bb60
--- /dev/null
+++ b/app/models/public_body_category.rb
@@ -0,0 +1,125 @@
+# == Schema Information
+#
+# 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
+#
+
+require 'forwardable'
+
+class PublicBodyCategory < ActiveRecord::Base
+ attr_accessible :locale, :category_tag, :title, :description
+
+ has_and_belongs_to_many :public_body_headings
+
+ 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
+ ]
+ end
+ end
+ categories
+ end
+
+ # Called from the data files themselves
+ def self.add(locale, categories)
+ 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(
+ {
+ :locale => locale,
+ :category_tag => category[0],
+ :title => category[1],
+ :description => category[2]
+ }
+ )
+ pb_category.public_body_headings << heading
+ pb_category.save
+ end
+ else
+ #headings
+ heading = PublicBodyHeading.find_or_create_by_locale_and_name(locale, category)
+ end
+ end
+ end
+
+ private
+ def self.load_categories()
+ I18n.available_locales.each do |locale|
+ begin
+ load "public_body_categories_#{locale}.rb"
+ rescue MissingSourceFile
+ end
+ end
+ end
+end
+
+# replicate original file-based PublicBodyCategories functionality
+class CategoryCollection
+ include Enumerable
+ extend Forwardable
+ def_delegators :@categories, :each, :<<
+
+ def initialize
+ @categories = []
+ end
+
+ def with_headings
+ @categories
+ end
+
+ def with_description
+ @categories.select() { |a| a.instance_of?(Array) }
+ end
+
+ def tags
+ tags = with_description.map() { |a| a[0] }
+ end
+
+ def by_tag
+ Hash[*with_description.map() { |a| a[0..1] }.flatten]
+ end
+
+ def singular_by_tag
+ Hash[*with_description.map() { |a| [a[0],a[2]] }.flatten]
+ end
+
+ def by_heading
+ output = {}
+ heading = nil
+ @categories.each do |row|
+ if row.is_a?(Array)
+ output[heading] << row[0]
+ else
+ heading = row
+ output[heading] = []
+ end
+ end
+ output
+ end
+
+ def headings
+ output = []
+ @categories.each do |row|
+ unless row.is_a?(Array)
+ output << row
+ end
+ end
+ output
+ end
+end
diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb
new file mode 100644
index 000000000..e9c854a7d
--- /dev/null
+++ b/app/models/public_body_heading.rb
@@ -0,0 +1,12 @@
+# == Schema Information
+#
+# 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
+end
diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb
index 9ea6428ba..850b6ec97 100644
--- a/config/initializers/alaveteli.rb
+++ b/config/initializers/alaveteli.rb
@@ -44,7 +44,6 @@ require 'world_foi_websites.rb'
require 'alaveteli_external_command.rb'
require 'quiet_opener.rb'
require 'mail_handler'
-require 'public_body_categories'
require 'ability'
require 'normalize_string'
require 'alaveteli_file_types'
@@ -62,3 +61,8 @@ AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales,
if Rails.env == 'test' and ActiveRecord::Base.configurations['test']['constraint_disabling'] == false
require 'no_constraint_disabling'
end
+
+# Allow the PublicBodyCategory model to be addressed using the same syntax
+# as the old PublicBodyCategories class without needing to rename everything,
+# make sure we're not going to break any themes
+PublicBodyCategories = PublicBodyCategory
diff --git a/db/migrate/20140710094405_create_public_body_headings_and_categories.rb b/db/migrate/20140710094405_create_public_body_headings_and_categories.rb
new file mode 100644
index 000000000..0ba7f64a0
--- /dev/null
+++ b/db/migrate/20140710094405_create_public_body_headings_and_categories.rb
@@ -0,0 +1,27 @@
+class CreatePublicBodyHeadingsAndCategories < ActiveRecord::Migration
+ def up
+ create_table :public_body_headings, :force => true do |t|
+ t.string :locale
+ t.text :name, :null => false
+ t.integer :display_order
+ end
+
+ create_table :public_body_categories, :force => true do |t|
+ t.string :locale
+ t.text :title, :null => false
+ t.text :category_tag, :null => false
+ t.text :description, :null => false
+ end
+
+ create_table :public_body_categories_public_body_headings, :id => false do |t|
+ t.integer :public_body_category_id, :null => false
+ t.integer :public_body_heading_id, :null => false
+ end
+ end
+
+ def down
+ drop_table :public_body_categories
+ drop_table :public_body_headings
+ drop_table :public_body_categories_public_body_headings
+ end
+end \ No newline at end of file
diff --git a/lib/public_body_categories.rb b/lib/public_body_categories.rb
deleted file mode 100644
index 7f548b130..000000000
--- a/lib/public_body_categories.rb
+++ /dev/null
@@ -1,60 +0,0 @@
-# lib/public_body_categories.rb:
-# Categorisations of public bodies.
-#
-# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
-# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
-
-class PublicBodyCategories
-
- attr_reader :with_description,
- :with_headings,
- :tags,
- :by_tag,
- :singular_by_tag,
- :by_heading,
- :headings
-
- def initialize(categories)
- @with_headings = categories
- # Arranged in different ways for different sorts of displaying
- @with_description = @with_headings.select() { |a| a.instance_of?(Array) }
- @tags = @with_description.map() { |a| a[0] }
- @by_tag = Hash[*@with_description.map() { |a| a[0..1] }.flatten]
- @singular_by_tag = Hash[*@with_description.map() { |a| [a[0],a[2]] }.flatten]
- @by_heading = {}
- heading = nil
- @headings = []
- @with_headings.each do |row|
- if ! row.instance_of?(Array)
- heading = row
- @headings << row
- @by_heading[row] = []
- else
- @by_heading[heading] << row[0]
- end
- end
- end
-
-
- def PublicBodyCategories.get
- load_categories if @@CATEGORIES.empty?
- @@CATEGORIES[I18n.locale.to_s] || @@CATEGORIES[I18n.default_locale.to_s] || PublicBodyCategories.new([])
- end
-
- # Called from the data files themselves
- def PublicBodyCategories.add(locale, categories)
- @@CATEGORIES[locale.to_s] = PublicBodyCategories.new(categories)
- end
-
- private
- @@CATEGORIES = {}
-
- def PublicBodyCategories.load_categories()
- I18n.available_locales.each do |locale|
- begin
- load "public_body_categories_#{locale}.rb"
- rescue MissingSourceFile
- end
- end
- end
-end
diff --git a/spec/lib/public_body_categories_spec.rb b/spec/models/public_body_category_spec.rb
index e53d9a028..9d1105084 100644
--- a/spec/lib/public_body_categories_spec.rb
+++ b/spec/models/public_body_category_spec.rb
@@ -1,6 +1,17 @@
+# == Schema Information
+#
+# 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
+#
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe PublicBodyCategories do
+describe PublicBodyCategory do
before do
load_test_categories
@@ -17,7 +28,7 @@ describe PublicBodyCategories do
"Miscellaneous", ["other",
"Miscellaneous",
"miscellaneous"]]
- PublicBodyCategories::get().with_headings().should == expected_categories
+ PublicBodyCategory::get().with_headings().should == expected_categories
end
end
@@ -25,7 +36,7 @@ describe PublicBodyCategories do
describe 'when asked for headings' do
it 'should return a list of headings' do
- PublicBodyCategories::get().headings().should == ['Local and regional', 'Miscellaneous']
+ PublicBodyCategory::get().headings().should == ['Local and regional', 'Miscellaneous']
end
end
@@ -33,7 +44,7 @@ describe PublicBodyCategories do
describe 'when asked for tags by headings' do
it 'should return a hash of tags keyed by heading' do
- PublicBodyCategories::get().by_heading().should == {'Local and regional' => ['local_council'],
+ PublicBodyCategory::get().by_heading().should == {'Local and regional' => ['local_council'],
'Miscellaneous' => ['other']}
end
diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb
new file mode 100644
index 000000000..73b5167fb
--- /dev/null
+++ b/spec/models/public_body_heading_spec.rb
@@ -0,0 +1,13 @@
+# == Schema Information
+#
+# Table name: public_body_headings
+#
+# id :integer not null, primary key
+# locale :string
+# name :text not null
+#
+
+require 'spec_helper'
+
+describe PublicBodyHeading do
+end