diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/about_me_validator.rb | 11 | ||||
-rw-r--r-- | app/models/censor_rule.rb | 7 | ||||
-rw-r--r-- | app/models/comment.rb | 11 | ||||
-rw-r--r-- | app/models/concerns/admin_column.rb | 17 | ||||
-rw-r--r-- | app/models/concerns/translatable.rb | 43 | ||||
-rw-r--r-- | app/models/foi_attachment.rb | 34 | ||||
-rw-r--r-- | app/models/incoming_message.rb | 8 | ||||
-rw-r--r-- | app/models/info_request.rb | 36 | ||||
-rw-r--r-- | app/models/info_request_batch.rb | 2 | ||||
-rw-r--r-- | app/models/info_request_event.rb | 23 | ||||
-rw-r--r-- | app/models/mail_server_log.rb | 2 | ||||
-rw-r--r-- | app/models/outgoing_message.rb | 7 | ||||
-rw-r--r-- | app/models/public_body.rb | 48 | ||||
-rw-r--r-- | app/models/public_body_category.rb | 40 | ||||
-rw-r--r-- | app/models/public_body_category/category_collection.rb | 8 | ||||
-rw-r--r-- | app/models/public_body_heading.rb | 38 |
16 files changed, 125 insertions, 210 deletions
diff --git a/app/models/about_me_validator.rb b/app/models/about_me_validator.rb index ecca5d07d..8465b0716 100644 --- a/app/models/about_me_validator.rb +++ b/app/models/about_me_validator.rb @@ -10,20 +10,11 @@ class AboutMeValidator attr_accessor :about_me - # TODO: Switch to built in validations - validate :length_of_about_me + validates_length_of :about_me, :maximum => 500, :message => _("Please keep it shorter than 500 characters") def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end end - - private - - def length_of_about_me - if !about_me.blank? && about_me.size > 500 - errors.add(:about_me, _("Please keep it shorter than 500 characters")) - end - end end diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index 0ef5d18bd..f1f1a0d70 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -23,6 +23,7 @@ # Email: hello@mysociety.org; WWW: http://www.mysociety.org/ class CensorRule < ActiveRecord::Base + include AdminColumn belongs_to :info_request belongs_to :user belongs_to :public_body @@ -58,12 +59,6 @@ class CensorRule < ActiveRecord::Base binary_to_censor.gsub!(to_replace) { |match| match.gsub(/./, 'x') } end - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, send(column.name), column.type.to_s, column.name) - end - end - def is_global? info_request_id.nil? && user_id.nil? && public_body_id.nil? end diff --git a/app/models/comment.rb b/app/models/comment.rb index ab4acfe56..772bb3eda 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -21,6 +21,7 @@ # Email: hello@mysociety.org; WWW: http://www.mysociety.org/ class Comment < ActiveRecord::Base + include AdminColumn strip_attributes! belongs_to :user @@ -58,10 +59,6 @@ class Comment < ActiveRecord::Base ret end - def raw_body - read_attribute(:body) - end - # So when takes changes it updates, or when made invisble it vanishes def event_xapian_update info_request_events.each { |event| event.xapian_mark_needs_index } @@ -76,12 +73,6 @@ class Comment < ActiveRecord::Base text.html_safe end - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, send(column.name), column.type.to_s, column.name) - end - end - private def check_body_has_content diff --git a/app/models/concerns/admin_column.rb b/app/models/concerns/admin_column.rb new file mode 100644 index 000000000..6e19f5aa5 --- /dev/null +++ b/app/models/concerns/admin_column.rb @@ -0,0 +1,17 @@ +module AdminColumn + extend ActiveSupport::Concern + + included do + class << self + attr_reader :non_admin_columns + end + + @non_admin_columns = [] + end + + def for_admin_column + self.class.content_columns.reject { |c| self.class.non_admin_columns.include?(c.name) }.each do |column| + yield(column.human_name, send(column.name), column.type.to_s, column.name) + end + end +end diff --git a/app/models/concerns/translatable.rb b/app/models/concerns/translatable.rb new file mode 100644 index 000000000..bc89e4c3b --- /dev/null +++ b/app/models/concerns/translatable.rb @@ -0,0 +1,43 @@ +module Translatable + extend ActiveSupport::Concern + + included do + accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params? + end + + def find_translation_by_locale(locale) + translations.find_by_locale(locale) + end + + def translated_versions + translations + end + + def ordered_translations + translations.select do |translation| + I18n.available_locales.include?(translation.locale) + end.sort_by do |translation| + I18n.available_locales.index(translation.locale) + end + end + + def build_all_translations + I18n.available_locales.each do |locale| + if translations.none? { |translation| translation.locale == locale } + translations.build(:locale => locale) + end + end + end + + def translated_versions=(translation_attrs) + warn "[DEPRECATION] #{self.class.name}#translated_versions= will be replaced " \ + "by #{self.class.name}#translations_attributes= as of release 0.22" + self.translations_attributes = translation_attrs + end + + private + + def empty_translation_in_params?(attributes) + attributes.select { |k, v| v.present? && k.to_s != 'locale' }.empty? + end +end diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb index d1c30672f..0af47b26e 100644 --- a/app/models/foi_attachment.rb +++ b/app/models/foi_attachment.rb @@ -244,36 +244,32 @@ class FoiAttachment < ActiveRecord::Base # The full list of supported types can be found at # https://docs.google.com/support/bin/answer.py?hl=en&answer=1189935 def has_google_docs_viewer? - return !! { - "application/pdf" => true, # .pdf - "image/tiff" => true, # .tiff + [ + "application/pdf", # .pdf + "image/tiff", # .tiff - "application/vnd.ms-word" => true, # .doc - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => true, # .docx + "application/vnd.ms-word", # .doc + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx - "application/vnd.ms-powerpoint" => true, # .ppt - "application/vnd.openxmlformats-officedocument.presentationml.presentation" => true, # .pptx + "application/vnd.ms-powerpoint", # .ppt + "application/vnd.openxmlformats-officedocument.presentationml.presentation", # .pptx - "application/vnd.ms-excel" => true, # .xls - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => true, # .xlsx - - } [self.content_type] + "application/vnd.ms-excel", # .xls + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # .xlsx + ].include?(content_type) end # Whether this type has a "View as HTML" def has_body_as_html? - return ( - !!{ - "text/plain" => true, - "application/rtf" => true, - }[self.content_type] or - self.has_google_docs_viewer? - ) + [ + "text/plain", + "application/rtf", + ].include?(content_type) || has_google_docs_viewer? end # Name of type of attachment type - only valid for things that has_body_as_html? def name_of_content_type - return { + { "text/plain" => "Text file", 'application/rtf' => "RTF file", diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index a74056b5d..986eb19f5 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -38,6 +38,7 @@ require 'zip/zip' require 'iconv' unless String.method_defined?(:encode) class IncomingMessage < ActiveRecord::Base + include AdminColumn extend MessageProminence belongs_to :info_request validates_presence_of :info_request @@ -754,13 +755,6 @@ class IncomingMessage < ActiveRecord::Base def IncomingMessage.get_all_file_extensions return AlaveteliFileTypes.all_extensions.join(" ") end - - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) - end - end - end diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 56d16c2ff..bef5d1893 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -28,8 +28,11 @@ require 'digest/sha1' class InfoRequest < ActiveRecord::Base + include AdminColumn include Rails.application.routes.url_helpers + @non_admin_columns = %w(title url_title) + strip_attributes! validates_presence_of :title, :message => N_("Please enter a summary of your request") @@ -550,7 +553,7 @@ public :status => 'ready', :message_type => 'initial_request', :body => 'This is the holding pen request. It shows responses that were sent to invalid addresses, and need moving to the correct request by an adminstrator.', - :last_sent_at => Time.now(), + :last_sent_at => Time.now, :what_doing => 'normal_sort' }) @@ -666,11 +669,11 @@ public if !curr_state.nil? && event.event_type == 'response' if event.calculated_state != curr_state event.calculated_state = curr_state - event.last_described_at = Time.now() + event.last_described_at = Time.now event.save! end if event.last_described_at.nil? # TODO: actually maybe this isn't needed - event.last_described_at = Time.now() + event.last_described_at = Time.now event.save! end curr_state = nil @@ -682,7 +685,7 @@ public # indexed. if event.calculated_state != event.described_state event.calculated_state = event.described_state - event.last_described_at = Time.now() + event.last_described_at = Time.now event.save! end @@ -699,7 +702,7 @@ public # case there is a preceding response that the described state should be applied to. if event.calculated_state != event.described_state event.calculated_state = event.described_state - event.last_described_at = Time.now() + event.last_described_at = Time.now event.save! end end @@ -992,20 +995,20 @@ public LIMIT 1)" end - def InfoRequest.last_public_response_clause() + def InfoRequest.last_public_response_clause join_clause = "incoming_messages.id = info_request_events.incoming_message_id AND incoming_messages.prominence = 'normal'" last_event_time_clause('response', 'incoming_messages', join_clause) end def InfoRequest.old_unclassified_params(extra_params, include_last_response_time=false) - last_response_created_at = last_public_response_clause() + last_response_created_at = last_public_response_clause age = extra_params[:age_in_days] ? extra_params[:age_in_days].days : OLD_AGE_IN_DAYS params = { :conditions => ["awaiting_description = ? AND #{last_response_created_at} < ? AND url_title != 'holding_pen' AND user_id IS NOT NULL", - true, Time.now() - age] } + true, Time.now - age] } if include_last_response_time params[:select] = "*, #{last_response_created_at} AS last_response_time" params[:order] = 'last_response_time' @@ -1040,7 +1043,7 @@ public find(:all, params) end - def InfoRequest.download_zip_dir() + def InfoRequest.download_zip_dir File.join(Rails.root, "cache", "zips", "#{Rails.env}") end @@ -1058,7 +1061,7 @@ public end def request_dirs - first_three_digits = id.to_s()[0..2] + first_three_digits = id.to_s[0..2] File.join(first_three_digits.to_s, id.to_s) end @@ -1067,7 +1070,7 @@ public end def make_zip_cache_path(user) - cache_file_dir = File.join(InfoRequest.download_zip_dir(), + cache_file_dir = File.join(InfoRequest.download_zip_dir, "download", request_dirs, last_update_hash) @@ -1240,7 +1243,7 @@ public :model => self.class.base_class.to_s, :model_id => self.id) end - req.save() + req.save end end @@ -1270,13 +1273,6 @@ public PublicBody.set_callback(:save, :after, :purge_in_cache) end - def for_admin_column - self.class.content_columns.map{|c| c unless %w(title url_title).include?(c.name) }.compact.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) - end - end - - # Get requests that have similar important terms def similar_requests(limit=10) xapian_similar = nil @@ -1403,7 +1399,7 @@ public self.described_state = 'waiting_response' end rescue ActiveModel::MissingAttributeError - # this should only happen on Model.exists?() call. It can be safely ignored. + # this should only happen on Model.exists? call. It can be safely ignored. # See http://www.tatvartha.com/2011/03/activerecordmissingattributeerror-missing-attribute-a-bug-or-a-features/ end diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 5a20edce8..b831f3042 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -70,7 +70,7 @@ class InfoRequestBatch < ActiveRecord::Base info_request end - def InfoRequestBatch.send_batches() + def InfoRequestBatch.send_batches find_each(:conditions => "sent_at IS NULL") do |info_request_batch| unrequestable = info_request_batch.create_batch! mail_message = InfoRequestBatchMailer.batch_sent(info_request_batch, diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index 75978fdc5..29c1ce965 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -22,7 +22,7 @@ # Email: hello@mysociety.org; WWW: http://www.mysociety.org/ class InfoRequestEvent < ActiveRecord::Base - + include AdminColumn extend XapianQueries belongs_to :info_request @@ -326,9 +326,17 @@ class InfoRequestEvent < ActiveRecord::Base end - def is_incoming_message?() not self.incoming_message_selective_columns("incoming_messages.id").nil? end - def is_outgoing_message?() not self.outgoing_message.nil? end - def is_comment?() not self.comment.nil? end + def is_incoming_message? + !self.incoming_message_selective_columns("incoming_messages.id").nil? + end + + def is_outgoing_message? + !self.outgoing_message.nil? + end + + def is_comment? + !self.comment.nil? + end # Display version of status def display_status @@ -419,11 +427,4 @@ class InfoRequestEvent < ActiveRecord::Base return ret end - - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) - end - end - end diff --git a/app/models/mail_server_log.rb b/app/models/mail_server_log.rb index 1c69635f3..ca3ea5d19 100644 --- a/app/models/mail_server_log.rb +++ b/app/models/mail_server_log.rb @@ -178,7 +178,7 @@ class MailServerLog < ActiveRecord::Base # Get all requests sent for from 2 to 10 days ago. The 2 day gap is # because we load mail server log lines via cron at best an hour after they # are made) - irs = InfoRequest.find(:all, :conditions => [ "created_at < ? and created_at > ? and user_id is not null", Time.now() - 2.day, Time.now() - 10.days ] ) + irs = InfoRequest.find(:all, :conditions => [ "created_at < ? and created_at > ? and user_id is not null", Time.now - 2.day, Time.now - 10.days ] ) # Go through each request and check it ok = true diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb index 7bb8ce8be..2e1b27fba 100644 --- a/app/models/outgoing_message.rb +++ b/app/models/outgoing_message.rb @@ -26,6 +26,7 @@ # Email: hello@mysociety.org; WWW: http://www.mysociety.org/ class OutgoingMessage < ActiveRecord::Base + include AdminColumn extend MessageProminence include Rails.application.routes.url_helpers include LinkToHelper @@ -283,12 +284,6 @@ class OutgoingMessage < ActiveRecord::Base info_request.purge_in_cache end - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) - end - end - def xapian_reindex_after_update if changes.include?('body') info_request_events.each do |event| diff --git a/app/models/public_body.rb b/app/models/public_body.rb index fc93d2348..103d86bb4 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -32,6 +32,10 @@ require 'securerandom' require 'set' class PublicBody < ActiveRecord::Base + include AdminColumn + + @non_admin_columns = %w(name last_edit_comment) + strip_attributes! validates_presence_of :name, :message => N_("Name can't be blank") @@ -64,7 +68,6 @@ class PublicBody < ActiveRecord::Base } translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme - accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params? # Default fields available for importing from CSV, in the format # [field_name, 'short description of field (basic html allowed)'] @@ -96,6 +99,8 @@ class PublicBody < ActiveRecord::Base self.non_versioned_columns << 'info_requests_not_held_count' << 'info_requests_overdue' self.non_versioned_columns << 'info_requests_overdue_count' + include Translatable + # Public: Search for Public Bodies whose name, short_name, request_email or # tags contain the given query # @@ -124,11 +129,6 @@ class PublicBody < ActiveRecord::Base uniq end - # Convenience methods for creating/editing translations via forms - def find_translation_by_locale(locale) - self.translations.find_by_locale(locale) - end - # TODO: - Don't like repeating this! def calculate_cached_fields(t) PublicBody.set_first_letter(t) @@ -148,28 +148,6 @@ class PublicBody < ActiveRecord::Base end end - def translated_versions - translations - end - - def ordered_translations - translations. - select { |t| I18n.available_locales.include?(t.locale) }. - sort_by { |t| I18n.available_locales.index(t.locale) } - end - - def build_all_translations - I18n.available_locales.each do |locale| - translations.build(:locale => locale) unless translations.detect{ |t| t.locale == locale } - end - end - - def translated_versions=(translation_attrs) - warn "[DEPRECATION] PublicBody#translated_versions= will be replaced " \ - "by PublicBody#translations_attributes= as of release 0.22" - self.translations_attributes = translation_attrs - end - 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) @@ -409,7 +387,7 @@ class PublicBody < ActiveRecord::Base # matching names won't work afterwards, and we'll create new bodies instead # of updating them bodies_by_name = {} - set_of_existing = Set.new() + set_of_existing = Set.new internal_admin_body_id = PublicBody.internal_admin_body.id I18n.with_locale(I18n.default_locale) do bodies = (tag.nil? || tag.empty?) ? PublicBody.find(:all, :include => :translations) : PublicBody.find_by_tag(tag) @@ -422,7 +400,7 @@ class PublicBody < ActiveRecord::Base end end - set_of_importing = Set.new() + set_of_importing = Set.new # Default values in case no field list is given field_names = { 'name' => 1, 'request_email' => 2 } line = 0 @@ -647,12 +625,6 @@ class PublicBody < ActiveRecord::Base self.info_requests.each {|x| x.purge_in_cache} end - def for_admin_column - self.class.content_columns.map{|c| c unless %w(name last_edit_comment).include?(c.name)}.compact.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) - end - end - def self.where_clause_for_stats(minimum_requests, total_column) # When producing statistics for public bodies, we want to # exclude any that are tagged with 'test' - we use a @@ -764,10 +736,6 @@ class PublicBody < ActiveRecord::Base end end - def empty_translation_in_params?(attributes) - attributes.select { |k, v| !v.blank? && k.to_s != 'locale' }.empty? - end - def request_email_if_requestable # Request_email can be blank, meaning we don't have details if self.is_requestable? diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb index af7711c3a..0a64172c1 100644 --- a/app/models/public_body_category.rb +++ b/app/models/public_body_category.rb @@ -18,13 +18,14 @@ class PublicBodyCategory < ActiveRecord::Base has_many :public_body_headings, :through => :public_body_category_links translates :title, :description - accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params? validates_uniqueness_of :category_tag, :message => 'Tag is already taken' validates_presence_of :title, :message => "Title can't be blank" validates_presence_of :category_tag, :message => "Tag can't be blank" validates_presence_of :description, :message => "Description can't be blank" + include Translatable + def self.get locale = I18n.locale.to_s || default_locale.to_s || "" categories = CategoryCollection.new @@ -52,43 +53,6 @@ class PublicBodyCategory < ActiveRecord::Base ) | PublicBodyCategory.find_by_sql(sql) end - - # Convenience methods for creating/editing translations via forms - def find_translation_by_locale(locale) - translations.find_by_locale(locale) - end - - def translated_versions - translations - end - - def translated_versions=(translation_attrs) - warn "[DEPRECATION] PublicBodyCategory#translated_versions= will be replaced " \ - "by PublicBodyCategory#translations_attributes= as of release 0.22" - self.translations_attributes = translation_attrs - end - - def ordered_translations - translations. - select { |t| I18n.available_locales.include?(t.locale) }. - sort_by { |t| I18n.available_locales.index(t.locale) } - end - - def build_all_translations - I18n.available_locales.each do |locale| - translations.build(:locale => locale) unless translations.detect{ |t| t.locale == locale } - end - end - - private - - def empty_translation_in_params?(attributes) - attrs_with_values = attributes.select do |key, value| - value != '' and key.to_s != 'locale' - end - attrs_with_values.empty? - end - end PublicBodyCategory::Translation.class_eval do diff --git a/app/models/public_body_category/category_collection.rb b/app/models/public_body_category/category_collection.rb index 3938722c0..7d5732a82 100644 --- a/app/models/public_body_category/category_collection.rb +++ b/app/models/public_body_category/category_collection.rb @@ -14,19 +14,19 @@ class PublicBodyCategory::CategoryCollection end def with_description - @categories.select() { |a| a.instance_of?(Array) } + @categories.select { |a| a.instance_of?(Array) } end def tags - tags = with_description.map() { |a| a[0] } + tags = with_description.map { |a| a[0] } end def by_tag - Hash[*with_description.map() { |a| a[0..1] }.flatten] + Hash[*with_description.map { |a| a[0..1] }.flatten] end def singular_by_tag - Hash[*with_description.map() { |a| [a[0],a[2]] }.flatten] + Hash[*with_description.map { |a| [a[0],a[2]] }.flatten] end def by_heading diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb index 514821e2c..d49b388bb 100644 --- a/app/models/public_body_heading.rb +++ b/app/models/public_body_heading.rb @@ -16,7 +16,6 @@ class PublicBodyHeading < ActiveRecord::Base default_scope order('display_order ASC') translates :name - accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params? validates_uniqueness_of :name, :message => 'Name is already taken' validates_presence_of :name, :message => 'Name can\'t be blank' @@ -29,32 +28,7 @@ class PublicBodyHeading < ActiveRecord::Base end end - # Convenience methods for creating/editing translations via forms - def find_translation_by_locale(locale) - translations.find_by_locale(locale) - end - - def translated_versions - translations - end - - def translated_versions=(translation_attrs) - warn "[DEPRECATION] PublicBodyHeading#translated_versions= will be replaced " \ - "by PublicBodyHeading#translations_attributes= as of release 0.22" - self.translations_attributes = translation_attrs - end - - def ordered_translations - translations. - select { |t| I18n.available_locales.include?(t.locale) }. - sort_by { |t| I18n.available_locales.index(t.locale) } - end - - def build_all_translations - I18n.available_locales.each do |locale| - translations.build(:locale => locale) unless translations.detect{ |t| t.locale == locale } - end - end + include Translatable def add_category(category) unless public_body_categories.include?(category) @@ -69,14 +43,4 @@ class PublicBodyHeading < ActiveRecord::Base 0 end end - - private - - def empty_translation_in_params?(attributes) - attrs_with_values = attributes.select do |key, value| - value != '' and key.to_s != 'locale' - end - attrs_with_values.empty? - end - end |