<commit_before># == Schema Information
# Schema version: 20120919140404
#
# 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
#  regexp            :boolean
#

# models/censor_rule.rb:
# Stores alterations to remove specific data from requests.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>; WWW: http://www.mysociety.org/

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

    # a flag to allow the require_user_request_or_public_body validation to be skipped
    attr_accessor :allow_global
    validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true }
    validate :require_valid_regexp, :if => proc{ |rule| rule.regexp? == true }
    validates_presence_of :text

    scope :global, {:conditions => {:info_request_id => nil,
                                    :user_id => nil,
                                    :public_body_id => nil}}

    def require_user_request_or_public_body
        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 require_valid_regexp
        begin
            self.make_regexp()
        rescue RegexpError => e
            errors.add(:text, e.message)
        end
    end

    def make_regexp
        return Regexp.new(self.text, Regexp::MULTILINE)
    end

    def apply_to_text!(text)
        if text.nil?
            return nil
        end
        to_replace = regexp? ? self.make_regexp() : self.text
        text.gsub!(to_replace, self.replacement)
    end

    def apply_to_binary!(binary)
        if binary.nil?
            return nil
        end
        to_replace = regexp? ? self.make_regexp() : self.text
        binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') }
    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 is_global?
        return true if (info_request_id.nil? && user_id.nil? && public_body_id.nil?)
        return false
    end

end
<commit_msg>Add the error to each attribute which would display the error next to each input field<commit_after># == Schema Information
# Schema version: 20120919140404
#
# 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
#  regexp            :boolean
#

# models/censor_rule.rb:
# Stores alterations to remove specific data from requests.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>; WWW: http://www.mysociety.org/

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

    # a flag to allow the require_user_request_or_public_body validation to be skipped
    attr_accessor :allow_global
    validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true }
    validate :require_valid_regexp, :if => proc{ |rule| rule.regexp? == true }
    validates_presence_of :text

    scope :global, {:conditions => {:info_request_id => nil,
                                    :user_id => nil,
                                    :public_body_id => nil}}

    def require_user_request_or_public_body
        if self.info_request.nil? && self.user.nil? && self.public_body.nil?
            [:info_request, :user, :public_body].each do |a|
                errors.add(a, "Rule must apply to an info request, a user or a body")
            end
        end
    end

    def require_valid_regexp
        begin
            self.make_regexp()
        rescue RegexpError => e
            errors.add(:text, e.message)
        end
    end

    def make_regexp
        return Regexp.new(self.text, Regexp::MULTILINE)
    end

    def apply_to_text!(text)
        if text.nil?
            return nil
        end
        to_replace = regexp? ? self.make_regexp() : self.text
        text.gsub!(to_replace, self.replacement)
    end

    def apply_to_binary!(binary)
        if binary.nil?
            return nil
        end
        to_replace = regexp? ? self.make_regexp() : self.text
        binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') }
    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 is_global?
        return true if (info_request_id.nil? && user_id.nil? && public_body_id.nil?)
        return false
    end

end

{'fraction_non_alphanumeric': 0.0874266862170088, 'fraction_numerical': 0.0076979472140762464, 'mean_word_length': 2.163478260869565, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3795642', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1642, 'n_words': 434}