aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/info_request.rb52
-rw-r--r--app/models/info_request_event.rb22
-rw-r--r--app/models/public_body.rb13
-rw-r--r--app/models/request_classification.rb16
4 files changed, 66 insertions, 37 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 6f472c290..2e16d0f58 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -35,7 +35,7 @@ class InfoRequest < ActiveRecord::Base
belongs_to :user
validate :must_be_internal_or_external
- belongs_to :public_body
+ belongs_to :public_body, :counter_cache => true
validates_presence_of :public_body_id
has_many :outgoing_messages, :order => 'created_at'
@@ -223,7 +223,7 @@ class InfoRequest < ActiveRecord::Base
incoming_message.clear_in_database_caches!
end
end
-
+
# For debugging
def InfoRequest.profile_search(query)
t = Time.now.usec
@@ -939,26 +939,54 @@ public
# Used to find when event last changed
def InfoRequest.last_event_time_clause(event_type=nil)
event_type_clause = ''
- event_type_clause = " and info_request_events.event_type = '#{event_type}'" if event_type
- "(select created_at from info_request_events where info_request_events.info_request_id = info_requests.id#{event_type_clause} order by created_at desc limit 1)"
+ event_type_clause = " AND info_request_events.event_type = '#{event_type}'" if event_type
+ "(SELECT created_at
+ FROM info_request_events
+ WHERE info_request_events.info_request_id = info_requests.id
+ #{event_type_clause}
+ ORDER BY created_at desc
+ LIMIT 1)"
end
- def InfoRequest.find_old_unclassified(extra_params={})
+ def InfoRequest.old_unclassified_params(extra_params, include_last_response_time=false)
last_response_created_at = last_event_time_clause('response')
age = extra_params[:age_in_days] ? extra_params[:age_in_days].days : OLD_AGE_IN_DAYS
- params = {:select => "*, #{last_response_created_at} as last_response_time",
- :conditions => ["awaiting_description = ? and #{last_response_created_at} < ? and url_title != 'holding_pen' and user_id is not null",
- true, Time.now() - age],
- :order => "last_response_time"}
- params[:limit] = extra_params[:limit] if extra_params[:limit]
- params[:include] = extra_params[:include] if extra_params[:include]
+ params = { :conditions => ["awaiting_description = ?
+ AND #{last_response_created_at} < ?
+ AND url_title != 'holding_pen'
+ AND user_id IS NOT NULL",
+ true, Time.now() - age] }
+ if include_last_response_time
+ params[:select] = "*, #{last_response_created_at} AS last_response_time"
+ params[:order] = 'last_response_time'
+ end
+ return params
+ end
+
+ def InfoRequest.count_old_unclassified(extra_params={})
+ params = old_unclassified_params(extra_params)
+ count(:all, params)
+ end
+
+ def InfoRequest.get_random_old_unclassified(limit)
+ params = old_unclassified_params({})
+ params[:limit] = limit
+ params[:order] = "random()"
+ find(:all, params)
+ end
+
+ def InfoRequest.find_old_unclassified(extra_params={})
+ params = old_unclassified_params(extra_params, include_last_response_time=true)
+ [:limit, :include, :offset].each do |extra|
+ params[extra] = extra_params[extra] if extra_params[extra]
+ end
if extra_params[:order]
params[:order] = extra_params[:order]
params.delete(:select)
end
if extra_params[:conditions]
condition_string = extra_params[:conditions].shift
- params[:conditions][0] += " and #{condition_string}"
+ params[:conditions][0] += " AND #{condition_string}"
params[:conditions] += extra_params[:conditions]
end
find(:all, params)
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index a827d19a4..54d2f5ef7 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -43,7 +43,7 @@ class InfoRequestEvent < ActiveRecord::Base
'resent',
'followup_sent',
'followup_resent',
-
+
'edit', # title etc. edited (in admin interface)
'edit_outgoing', # outgoing message edited (in admin interface)
'edit_comment', # comment edited (in admin interface)
@@ -53,7 +53,7 @@ class InfoRequestEvent < ActiveRecord::Base
'move_request', # changed user or public body (in admin interface)
'hide', # hid a request (in admin interface)
'manual', # you did something in the db by hand
-
+
'response',
'comment',
'status_update'
@@ -389,24 +389,6 @@ class InfoRequestEvent < ActiveRecord::Base
return TMail::Address.parse(prev_addr).address == TMail::Address.parse(curr_addr).address
end
- # Given a find condition clause, creates a league table of users who made those events.
- # XXX this isn't very generic yet, it is just used for the categorisation game tables.
- def InfoRequestEvent.make_league_table(conditions)
- status_update_events = InfoRequestEvent.find(:all, :conditions => conditions)
- table = Hash.new { |h,k| h[k] = 0 }
- for event in status_update_events
- user_id = event.params[:user_id]
- table[user_id] += 1
- end
- league_table = []
- for user_id, count in table
- user = User.find(user_id)
- league_table.push([user, count])
- end
- league_table.sort! { |a,b| b[1] <=> a[1] }
- return league_table
- end
-
def json_for_api(deep, snippet_highlight_proc = nil)
ret = {
:id => self.id,
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 60ecb2781..77da81d4c 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -45,6 +45,8 @@ class PublicBody < ActiveRecord::Base
has_many :censor_rules, :order => 'created_at desc'
has_tag_string
+ before_save :set_api_key, :set_default_publication_scheme
+
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
@@ -89,13 +91,13 @@ class PublicBody < ActiveRecord::Base
end
end
- def after_initialize
+ def set_default_publication_scheme
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
self.publication_scheme = "" if self.publication_scheme.nil?
end
- def before_save
+ def set_api_key
self.api_key = SecureRandom.base64(33) if self.api_key.nil?
end
@@ -104,7 +106,7 @@ class PublicBody < ActiveRecord::Base
locale = self.locale || I18n.locale
PublicBody.with_locale(locale) do
found = PublicBody.find(:all,
- :conditions => ["public_body_translations.url_name='#{name}'"],
+ :conditions => ["public_body_translations.url_name=?", name],
:joins => :translations,
:readonly => false)
# If many bodies are found (usually because the url_name is the same across
@@ -184,7 +186,7 @@ class PublicBody < ActiveRecord::Base
end
acts_as_versioned
- self.non_versioned_columns << 'created_at' << 'updated_at' << 'first_letter' << 'api_key'
+ self.non_versioned_columns << 'created_at' << 'updated_at' << 'first_letter' << 'api_key' << 'info_requests_count'
class Version
attr_accessor :created_at
@@ -549,9 +551,10 @@ class PublicBody < ActiveRecord::Base
def notes_as_html
self.notes
end
+
def notes_without_html
# assume notes are reasonably behaved HTML, so just use simple regexp on this
- self.notes.nil? ? '' : self.notes.gsub(/<\/?[^>]*>/, "")
+ @notes_without_html ||= (self.notes.nil? ? '' : self.notes.gsub(/<\/?[^>]*>/, ""))
end
def json_for_api
diff --git a/app/models/request_classification.rb b/app/models/request_classification.rb
new file mode 100644
index 000000000..678b6cd16
--- /dev/null
+++ b/app/models/request_classification.rb
@@ -0,0 +1,16 @@
+class RequestClassification < ActiveRecord::Base
+ belongs_to :user
+
+ # return classification instances representing the top n
+ # users, with a 'cnt' attribute representing the number
+ # of classifications the user has made.
+ def RequestClassification.league_table(size, conditions=[])
+ find(:all, :select => 'user_id, count(*) as cnt',
+ :conditions => conditions,
+ :group => 'user_id',
+ :order => 'cnt desc',
+ :limit => size,
+ :include => :user)
+ end
+
+end \ No newline at end of file