aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/censor_rule.rb
blob: a477d2568c17d7cff053094f9f2ea850b342113a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# == Schema Information
# Schema version: 114
#
# Table name: censor_rules
#
#  id                :integer         not null, primary key
#  info_request_id   :integer
#  user_id           :integer
#  public_body_id    :integer
#  text              :text            not null
#  replacement       :text            not null
#  last_edit_editor  :string(255)     not null
#  last_edit_comment :text            not null
#  created_at        :datetime        not null
#  updated_at        :datetime        not null
#

# models/censor_rule.rb:
# Stores alterations to remove specific data from requests.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: censor_rule.rb,v 1.14 2009-09-17 21:10:04 francis Exp $

class CensorRule < ActiveRecord::Base
    belongs_to :info_request
    belongs_to :user
    belongs_to :public_body

    def binary_replacement
        self.text.gsub(/./, 'x')
    end

    def apply_to_text!(text)
        if text.nil?
            return nil
        end
        text.gsub!(self.text, self.replacement)
    end
    def apply_to_binary!(binary)
        if binary.nil?
            return nil
        end
        binary.gsub!(self.text, self.binary_replacement)
    end


    def validate
        if self.info_request.nil? && self.user.nil? && self.public_body.nil?
            errors.add("Censor must apply to an info request a user or a body; ")
        end
    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