Hide group name only on budgets with one group

In the form of creating a new investment was hiding the name of the
group if it had only one heading, but could be confusing to users if
there are, for example, five different groups of one heading.

The solution:

- If the budget has one group and one heading, the heading selector is
  hidden.
- If the budget has one group and more than one heading, the group name
  is hidden.
- If the budget has more than one group, the group name appears
  regardless of the number of headings.
This commit is contained in:
decabeza
2020-11-11 09:06:56 +01:00
committed by Javi Martín
parent bc165eeda5
commit 88ad711330
7 changed files with 61 additions and 15 deletions

View File

@@ -162,8 +162,12 @@ class Budget < ApplicationRecord
current_phase&.balloting_or_later? current_phase&.balloting_or_later?
end end
def single_group?
groups.one?
end
def single_heading? def single_heading?
groups.one? && headings.one? single_group? && headings.one?
end end
def heading_price(heading) def heading_price(heading)

View File

@@ -30,10 +30,6 @@ class Budget
all.sort_by(&:name) all.sort_by(&:name)
end end
def single_heading_group?
headings.count == 1
end
private private
def generate_slug? def generate_slug?

View File

@@ -48,7 +48,7 @@ class Budget
end end
def name_scoped_by_group def name_scoped_by_group
group.single_heading_group? ? name : "#{group.name}: #{name}" budget.single_group? ? name : "#{group.name}: #{name}"
end end
def can_be_deleted? def can_be_deleted?

View File

@@ -332,4 +332,33 @@ describe Budget::Heading do
expect(build(:budget_heading, max_ballot_lines: 0)).not_to be_valid expect(build(:budget_heading, max_ballot_lines: 0)).not_to be_valid
end end
end end
describe "#name_scoped_by_group" do
it "returns heading name in budgets with a single heading" do
heading = create(:budget_heading, group: group, name: "One and only")
expect(heading.name_scoped_by_group).to eq "One and only"
end
it "returns heading name in budgets with one group and many headings" do
schools = create(:budget_heading, group: group, name: "Schools")
universities = create(:budget_heading, group: group, name: "Universities")
expect(schools.name_scoped_by_group).to eq "Schools"
expect(universities.name_scoped_by_group).to eq "Universities"
end
it "returns heading name in groups with many headings in budgets with many groups" do
education = create(:budget_group, budget: budget, name: "Education")
health = create(:budget_group, budget: budget, name: "Health")
schools = create(:budget_heading, group: education, name: "Schools")
universities = create(:budget_heading, group: education, name: "Universities")
supplies = create(:budget_heading, group: health, name: "Medical supplies")
expect(schools.name_scoped_by_group).to eq "Education: Schools"
expect(universities.name_scoped_by_group).to eq "Education: Universities"
expect(supplies.name_scoped_by_group).to eq "Health: Medical supplies"
end
end
end end

View File

@@ -101,7 +101,7 @@ describe "Admin budget investments", :admin do
expect(page).to have_link("Change name") expect(page).to have_link("Change name")
expect(page).to have_link("Plant trees") expect(page).to have_link("Plant trees")
select "Central Park", from: "heading_id" select "Parks: Central Park", from: "heading_id"
click_button "Filter" click_button "Filter"
expect(page).not_to have_link("Realocate visitors") expect(page).not_to have_link("Realocate visitors")
@@ -1040,7 +1040,7 @@ describe "Admin budget investments", :admin do
fill_in "Title", with: "Potatoes" fill_in "Title", with: "Potatoes"
fill_in_ckeditor "Description", with: "Carrots" fill_in_ckeditor "Description", with: "Carrots"
select "#{budget_investment.group.name}: Barbate", from: "budget_investment[heading_id]" select "Barbate", from: "budget_investment[heading_id]"
uncheck "budget_investment_incompatible" uncheck "budget_investment_incompatible"
check "budget_investment_selected" check "budget_investment_selected"

View File

@@ -570,18 +570,35 @@ describe "Budget Investments" do
expect(page).to have_content "Build a skyscraper" expect(page).to have_content "Build a skyscraper"
end end
scenario "Create with multiple headings" do scenario "Create with single group and multiple headings" do
create(:budget_heading, budget: budget, name: "Medical supplies") create(:budget_heading, group: group, name: "Medical supplies")
create(:budget_heading, budget: budget, name: "Even more hospitals") create(:budget_heading, group: group, name: "Even more hospitals")
login_as(author)
visit new_budget_investment_path(budget)
expect(page).to have_select "Heading",
options: ["", "More hospitals", "Medical supplies", "Even more hospitals"]
expect(page).not_to have_content "Health"
end
scenario "Create with multiple groups" do
education = create(:budget_group, budget: budget, name: "Education")
create(:budget_heading, group: group, name: "Medical supplies")
create(:budget_heading, group: education, name: "Schools")
login_as(author) login_as(author)
visit new_budget_investment_path(budget) visit new_budget_investment_path(budget)
expect(page).not_to have_content("#{heading.name} (#{budget.formatted_heading_price(heading)})") expect(page).not_to have_content("#{heading.name} (#{budget.formatted_heading_price(heading)})")
expect(page).to have_select "Heading", expect(page).to have_select "Heading",
options: ["", "More hospitals", "Medical supplies", "Even more hospitals"] options: ["", "Health: More hospitals", "Health: Medical supplies", "Education: Schools"]
select "Health: Medical supplies", from: "Heading"
select "Medical supplies", from: "Heading"
fill_in "Title", with: "Build a skyscraper" fill_in "Title", with: "Build a skyscraper"
fill_in_ckeditor "Description", with: "I want to live in a high tower over the clouds" fill_in_ckeditor "Description", with: "I want to live in a high tower over the clouds"
fill_in "Location additional info", with: "City center" fill_in "Location additional info", with: "City center"
@@ -755,7 +772,7 @@ describe "Budget Investments" do
select_options = find("#budget_investment_heading_id").all("option").map(&:text) select_options = find("#budget_investment_heading_id").all("option").map(&:text)
expect(select_options).to eq ["", expect(select_options).to eq ["",
"Toda la ciudad", "Toda la ciudad: Toda la ciudad",
"Health: More health professionals", "Health: More health professionals",
"Health: More hospitals"] "Health: More hospitals"]
end end

View File

@@ -396,7 +396,7 @@ describe "Budget Investments" do
expect(page).to have_content(low_investment.title) expect(page).to have_content(low_investment.title)
end end
select "Whole city: District Nine", from: "heading_id" select "District Nine", from: "heading_id"
click_button("Search") click_button("Search")
within "#budget-investments" do within "#budget-investments" do