~xhire/ipv6portal

6b9e1693cceff44550f9aafa2cc28703c9852533 — xHire 4 months ago 28e0869
Strip \r characters from reports content
2 files changed, 10 insertions(+), 0 deletions(-)

M app/models/entity_report.rb
M test/models/entity_report_test.rb
M app/models/entity_report.rb => app/models/entity_report.rb +5 -0
@@ 35,6 35,7 @@ class EntityReport < ApplicationRecord

  scope :approved, -> { where(:approved => true) }

  before_validation :content_strip_cr
  after_save :update_entity_status

  def approve!


@@ 55,6 56,10 @@ class EntityReport < ApplicationRecord
  end

  private
  def content_strip_cr
    content.tr! ?\r, '' if content.present?
  end

  def update_entity_status
    if approved? and entity.reports.approved.order(:created_at => :desc).first == self
      entity.update :status_cached => status, :updated_at => created_at

M test/models/entity_report_test.rb => test/models/entity_report_test.rb +5 -0
@@ 20,6 20,11 @@ class EntityReportTest < ActiveSupport::TestCase
    assert_equal [ er2, er4 ], EntityReport.approved.to_a
  end

  test 'automatically strip \r characters from content' do
    er = FactoryGirl.create :entity_report, :content => "hello\r\nbye\r\n"
    assert_equal "hello\nbye\n", er.content
  end

  test '#approve! sets approved to true' do
    er = FactoryGirl.create :entity_report, :approved => false
    assert_changes 'er.reload.approved', :from => false, :to => true do