aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2013-09-24 11:29:31 +0100
committerLouise Crow <louise.crow@gmail.com>2013-09-24 11:29:31 +0100
commitd8deb8418b4cd26c68eb1301959e156c19b111e2 (patch)
tree99c346db95d17be9c5105ce47d5f3ac8e943e952 /lib
parent8459314b691f5b02277035219cd58f510d100a77 (diff)
parent75542416a1cc36b353ade557b1bc4f729b02423a (diff)
Merge branch 'release/0.14'0.14
Conflicts: locale/bg/app.po locale/fr/app.po locale/fr_CA/app.po locale/he_IL/app.po locale/hr/app.po locale/it/app.po locale/nb_NO/app.po locale/pl/app.po locale/sv/app.po locale/vi/app.po
Diffstat (limited to 'lib')
-rw-r--r--lib/ability.rb13
-rw-r--r--lib/confidence_intervals.rb31
-rw-r--r--lib/configuration.rb3
-rw-r--r--lib/i18n_fixes.rb105
-rw-r--r--lib/message_prominence.rb26
-rw-r--r--lib/tasks/gettext.rake42
-rw-r--r--lib/tasks/stats.rake26
7 files changed, 121 insertions, 125 deletions
diff --git a/lib/ability.rb b/lib/ability.rb
index 2865ccb1c..f63845e84 100644
--- a/lib/ability.rb
+++ b/lib/ability.rb
@@ -2,4 +2,15 @@ module Ability
def self.can_update_request_state?(user, request)
(user && request.is_old_unclassified?) || request.is_owning_user?(user)
end
-end \ No newline at end of file
+
+ def self.can_view_with_prominence?(prominence, info_request, user)
+ if prominence == 'hidden'
+ return User.view_hidden?(user)
+ end
+ if prominence == 'requester_only'
+ return info_request.is_owning_user?(user)
+ end
+ return true
+ end
+
+end
diff --git a/lib/confidence_intervals.rb b/lib/confidence_intervals.rb
new file mode 100644
index 000000000..9fe38045a
--- /dev/null
+++ b/lib/confidence_intervals.rb
@@ -0,0 +1,31 @@
+# Calculate the confidence interval for a samples from a binonial
+# distribution using Wilson's score interval. For more theoretical
+# details, please see:
+#
+# http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson%20score%20interval
+#
+# This is a variant of the function suggested here:
+#
+# http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
+#
+# total: the total number of observations
+# successes: the subset of those observations that were "successes"
+# power: for a 95% confidence interval, this should be 0.05
+#
+# The naive proportion is (successes / total). This returns an array
+# with the proportions that represent the lower and higher confidence
+# intervals around that.
+
+require 'statistics2'
+
+def ci_bounds(successes, total, power)
+ if total == 0
+ raise RuntimeError, "Can't calculate the CI for 0 observations"
+ end
+ z = Statistics2.pnormaldist(1 - power/2)
+ phat = successes.to_f/total
+ offset = z*Math.sqrt((phat*(1 - phat) + z*z/(4*total))/total)
+ denominator = 1 + z*z/total
+ return [(phat + z*z/(2*total) - offset)/denominator,
+ (phat + z*z/(2*total) + offset)/denominator]
+end
diff --git a/lib/configuration.rb b/lib/configuration.rb
index 03c4ac616..ab985c8bf 100644
--- a/lib/configuration.rb
+++ b/lib/configuration.rb
@@ -43,10 +43,13 @@ module AlaveteliConfiguration
:INCOMING_EMAIL_PREFIX => '',
:INCOMING_EMAIL_SECRET => 'dummysecret',
:ISO_COUNTRY_CODE => 'GB',
+ :MINIMUM_REQUESTS_FOR_STATISTICS => 100,
:MAX_REQUESTS_PER_USER_PER_DAY => '',
:MTA_LOG_TYPE => 'exim',
:NEW_RESPONSE_REMINDER_AFTER_DAYS => [3, 10, 24],
:OVERRIDE_ALL_PUBLIC_BODY_REQUEST_EMAILS => '',
+ :PUBLIC_BODY_STATISTICS_PAGE => false,
+ :PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE => false,
:RAW_EMAILS_LOCATION => 'files/raw_emails',
:READ_ONLY => '',
:RECAPTCHA_PRIVATE_KEY => 'x',
diff --git a/lib/i18n_fixes.rb b/lib/i18n_fixes.rb
index 9c1206215..9f0849e75 100644
--- a/lib/i18n_fixes.rb
+++ b/lib/i18n_fixes.rb
@@ -49,111 +49,6 @@ def gettext_interpolate(string, values)
end
-module I18n
- # used by Globalize plugin.
- # XXX much of this stuff should (might?) be in newer versions of Rails
- @@fallbacks = nil
- class << self
- # Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+.
- def fallbacks
- @@fallbacks ||= I18n::Locale::Fallbacks.new
- end
- end
-
- module Locale
- module Tag
- class Simple
- class << self
- def tag(tag)
- new(tag)
- end
- end
-
- attr_reader :tag
-
- def initialize(*tag)
- @tag = tag.join('-').to_sym
- end
-
- def subtags
- @subtags = tag.to_s.split('-').map { |subtag| subtag.to_s }
- end
-
- def to_sym
- tag
- end
-
- def to_s
- tag.to_s
- end
-
- def to_a
- subtags
- end
-
- def parent
- @parent ||= begin
- segs = to_a.compact
- segs.length > 1 ? self.class.tag(*segs[0..(segs.length-2)].join('-')) : nil
- end
- end
-
- def self_and_parents
- @self_and_parents ||= [self] + parents
- end
-
- def parents
- @parents ||= ([parent] + (parent ? parent.parents : [])).compact
- end
-
-
- end
- end
- class Fallbacks < Hash
- def initialize(*mappings)
- @map = {}
- map(mappings.pop) if mappings.last.is_a?(Hash)
- self.defaults = mappings.empty? ? [I18n.default_locale.to_sym] : mappings
- end
-
- def defaults=(defaults)
- @defaults = defaults.map { |default| compute(default, false) }.flatten
- end
- attr_reader :defaults
-
- def [](locale)
- raise InvalidLocale.new(locale) if locale.nil?
- locale = locale.to_sym
- super || store(locale, compute(locale))
- end
-
- def map(mappings)
- mappings.each do |from, to|
- from, to = from.to_sym, Array(to)
- to.each do |_to|
- @map[from] ||= []
- @map[from] << _to.to_sym
- end
- end
- end
-
- protected
-
- def compute(tags, include_defaults = true)
- result = Array(tags).collect do |tag|
- tags = I18n::Locale::Tag::Simple.tag(tag).self_and_parents.map! { |t| t.to_sym }
- tags.each { |_tag| tags += compute(@map[_tag]) if @map[_tag] }
- tags
- end.flatten
- result.push(*defaults) if include_defaults
- result.uniq.compact
- end
- end
- autoload :Fallbacks, 'i18n/locale/fallbacks'
- end
-end
-
-
# this monkeypatch corrects inconsistency with gettext_i18n_rails
# where the latter deals with strings but rails i18n deals with
# symbols for locales
diff --git a/lib/message_prominence.rb b/lib/message_prominence.rb
new file mode 100644
index 000000000..8f54fcc95
--- /dev/null
+++ b/lib/message_prominence.rb
@@ -0,0 +1,26 @@
+module MessageProminence
+
+ def has_prominence
+ send :include, InstanceMethods
+ cattr_accessor :prominence_states
+ self.prominence_states = ['normal', 'hidden','requester_only']
+ validates_inclusion_of :prominence, :in => self.prominence_states
+ end
+
+ module InstanceMethods
+
+ def user_can_view?(user)
+ Ability.can_view_with_prominence?(self.prominence, self.info_request, user)
+ end
+
+ def indexed_by_search?
+ self.prominence == 'normal'
+ end
+
+ def all_can_view?
+ self.prominence == 'normal'
+ end
+
+ end
+end
+
diff --git a/lib/tasks/gettext.rake b/lib/tasks/gettext.rake
index ace7205ae..366dfbe88 100644
--- a/lib/tasks/gettext.rake
+++ b/lib/tasks/gettext.rake
@@ -9,27 +9,31 @@ namespace :gettext do
end
end
- desc "Update pot file only, without fuzzy guesses (these are done by Transifex)"
- task :findpot => :environment do
+ desc "Update pot/po files for a theme."
+ task :find_theme => :environment do
+ theme = ENV['THEME']
+ unless theme
+ puts "Usage: Specify an Alaveteli-theme with THEME=[theme directory name]"
+ exit(0)
+ end
load_gettext
- $LOAD_PATH << File.join(File.dirname(__FILE__),'..','..','lib')
- require 'gettext_i18n_rails/haml_parser'
- files = files_to_translate
+ msgmerge = Rails.application.config.gettext_i18n_rails.msgmerge
+ msgmerge ||= %w[--sort-output --no-location --no-wrap]
+ GetText.update_pofiles_org(
+ text_domain,
+ theme_files_to_translate(theme),
+ "version 0.0.1",
+ :po_root => theme_locale_path(theme),
+ :msgmerge => msgmerge
+ )
+ end
- #write found messages to tmp.pot
- temp_pot = "tmp.pot"
- GetText::rgettext(files, temp_pot)
+ def theme_files_to_translate(theme)
+ Dir.glob("{vendor/plugins/#{theme}/lib}/**/*.{rb,erb}")
+ end
- #merge tmp.pot and existing pot
- FileUtils.mkdir_p('locale')
- GetText::msgmerge("locale/app.pot", temp_pot, "alaveteli", :po_root => 'locale', :msgmerge=>[ :no_wrap, :sort_output ])
- Dir.glob("locale/*/app.po") do |po_file|
- GetText::msgmerge(po_file, temp_pot, "alaveteli", :po_root => 'locale', :msgmerge=>[ :no_wrap, :sort_output ])
- end
- File.delete(temp_pot)
- end
+ def theme_locale_path(theme)
+ File.join(Rails.root, "vendor", "plugins", theme, "locale-theme")
+ end
- def files_to_translate
- Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml,rhtml}")
- end
end
diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake
index 9d7d70540..4eda27289 100644
--- a/lib/tasks/stats.rake
+++ b/lib/tasks/stats.rake
@@ -91,4 +91,30 @@ namespace :stats do
end
end
+ desc 'Update statistics in the public_bodies table'
+ task :update_public_bodies_stats => :environment do
+ verbose = ENV['VERBOSE'] == '1'
+ PublicBody.all.each do |public_body|
+ puts "Counting overdue requests for #{public_body.name}" if verbose
+
+ # Look for values of 'waiting_response_overdue' and
+ # 'waiting_response_very_overdue' which aren't directly in the
+ # described_state column, and instead need to be calculated:
+ overdue_count = 0
+ very_overdue_count = 0
+ InfoRequest.find_each(:conditions => {:public_body_id => public_body.id}) do |ir|
+ case ir.calculate_status
+ when 'waiting_response_very_overdue'
+ very_overdue_count += 1
+ when 'waiting_response_overdue'
+ overdue_count += 1
+ end
+ end
+ public_body.info_requests_overdue_count = overdue_count + very_overdue_count
+ public_body.no_xapian_reindex = true
+ public_body.without_revision do
+ public_body.save!
+ end
+ end
+ end
end