Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Freeze amender phone_number #7

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions app/decorators/decidim/amendable/edit_form_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,4 @@ def map_model(model)
self.emendation_params = model.emendation.attributes.slice(*amendable_fields_as_string)
self.phone_number = model.amender.phone_number
end

# Method added.
# ProposalNote created in Decidim::Amendable::UpdateDraft.
def proposal_note
@proposal_note ||= Decidim::Proposals::ProposalNote.find_by(proposal: emendation, author: current_user)
end

# Method added.
# The value to render in the :phone_number field at app/views/decidim/amendments/edit_draft.html.erb
def phone_number_value
proposal_note&.body || phone_number
end
end
19 changes: 8 additions & 11 deletions app/decorators/decidim/amendable/update_draft_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def call

transaction do
update_draft
handle_proposal_note
create_proposal_note
end

broadcast(:ok, @amendment)
Expand All @@ -29,15 +29,12 @@ def update_draft
end

# Method added.
# Creates and/or updates or deletes the ProposalNote with the phone number.
def handle_proposal_note
proposal_note = Decidim::Proposals::ProposalNote.find_or_initialize_by(proposal: emendation, author: current_user)

if form.phone_number.present?
proposal_note&.update(body: form.phone_number)
else
proposal_note&.delete
Decidim::Proposals::Proposal.reset_counters(emendation.id, :proposal_notes_count)
end
# Creates a ProposalNote with the phone number.
def create_proposal_note
Decidim::Proposals::ProposalNote.find_or_create_by(
proposal: emendation,
author: current_user,
body: form.phone_number
)
end
end
4 changes: 2 additions & 2 deletions app/views/decidim/amendments/edit_draft.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<div class="field">
<%= form.text_field :phone_number,
label: t(".phone_number"),
help_text: t(".phone_number_help"),
value: form.object.phone_number_value %>
help_text: t(".phone_number_help").html_safe,
readonly: true %>
</div>
<!-- /Overwriting this view to add this field -->

Expand Down
4 changes: 2 additions & 2 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ ca:
discard: Descarta aquest esborrany
discard_confirmation: Estàs segur de que vols descartar aquest esborrany d'esmena?
phone_number: Telèfon de contacte
phone_number_help: Siusplau, indica el teu telèfon de contacte per tal que
la organització es pugui posar en contacte amb tu.
phone_number_help: Si aquest no és el teu telèfon de contacte, siusplau, posa't
en contacte amb <strong>administracio@erc.cat</strong>.
send: Vista prèvia
title: Completa l'esborrany d'esmena
emendation:
Expand Down
4 changes: 2 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ en:
amendments:
edit_draft:
phone_number: Contact phone number
phone_number_help: Please, review your phone number so the organization
can contact you.
phone_number_help: Please, contact administracio@erc.cat if your data
is not up to date.
81 changes: 6 additions & 75 deletions spec/system/amendments_wizard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,93 +77,24 @@

it "show the phone_number field prefilled" do
within ".edit_amendment" do
expect(page).to have_field("Contact phone number", with: "666-666-666")
end
end

context "when the user leaves the phone number field prefilled as is" do
before do
within ".edit_amendment" do
find("*[type=submit]").click
# Data returned from CiviCRM should be readonly and users must be informed.
expect(page).to have_field("Contact phone number", with: "666-666-666", readonly: true)
within "#amendment_phone_number" do
expect(page).to have_tag("p.help-text", text: /administracio@erc.cat/)
end
end

it "creates a proposal note" do
expect(emendation_draft.proposal_notes_count).to eq(1)
expect(Decidim::Proposals::ProposalNote.last.body).to eq("666-666-666")
end
end

context "when the user fills the phone number field with another value" do
context "when the user submits the form" do
before do
within ".edit_amendment" do
fill_in :amendment_phone_number, with: "999-999-999"
find("*[type=submit]").click
end
end

it "creates a proposal note" do
expect(emendation_draft.proposal_notes_count).to eq(1)
expect(Decidim::Proposals::ProposalNote.last.body).to eq("999-999-999")
end
end

context "when the user empties the phone number field" do
before do
within ".edit_amendment" do
fill_in :amendment_phone_number, with: ""
find("*[type=submit]").click
end
end

it "does NOT create a proposal note" do
expect(emendation_draft.proposal_notes_count).to eq(0)
end
end
end

context "and in step_4: Preview your amendment" do
before do
within ".new_amendment" do
fill_in :amendment_emendation_params_title, with: title
fill_in :amendment_emendation_params_body, with: body
find("*[type=submit]").click
end
within ".edit_amendment" do
find("*[type=submit]").click
end
end

context "when the Modify link is clicked" do
before do
click_link "Modify"
end

context "and the empties the phone number field" do
before do
within ".edit_amendment" do
fill_in :amendment_phone_number, with: ""
find("*[type=submit]").click
end
end

it "deletes the proposal note" do
expect(emendation_draft.reload.proposal_notes_count).to eq(0)
end
end

context "and updates the phone number field" do
before do
within ".edit_amendment" do
fill_in :amendment_phone_number, with: "999-999-999"
find("*[type=submit]").click
end
end

it "updates the proposal note" do
expect(emendation_draft.proposal_notes_count).to eq(1)
expect(Decidim::Proposals::ProposalNote.last.body).to eq("999-999-999")
end
expect(Decidim::Proposals::ProposalNote.last.body).to eq("666-666-666")
end
end
end
Expand Down