Fix poll question with non-underscored locales

Ruby can't have hyphens in method names, so sending something like
`title_pt-BR=` would raise an exception.
This commit is contained in:
Javi Martín
2018-10-23 14:23:10 +02:00
parent b8673c1cbf
commit ccdbdb26ba
2 changed files with 25 additions and 8 deletions

View File

@@ -45,7 +45,7 @@ class Poll::Question < ActiveRecord::Base
self.author = proposal.author
self.author_visible_name = proposal.author.name
self.proposal_id = proposal.id
send(:"title_#{Globalize.locale}=", proposal.title)
send(:"#{localized_attr_name_for(:title, Globalize.locale)}=", proposal.title)
end
end

View File

@@ -16,14 +16,31 @@ RSpec.describe Poll::Question, type: :model do
end
describe "#copy_attributes_from_proposal" do
before { create_list(:geozone, 3) }
let(:proposal) { create(:proposal) }
it "copies the attributes from the proposal" do
create_list(:geozone, 3)
p = create(:proposal)
poll_question.copy_attributes_from_proposal(p)
expect(poll_question.author).to eq(p.author)
expect(poll_question.author_visible_name).to eq(p.author.name)
expect(poll_question.proposal_id).to eq(p.id)
expect(poll_question.title).to eq(p.title)
poll_question.copy_attributes_from_proposal(proposal)
expect(poll_question.author).to eq(proposal.author)
expect(poll_question.author_visible_name).to eq(proposal.author.name)
expect(poll_question.proposal_id).to eq(proposal.id)
expect(poll_question.title).to eq(proposal.title)
end
context "locale with non-underscored name" do
before do
I18n.locale = :"pt-BR"
Globalize.locale = I18n.locale
end
it "correctly creates a translation" do
poll_question.copy_attributes_from_proposal(proposal)
translation = poll_question.translations.first
expect(poll_question.title).to eq(proposal.title)
expect(translation.title).to eq(proposal.title)
expect(translation.locale).to eq(:"pt-BR")
end
end
end