Skip to content

Commit

Permalink
Improved robustness on setting the paperwork's document link (tildes,…
Browse files Browse the repository at this point in the history
… trailing whitespaces and periods)
  • Loading branch information
maugsbur committed Feb 27, 2014
1 parent bf87ab5 commit 97df0af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/models/paperwork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ def timeline_status_hash
def set_document_link
documents = Bill.find(self.bill_id).documents
documents.each do |document|
if document.date == self.date && document.stage == self.stage && document.step == self.description
stage_match = (clean_string document.stage) == (clean_string self.stage)
description_match = (clean_string document.step) == (clean_string self.description)
if document.date == self.date && stage_match && description_match
self.document_link = document.link
end
end
end

def clean_string messy_string
clean_string = messy_string.strip.chomp('.').strip
clean_string = I18n.transliterate(clean_string, locale: :transliterate_special).downcase
end

end
7 changes: 7 additions & 0 deletions spec/factories/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
b.stage "Primer trámite constitucional"
b.link "http://example-site.org"
end

factory :inconsistent_data_document do |b|
b.date "2014-01-01 11:35:01"
b.step "Ingreso de proyecto ."
b.stage "Primer tramite cónstitucional . "
b.link "http://example-site.org"
end
end
end
13 changes: 13 additions & 0 deletions spec/models/paperwork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@
@paperwork1.document_link.should eq @document1.link
@paperwork2.document_link.should be_nil
end

context "with inconsistent data" do
it "saves a valid document link " do
@paperwork1 = FactoryGirl.build(:paperwork1)
@inconsistent_data_document = FactoryGirl.create(:inconsistent_data_document)
@bill = FactoryGirl.create(:bill1)
@bill.paperworks = [@paperwork1]
@bill.documents = [@inconsistent_data_document]
@bill.save
@paperwork1.save
@paperwork1.document_link.should eq @inconsistent_data_document.link
end
end
end

0 comments on commit 97df0af

Please sign in to comment.