Skip to content

Commit

Permalink
unify new line and strip resume body
Browse files Browse the repository at this point in the history
  • Loading branch information
kwappa committed Jan 20, 2015
1 parent 8889824 commit fed6b80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app/models/user_resume.rb
Expand Up @@ -3,6 +3,8 @@ class UserResume < ActiveRecord::Base

belongs_to :user

before_create :prepare_body

after_create :record_first_diff
after_save :touch_user

Expand All @@ -11,16 +13,25 @@ def touch_user
end

def update_body(content)
old_body = self.body.strip
result = update_attributes(body: content.strip)
old_body = self.body
new_body = self.class.strip_and_unify_newline(content)
result = update_attributes(body: new_body)

if result
UserResumeHistory.record_diff(self.user_id, old_body, self.body)
UserResumeHistory.record_diff(self.user_id, old_body, new_body)
end
result
end

def record_first_diff
UserResumeHistory.record_diff(self.user_id, '', self.body)
end

def self.strip_and_unify_newline(str)
str.strip.gsub(/\r\n|\r|\n/, "\n")
end

def prepare_body
self.body = self.class.strip_and_unify_newline(self.body)
end
end
20 changes: 20 additions & 0 deletions spec/models/user_resume_spec.rb
@@ -1,6 +1,26 @@
require 'rails_helper'

RSpec.describe UserResume, type: :model do
describe 'unify newline and strip' do
let(:user) { create(:user) }
let(:original_body) { " new\r\nline\rwith\nblank\t" }
let(:saved_body) { "new\nline\nwith\nblank" }

context 'when create' do
before { described_class.create(user_id: user.id, body: original_body) }
it 'strips and unifies new line' do
expect(user.resume.body).to eq saved_body
end
end

context 'when update' do
let(:old_body) { 'old_body' }
before { described_class.create(user_id: user.id, body: old_body) }
it 'strips and unifies new line' do
expect { user.resume.update_body(original_body) }.to change { user.resume.body }.from(old_body).to(saved_body)
end
end
end

describe 'after save' do
let(:old_time) { 1.minute.ago.change(usec: 0) }
Expand Down

0 comments on commit fed6b80

Please sign in to comment.