diff options
-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/incoming_message.rb | 8 | ||||
-rw-r--r-- | app/models/info_request.rb | 10 | ||||
-rw-r--r-- | app/models/info_request_event.rb | 9 | ||||
-rw-r--r-- | app/models/outgoing_message.rb | 7 | ||||
-rw-r--r-- | app/models/public_body.rb | 10 | ||||
-rw-r--r-- | config/application.rb | 1 | ||||
-rw-r--r-- | lib/attachment_to_html/adapter.rb | 67 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/could_not_convert.rb | 41 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/google_docs_viewer.rb | 38 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/pdf.rb | 50 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/rtf.rb | 50 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/text.rb | 41 | ||||
-rw-r--r-- | lib/attachment_to_html/attachment_to_html.rb | 2 |
16 files changed, 117 insertions, 252 deletions
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/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 65099b1c4..38627df50 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") @@ -1282,13 +1285,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 diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index 75978fdc5..285e54f9a 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 @@ -419,11 +419,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/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..5cd82f8d7 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") @@ -647,12 +651,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 diff --git a/config/application.rb b/config/application.rb index 08f852089..eccf0937c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -71,6 +71,7 @@ module Alaveteli app.routes.append{ match '*path', :to => 'general#not_found' } end + config.autoload_paths << "#{Rails.root.to_s}/app/models/concerns" config.autoload_paths << "#{Rails.root.to_s}/lib/mail_handler" config.autoload_paths << "#{Rails.root.to_s}/lib/attachment_to_html" config.autoload_paths << "#{Rails.root.to_s}/lib/health_checks" diff --git a/lib/attachment_to_html/adapter.rb b/lib/attachment_to_html/adapter.rb new file mode 100644 index 000000000..d8b9f41f7 --- /dev/null +++ b/lib/attachment_to_html/adapter.rb @@ -0,0 +1,67 @@ +module AttachmentToHTML + class Adapter + attr_reader :attachment + + # Public: Initialize a converter + # + # attachment - the FoiAttachment to convert to HTML + # opts - a Hash of options (default: {}): + # No options currently accepted + def initialize(attachment, opts = {}) + @attachment = attachment + end + + # Public: The title to use in the <title> tag + # + # Returns a String + def title + @title ||= attachment.display_filename + end + + # Public: The contents of the extracted html <body> tag + # + # Returns a String + def body + @body ||= parse_body + end + + def parse_body + convert + end + + # Public: Was the document conversion successful? + # + # Returns true + def success? + true + end + + def has_content? + !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? + end + + def contains_images? + !!body.match(/<img[^>]*>/mi) + end + + def create_tempfile(text) + tempfile = if RUBY_VERSION.to_f >= 1.9 + Tempfile.new('foiextract', '.', :encoding => text.encoding) + else + Tempfile.new('foiextract', '.') + end + tempfile.print(text) + tempfile.flush + tempfile + end + + def cleanup_tempfile(tempfile) + tempfile.close + tempfile.delete + end + + def attachment_body + @attachment_body ||= attachment.body + end + end +end diff --git a/lib/attachment_to_html/adapters/could_not_convert.rb b/lib/attachment_to_html/adapters/could_not_convert.rb index f23583590..745a54114 100644 --- a/lib/attachment_to_html/adapters/could_not_convert.rb +++ b/lib/attachment_to_html/adapters/could_not_convert.rb @@ -1,50 +1,15 @@ # -*- encoding : utf-8 -*- module AttachmentToHTML module Adapters - class CouldNotConvert - - attr_reader :attachment - - # Public: Initialize a PDF converter - # - # attachment - the FoiAttachment to convert to HTML - # opts - a Hash of options (default: {}): - # No options currently accepted - def initialize(attachment, opts = {}) - @attachment = attachment - end - - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - - - # Public: Was the document conversion successful? - # As this is a fallback option and not doing anything dynamic - # we're assuming this is successful whatever the case - # - # Returns true - def success? - true - end - + # As this is a fallback option and not doing anything dynamic + # we're assuming this is successful whatever the case + class CouldNotConvert < Adapter private def parse_body "<p>Sorry, we were unable to convert this file to HTML. " \ "Please use the download link at the top right.</p>" end - end end end diff --git a/lib/attachment_to_html/adapters/google_docs_viewer.rb b/lib/attachment_to_html/adapters/google_docs_viewer.rb index 343a89791..0817d08fd 100644 --- a/lib/attachment_to_html/adapters/google_docs_viewer.rb +++ b/lib/attachment_to_html/adapters/google_docs_viewer.rb @@ -2,9 +2,13 @@ module AttachmentToHTML module Adapters # Renders the attachment in a Google Docs Viewer - class GoogleDocsViewer - - attr_reader :attachment, :attachment_url + # + # We can't really tell whether the document conversion has been + # successful as such; We're assuming that given a correctly + # constructed iframe (which is tested) that Google will make this + # Just Work. + class GoogleDocsViewer < Adapter + attr_reader :attachment_url # Public: Initialize a GoogleDocsViewer converter # @@ -13,35 +17,10 @@ module AttachmentToHTML # :attachment_url - a String url to the attachment for # Google to render (default: nil) def initialize(attachment, opts = {}) - @attachment = attachment + super @attachment_url = opts.fetch(:attachment_url, nil) end - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - - # Public: Was the document conversion successful? - # We can't really tell whether the document conversion has been - # successful as such; We're assuming that given a correctly - # constructed iframe (which is tested) that Google will make this - # Just Work. - # - # Returns true - def success? - true - end - private def parse_body @@ -51,7 +30,6 @@ module AttachmentToHTML def protocol AlaveteliConfiguration.force_ssl ? 'https' : 'http' end - end end end diff --git a/lib/attachment_to_html/adapters/pdf.rb b/lib/attachment_to_html/adapters/pdf.rb index f99bdc78b..afc8fbcb0 100644 --- a/lib/attachment_to_html/adapters/pdf.rb +++ b/lib/attachment_to_html/adapters/pdf.rb @@ -2,10 +2,10 @@ module AttachmentToHTML module Adapters # Convert application/pdf documents in to HTML - class PDF + class PDF < Adapter TOO_MANY_IMAGES = 51 - attr_reader :attachment, :tmpdir + attr_reader :tmpdir # Public: Initialize a PDF converter # @@ -14,24 +14,10 @@ module AttachmentToHTML # :tmpdir - String name of directory to store the # converted document def initialize(attachment, opts = {}) - @attachment = attachment + super @tmpdir = opts.fetch(:tmpdir, ::Rails.root.join('tmp')) end - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - # Public: Was the document conversion successful? # # Returns a Boolean @@ -48,14 +34,6 @@ module AttachmentToHTML match ? match[1] : '' end - def has_content? - !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? - end - - def contains_images? - body.match(/<img[^>]*>/mi) ? true : false - end - # Works around https://bugs.freedesktop.org/show_bug.cgi?id=77932 in pdftohtml def contains_too_many_images? number_of_images_in_body >= TOO_MANY_IMAGES @@ -82,28 +60,6 @@ module AttachmentToHTML html end end - - def create_tempfile(text) - tempfile = if RUBY_VERSION.to_f >= 1.9 - Tempfile.new('foiextract', '.', - :encoding => text.encoding) - else - Tempfile.new('foiextract', '.') - end - tempfile.print(text) - tempfile.flush - tempfile - end - - def cleanup_tempfile(tempfile) - tempfile.close - tempfile.delete - end - - def attachment_body - @attachment_body ||= attachment.body - end - end end end diff --git a/lib/attachment_to_html/adapters/rtf.rb b/lib/attachment_to_html/adapters/rtf.rb index 90478d9ca..4a08bf618 100644 --- a/lib/attachment_to_html/adapters/rtf.rb +++ b/lib/attachment_to_html/adapters/rtf.rb @@ -2,9 +2,9 @@ module AttachmentToHTML module Adapters # Convert application/rtf documents in to HTML - class RTF + class RTF < Adapter - attr_reader :attachment, :tmpdir + attr_reader :tmpdir # Public: Initialize a RTF converter # @@ -13,24 +13,10 @@ module AttachmentToHTML # :tmpdir - String name of directory to store the # converted document def initialize(attachment, opts = {}) - @attachment = attachment + super @tmpdir = opts.fetch(:tmpdir, ::Rails.root.join('tmp')) end - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - # Public: Was the document conversion successful? # # Returns a Boolean @@ -45,14 +31,6 @@ module AttachmentToHTML match ? match[1] : '' end - def has_content? - !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? - end - - def contains_images? - body.match(/<img[^>]*>/mi) ? true : false - end - def convert # Get the attachment body outside of the chdir call as getting # the body may require opening files too @@ -83,28 +61,6 @@ module AttachmentToHTML end html end - - def create_tempfile(text) - tempfile = if RUBY_VERSION.to_f >= 1.9 - Tempfile.new('foiextract', '.', - :encoding => text.encoding) - else - Tempfile.new('foiextract', '.') - end - tempfile.print(text) - tempfile.flush - tempfile - end - - def cleanup_tempfile(tempfile) - tempfile.close - tempfile.delete - end - - def attachment_body - @attachment_body ||= attachment.body - end - end end end diff --git a/lib/attachment_to_html/adapters/text.rb b/lib/attachment_to_html/adapters/text.rb index 7e22422e2..61e4e57a8 100644 --- a/lib/attachment_to_html/adapters/text.rb +++ b/lib/attachment_to_html/adapters/text.rb @@ -2,33 +2,7 @@ module AttachmentToHTML module Adapters # Convert text/plain documents in to HTML - class Text - - attr_reader :attachment - - # Public: Initialize a Text converter - # - # attachment - the FoiAttachment to convert to HTML - # opts - a Hash of options (default: {}): - # No options currently accepted - def initialize(attachment, opts = {}) - @attachment = attachment - end - - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - + class Text < Adapter # Public: Was the document conversion successful? # # Returns a Boolean @@ -44,19 +18,6 @@ module AttachmentToHTML text = MySociety::Format.make_clickable(text) text = text.gsub(/\n/, '<br>') end - - def parse_body - convert - end - - def has_content? - !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? - end - - def contains_images? - body.match(/<img[^>]*>/mi) ? true : false - end - end end end diff --git a/lib/attachment_to_html/attachment_to_html.rb b/lib/attachment_to_html/attachment_to_html.rb index e95f67262..2e8d35ca9 100644 --- a/lib/attachment_to_html/attachment_to_html.rb +++ b/lib/attachment_to_html/attachment_to_html.rb @@ -1,6 +1,8 @@ # -*- encoding : utf-8 -*- require 'view' +require 'attachment_to_html/adapter' + Dir[File.dirname(__FILE__) + '/adapters/*.rb'].each do |file| require file end |