diff options
-rw-r--r-- | app/controllers/admin_public_body_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/public_body_controller.rb | 4 | ||||
-rw-r--r-- | app/models/public_body.rb | 16 | ||||
-rw-r--r-- | app/views/admin_public_body/_tag_help.rhtml | 2 | ||||
-rw-r--r-- | app/views/admin_public_body/import_csv.rhtml | 2 | ||||
-rw-r--r-- | app/views/public_body/list.rhtml | 2 | ||||
-rw-r--r-- | lib/public_body_categories.rb | 43 | ||||
-rw-r--r-- | lib/public_body_categories_en.rb | 160 |
8 files changed, 119 insertions, 112 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb index 8cb7043a0..d80e2937f 100644 --- a/app/controllers/admin_public_body_controller.rb +++ b/app/controllers/admin_public_body_controller.rb @@ -6,6 +6,8 @@ # # $Id: admin_public_body_controller.rb,v 1.23 2009-08-26 00:58:29 francis Exp $ +require "public_body_categories" + class AdminPublicBodyController < AdminController def index list diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 79ca58d13..e4f8753af 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -99,7 +99,7 @@ class PublicBodyController < ApplicationController @tag = "all" conditions = [locale_condition, @query, @query, @locale] elsif @tag == 'other' - category_list = PublicBodyCategories::CATEGORIES.map{|c| "'"+c+"'"}.join(",") + category_list = PublicBodyCategories::get().tags().map{|c| "'"+c+"'"}.join(",") conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' and has_tag_string_tags.name in (' + category_list + ')) = 0', @query, @query, @locale] @@ -121,7 +121,7 @@ class PublicBodyController < ApplicationController elsif @tag.size == 1 @description = _("beginning with") + " '" + @tag + "'" else - @description = PublicBodyCategories::CATEGORIES_BY_TAG[@tag] + @description = PublicBodyCategories::get().by_tag()[@tag] if @description.nil? @description = @tag end diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 8c34f9e27..430aa8f34 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -112,16 +112,6 @@ class PublicBody < ActiveRecord::Base end end - # XXX this should be saner; probably implement categories as data - begin - load "public_body_categories_#{I18n.locale.to_s}.rb" - rescue MissingSourceFile - begin - load "public_body_categories_#{I18n.default_locale.to_s}.rb" - rescue MissingSourceFile - load "public_body_categories.rb" - end - end # Set the first letter, which is used for faster queries before_save(:set_first_letter) def set_first_letter @@ -255,8 +245,8 @@ class PublicBody < ActiveRecord::Base types = [] first = true for tag in self.tags - if PublicBodyCategories::CATEGORIES_BY_TAG.include?(tag.name) - desc = PublicBodyCategories::CATEGORY_SINGULAR_BY_TAG[tag.name] + if PublicBodyCategories::get().by_tag().include?(tag.name) + desc = PublicBodyCategories::get().singular_by_tag()[tag.name] if first # terrible that Ruby/Rails doesn't have an equivalent of ucfirst # (capitalize shockingly converts later characters to lowercase) @@ -518,7 +508,7 @@ class PublicBody < ActiveRecord::Base end def notes_without_html # assume notes are reasonably behaved HTML, so just use simple regexp on this - self.notes.gsub(/<\/?[^>]*>/, "") + self.notes.nil? ? '' : self.notes.gsub(/<\/?[^>]*>/, "") end def json_for_api diff --git a/app/views/admin_public_body/_tag_help.rhtml b/app/views/admin_public_body/_tag_help.rhtml index 7954ff992..0d0f84dda 100644 --- a/app/views/admin_public_body/_tag_help.rhtml +++ b/app/views/admin_public_body/_tag_help.rhtml @@ -1,7 +1,7 @@ <div id="tag_help"> <h2>List of tags</h2> <% first_row = true %> - <% for row in PublicBodyCategories::CATEGORIES_WITH_HEADINGS %> + <% for row in PublicBodyCategories::get().with_headings() %> <% if row.instance_of?(Array) %> <% if row[0] != 'other' %> <strong><%= row[0] %></strong>=<%= row[1] %> diff --git a/app/views/admin_public_body/import_csv.rhtml b/app/views/admin_public_body/import_csv.rhtml index 1523ef289..a7a13f477 100644 --- a/app/views/admin_public_body/import_csv.rhtml +++ b/app/views/admin_public_body/import_csv.rhtml @@ -49,7 +49,7 @@ <hr> <p>Standard tags: - <% for category, description in PublicBodyCategories::CATEGORIES_BY_TAG %> + <% for category, description in PublicBodyCategories::get().by_tag() %> <% if category != "other" %> <strong><%= category %></strong>=<%= description %>; <% end %> diff --git a/app/views/public_body/list.rhtml b/app/views/public_body/list.rhtml index e4c803113..81593fcc7 100644 --- a/app/views/public_body/list.rhtml +++ b/app/views/public_body/list.rhtml @@ -7,7 +7,7 @@ </li></ul> <% first_row = true %> - <% for row in PublicBodyCategories::CATEGORIES_WITH_HEADINGS %> + <% for row in PublicBodyCategories::get().with_headings() %> <% if row.instance_of?(Array) %> <li> <%= link_to_unless (@tag == row[0]), row[1], list_public_bodies_url(:tag => row[0]) %> diff --git a/lib/public_body_categories.rb b/lib/public_body_categories.rb index 844e14e67..3ede0167e 100644 --- a/lib/public_body_categories.rb +++ b/lib/public_body_categories.rb @@ -6,14 +6,39 @@ # # $Id: public_body_categories.rb,v 1.1 2009-09-14 14:45:48 francis Exp $ -module PublicBodyCategories +class PublicBodyCategories + + attr_reader :with_description, :with_headings, :tags, :by_tag, :singular_by_tag + + 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] + end - CATEGORIES_WITH_HEADINGS = [] - - # Arranged in different ways for different sorts of displaying - CATEGORIES_WITH_DESCRIPTION = CATEGORIES_WITH_HEADINGS.select() { |a| a.instance_of?(Array) } - CATEGORIES = CATEGORIES_WITH_DESCRIPTION.map() { |a| a[0] } - CATEGORIES_BY_TAG = Hash[*CATEGORIES_WITH_DESCRIPTION.map() { |a| a[0..1] }.flatten] - CATEGORY_SINGULAR_BY_TAG = Hash[*CATEGORIES_WITH_DESCRIPTION.map() { |a| [a[0],a[2]] }.flatten] -end + def PublicBodyCategories.get + load_categories() if @@CATEGORIES.nil? + @@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 = nil + + def PublicBodyCategories.load_categories() + @@CATEGORIES = {} if @@CATEGORIES.nil? + I18n.available_locales.each do |locale| + begin + load "lib/public_body_categories_#{locale}.rb" + rescue MissingSourceFile + end + end + end +end
\ No newline at end of file diff --git a/lib/public_body_categories_en.rb b/lib/public_body_categories_en.rb index 93183d397..5fe762357 100644 --- a/lib/public_body_categories_en.rb +++ b/lib/public_body_categories_en.rb @@ -6,88 +6,78 @@ # # $Id: public_body_categories.rb,v 1.1 2009-09-14 14:45:48 francis Exp $ -module PublicBodyCategories - - CATEGORIES_WITH_HEADINGS = [ - "Miscellaneous", - [ "other", "Miscellaneous", "miscellaneous" ], - _("Central government"), - [ "department", "Ministerial departments", "a ministerial department" ], - [ "non_ministerial_department", "Non-ministerial departments", "a non-ministerial department" ], - [ "executive_agency", "Executive agencies", "an executive agency" ], - [ "government_office", "Government offices for the regions", "a government office for the regions" ], - [ "advisory_committee", "Advisory committees", "an advisory committee" ], - [ "awc", "Agricultural wages committees", "an agriculatural wages committee" ], - [ "adhac", "Agricultural dwelling house advisory committees", "an agriculatural dwelling house advisory committee" ], - [ "newdeal", "New Deal for Communities partnership", "a New Deal for Communities partnership" ], - _("Local and regional"), - [ "local_council", "Local councils", "a local council" ], - [ "parish_council", "Town and Parish councils", "a town or parish council"], - [ "housing_association", "Housing associations", "a housing association"], - [ "almo", "Housing ALMOs", "a housing ALMO"], - [ "municipal_bank", "Municipal bank", "a municipal bank"], - [ "nsbody", "North/south bodies", "a north/south body"], - [ "pbo", "Professional buying organisations", "a professional buying organisation"], - [ "regional_assembly", "Regional assemblies", "a regional assembly"], - [ "rda", "Regional development agencies", "a regional development agency" ], - "Education", - [ "university", "Universities", "a university" ], - [ "university_college", "University colleges", "a university college" ], - [ "cambridge_college", "Cambridge colleges", "a Cambridge college" ], - [ "durham_college", "Durham colleges", "a Durham college" ], - [ "oxford_college", "Oxford colleges", "an Oxford college or permanent private hall" ], - [ "york_college", "York colleges", "a college of the University of York" ], - [ "university_owned_company", "University owned companies", "a university owned company" ], - [ "hei", "Higher education institutions", "a higher educational institution" ], - [ "fei", "Further education institutions", "a further educational institution" ], - [ "school", "Schools", "a school" ], - [ "research_council", "Research councils", "a research council" ], - [ "lib_board", "Education and library boards", "an education and library board" ], - [ "rbc", "Regional Broadband Consortia", "a Regional Broadband Consortium" ], - "Environment", - [ "npa", "National park authorities", "a national park authority" ], - [ "rpa", "Regional park authorities", "a regional park authority" ], - [ "sea_fishery_committee", "Sea fisheries committees", "a sea fisheries committee" ], - [ "watercompanies", "Water companies", "a water company" ], - [ "idb", "Internal drainage boards", "an internal drainage board" ], - [ "rfdc", "Regional flood defence committees", "a regional flood defence committee" ], - [ "wda", "Waste disposal authorities", "a waste disposal authority" ], - [ "zoo", "Zoos", "a zoo" ], - "Health", - [ "nhstrust", "NHS trusts", "an NHS trust" ], - [ "pct", "Primary care trusts", "a primary care trust" ], - [ "nhswales", "NHS in Wales", "part of the NHS in Wales" ], - [ "nhsni", "NHS in Northern Ireland", "part of the NHS in Northern Ireland" ], - [ "hscr", "Health / social care", "Relating to health / social care" ], - [ "pha", "Port health authorities", "a port health authority"], - [ "sha", "Strategic health authorities", "a strategic health authority" ], - [ "specialha", "Special health authorities", "a special health authority" ], - "Media and culture", - [ "media", "Media", "a media organisation" ], - [ "rcc", "Cultural consortia", "a cultural consortium"], - [ "museum", "Museums and galleries", "a museum or gallery" ], - "Military and security services", - [ "military_college", "Military colleges", "a military college" ], - [ "security_services", "Security services", "a security services body" ], - "Emergency services and the courts", - [ "police", "Police forces", "a police force" ], - [ "police_authority", "Police authorities", "a police authority" ], - [ "dpp", "District policing partnerships", "a district policing partnership" ], - [ "fire_service", "Fire and rescue services", "a fire and rescue service" ], - [ "prob_board", "Probation boards", "a probation board" ], - [ "rules_committee", "Rules commitees", "a rules committee" ], - [ "tribunal", "Tribunals", "a tribunal"], - "Transport", - [ "npte", "Passenger transport executives", "a passenger transport executive" ], - [ "port_authority", "Port authorities", "a port authority" ], - [ "scp", "Safety Camera Partnerships", "a safety camera partnership" ], - [ "srp", "Safer Roads Partnership", "a safer roads partnership" ] - ] - - # Arranged in different ways for different sorts of displaying - CATEGORIES_WITH_DESCRIPTION = CATEGORIES_WITH_HEADINGS.select() { |a| a.instance_of?(Array) } - CATEGORIES = CATEGORIES_WITH_DESCRIPTION.map() { |a| a[0] } - CATEGORIES_BY_TAG = Hash[*CATEGORIES_WITH_DESCRIPTION.map() { |a| a[0..1] }.flatten] - CATEGORY_SINGULAR_BY_TAG = Hash[*CATEGORIES_WITH_DESCRIPTION.map() { |a| [a[0],a[2]] }.flatten] -end - +PublicBodyCategories.add(:en, [ + "Miscellaneous", + [ "other", "Miscellaneous", "miscellaneous" ], + "Central government", + [ "department", "Ministerial departments", "a ministerial department" ], + [ "non_ministerial_department", "Non-ministerial departments", "a non-ministerial department" ], + [ "executive_agency", "Executive agencies", "an executive agency" ], + [ "government_office", "Government offices for the regions", "a government office for the regions" ], + [ "advisory_committee", "Advisory committees", "an advisory committee" ], + [ "awc", "Agricultural wages committees", "an agriculatural wages committee" ], + [ "adhac", "Agricultural dwelling house advisory committees", "an agriculatural dwelling house advisory committee" ], + [ "newdeal", "New Deal for Communities partnership", "a New Deal for Communities partnership" ], + "Local and regional", + [ "local_council", "Local councils", "a local council" ], + [ "parish_council", "Town and Parish councils", "a town or parish council"], + [ "housing_association", "Housing associations", "a housing association"], + [ "almo", "Housing ALMOs", "a housing ALMO"], + [ "municipal_bank", "Municipal bank", "a municipal bank"], + [ "nsbody", "North/south bodies", "a north/south body"], + [ "pbo", "Professional buying organisations", "a professional buying organisation"], + [ "regional_assembly", "Regional assemblies", "a regional assembly"], + [ "rda", "Regional development agencies", "a regional development agency" ], + "Education", + [ "university", "Universities", "a university" ], + [ "university_college", "University colleges", "a university college" ], + [ "cambridge_college", "Cambridge colleges", "a Cambridge college" ], + [ "durham_college", "Durham colleges", "a Durham college" ], + [ "oxford_college", "Oxford colleges", "an Oxford college or permanent private hall" ], + [ "york_college", "York colleges", "a college of the University of York" ], + [ "university_owned_company", "University owned companies", "a university owned company" ], + [ "hei", "Higher education institutions", "a higher educational institution" ], + [ "fei", "Further education institutions", "a further educational institution" ], + [ "school", "Schools", "a school" ], + [ "research_council", "Research councils", "a research council" ], + [ "lib_board", "Education and library boards", "an education and library board" ], + [ "rbc", "Regional Broadband Consortia", "a Regional Broadband Consortium" ], + "Environment", + [ "npa", "National park authorities", "a national park authority" ], + [ "rpa", "Regional park authorities", "a regional park authority" ], + [ "sea_fishery_committee", "Sea fisheries committees", "a sea fisheries committee" ], + [ "watercompanies", "Water companies", "a water company" ], + [ "idb", "Internal drainage boards", "an internal drainage board" ], + [ "rfdc", "Regional flood defence committees", "a regional flood defence committee" ], + [ "wda", "Waste disposal authorities", "a waste disposal authority" ], + [ "zoo", "Zoos", "a zoo" ], + "Health", + [ "nhstrust", "NHS trusts", "an NHS trust" ], + [ "pct", "Primary care trusts", "a primary care trust" ], + [ "nhswales", "NHS in Wales", "part of the NHS in Wales" ], + [ "nhsni", "NHS in Northern Ireland", "part of the NHS in Northern Ireland" ], + [ "hscr", "Health / social care", "Relating to health / social care" ], + [ "pha", "Port health authorities", "a port health authority"], + [ "sha", "Strategic health authorities", "a strategic health authority" ], + [ "specialha", "Special health authorities", "a special health authority" ], + "Media and culture", + [ "media", "Media", "a media organisation" ], + [ "rcc", "Cultural consortia", "a cultural consortium"], + [ "museum", "Museums and galleries", "a museum or gallery" ], + "Military and security services", + [ "military_college", "Military colleges", "a military college" ], + [ "security_services", "Security services", "a security services body" ], + "Emergency services and the courts", + [ "police", "Police forces", "a police force" ], + [ "police_authority", "Police authorities", "a police authority" ], + [ "dpp", "District policing partnerships", "a district policing partnership" ], + [ "fire_service", "Fire and rescue services", "a fire and rescue service" ], + [ "prob_board", "Probation boards", "a probation board" ], + [ "rules_committee", "Rules commitees", "a rules committee" ], + [ "tribunal", "Tribunals", "a tribunal"], + "Transport", + [ "npte", "Passenger transport executives", "a passenger transport executive" ], + [ "port_authority", "Port authorities", "a port authority" ], + [ "scp", "Safety Camera Partnerships", "a safety camera partnership" ], + [ "srp", "Safer Roads Partnership", "a safer roads partnership" ] +]) |