diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/alaveteli_localization.rb | 1 | ||||
-rw-r--r-- | lib/category_and_heading_migrator.rb | 91 | ||||
-rw-r--r-- | lib/configuration.rb | 5 | ||||
-rw-r--r-- | lib/health_checks/checks/days_ago_check.rb | 28 | ||||
-rw-r--r-- | lib/health_checks/health_checkable.rb | 28 | ||||
-rw-r--r-- | lib/health_checks/health_checks.rb | 37 | ||||
-rw-r--r-- | lib/public_body_categories.rb | 61 | ||||
-rw-r--r-- | lib/public_body_categories_en.rb | 19 | ||||
-rw-r--r-- | lib/routing_filters.rb | 8 |
9 files changed, 202 insertions, 76 deletions
diff --git a/lib/alaveteli_localization.rb b/lib/alaveteli_localization.rb index 6daab124a..2b6978c92 100644 --- a/lib/alaveteli_localization.rb +++ b/lib/alaveteli_localization.rb @@ -7,6 +7,7 @@ class AlaveteliLocalization I18n.locale = default_locale I18n.available_locales = available_locales.map { |locale_name| locale_name.to_sym } I18n.default_locale = default_locale + RoutingFilter::Conditionallyprependlocale.locales = available_locales end def set_default_text_domain(name, path) diff --git a/lib/category_and_heading_migrator.rb b/lib/category_and_heading_migrator.rb new file mode 100644 index 000000000..402ea7204 --- /dev/null +++ b/lib/category_and_heading_migrator.rb @@ -0,0 +1,91 @@ +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 + if PublicBodyCategory.count > 0 + puts "PublicBodyCategories exist already, not migrating." + else + @first_locale = true + I18n.available_locales.each do |locale| + begin + load "public_body_categories_#{locale}.rb" + rescue MissingSourceFile + end + @first_locale = false + end + 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 = nil + I18n.with_locale(locale) do + heading = PublicBodyHeading.find_by_name(name) + end + # 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/lib/configuration.rb b/lib/configuration.rb index bd2d31ac2..2144f9954 100644 --- a/lib/configuration.rb +++ b/lib/configuration.rb @@ -42,11 +42,12 @@ module AlaveteliConfiguration :HTML_TO_PDF_COMMAND => '', :INCLUDE_DEFAULT_LOCALE_IN_URLS => true, :INCOMING_EMAIL_DOMAIN => 'localhost', - :INCOMING_EMAIL_PREFIX => '', + :INCOMING_EMAIL_PREFIX => 'foi+', :INCOMING_EMAIL_SECRET => 'dummysecret', :ISO_COUNTRY_CODE => 'GB', :MINIMUM_REQUESTS_FOR_STATISTICS => 100, - :MAX_REQUESTS_PER_USER_PER_DAY => '', + :MAX_REQUESTS_PER_USER_PER_DAY => 6, + :MTA_LOG_PATH => '/var/log/exim4/exim-mainlog-*', :MTA_LOG_TYPE => 'exim', :NEW_RESPONSE_REMINDER_AFTER_DAYS => [3, 10, 24], :OVERRIDE_ALL_PUBLIC_BODY_REQUEST_EMAILS => '', diff --git a/lib/health_checks/checks/days_ago_check.rb b/lib/health_checks/checks/days_ago_check.rb new file mode 100644 index 000000000..793fff586 --- /dev/null +++ b/lib/health_checks/checks/days_ago_check.rb @@ -0,0 +1,28 @@ +module HealthChecks + module Checks + class DaysAgoCheck + include HealthChecks::HealthCheckable + + attr_reader :days, :subject + + def initialize(args = {}, &block) + @days = args.fetch(:days) { 1 } + @subject = block + super(args) + end + + def failure_message + "#{ super }: #{ subject.call }" + end + + def success_message + "#{ super }: #{ subject.call }" + end + + def check + subject.call >= days.days.ago + end + + end + end +end diff --git a/lib/health_checks/health_checkable.rb b/lib/health_checks/health_checkable.rb new file mode 100644 index 000000000..5d674ca32 --- /dev/null +++ b/lib/health_checks/health_checkable.rb @@ -0,0 +1,28 @@ +module HealthChecks + module HealthCheckable + + attr_accessor :failure_message, :success_message + + def initialize(args = {}) + self.failure_message = args.fetch(:failure_message) { _('Failed') } + self.success_message = args.fetch(:success_message) { _('Success') } + end + + def name + self.class.to_s + end + + def check + raise NotImplementedError + end + + def ok? + check ? true : false + end + + def message + ok? ? success_message : failure_message + end + + end +end diff --git a/lib/health_checks/health_checks.rb b/lib/health_checks/health_checks.rb new file mode 100644 index 000000000..6f0c9de8e --- /dev/null +++ b/lib/health_checks/health_checks.rb @@ -0,0 +1,37 @@ +require 'health_checkable' + +Dir[File.dirname(__FILE__) + '/checks/*.rb'].each do |file| + require file +end + +module HealthChecks + extend self + + def all + @checks ||= [] + end + + def add(check) + if assert_valid_check(check) + all << check + check + else + false + end + end + + def each(&block) + all.each(&block) + end + + def ok? + all.all? { |check| check.ok? } + end + + private + + def assert_valid_check(check) + check.respond_to?(:check) + end + +end diff --git a/lib/public_body_categories.rb b/lib/public_body_categories.rb index 7f548b130..3528e85b1 100644 --- a/lib/public_body_categories.rb +++ b/lib/public_body_categories.rb @@ -1,60 +1,11 @@ -# 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/ - +# 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 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([]) + def self.method_missing(method, *args, &block) + warn 'Use of PublicBodyCategories is deprecated and will be removed in release 0.21. Please use PublicBodyCategory instead.' + PublicBodyCategory.send(method, *args, &block) 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/lib/public_body_categories_en.rb b/lib/public_body_categories_en.rb deleted file mode 100644 index 95eed750b..000000000 --- a/lib/public_body_categories_en.rb +++ /dev/null @@ -1,19 +0,0 @@ -# The PublicBodyCategories structure works like this: -# [ -# "Main category name", -# [ "tag_to_use_as_category", "Sub category title", "sentence that can describes things in this subcategory" ], -# [ "another_tag", "Second sub category title", "another descriptive sentence for things in this subcategory"], -# "Another main category name", -# [ "another_tag_2", "Another sub category title", "another descriptive sentence"] -# ]) -# -# DO NOT EDIT THIS FILE! It should be overridden in a custom theme. -# See doc/THEMES.md for more info - -PublicBodyCategories.add(:en, [ - "Silly ministries", - [ "useless_agency", "Useless ministries", "a useless ministry" ], - [ "lonely_agency", "Lonely agencies", "a lonely agency"], - "Popular agencies", - [ "popular_agency", "Popular agencies", "a lonely agency"] -]) diff --git a/lib/routing_filters.rb b/lib/routing_filters.rb index a9a62b8db..5b5da6870 100644 --- a/lib/routing_filters.rb +++ b/lib/routing_filters.rb @@ -22,5 +22,13 @@ module RoutingFilter prepend_segment!(result, locale) if prepend_locale?(locale) end end + + # Reset the locale pattern when the locales are set. + class << self + def locales=(locales) + @@locales_pattern = nil + @@locales = locales.map(&:to_sym) + end + end end end |