Skip to content

Commit

Permalink
Hide group name only on budgets with one group
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
decabeza committed Nov 11, 2020
1 parent f1d698b commit 296240f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
6 changes: 5 additions & 1 deletion app/models/budget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ def balloting_or_later?
current_phase&.balloting_or_later?
end

def single_group?
groups.count == 1
end

def single_heading?
groups.count == 1 && headings.count == 1
single_group? && headings.count == 1
end

def enabled_phases_amount
Expand Down
4 changes: 0 additions & 4 deletions app/models/budget/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def self.sort_by_name
all.sort_by(&:name)
end

def single_heading_group?
headings.count == 1
end

private

def generate_slug?
Expand Down
2 changes: 1 addition & 1 deletion app/models/budget/heading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.sort_by_name
end

def name_scoped_by_group
group.single_heading_group? ? name : "#{group.name}: #{name}"
budget.single_group? ? name : "#{group.name}: #{name}"
end

def can_be_deleted?
Expand Down
4 changes: 2 additions & 2 deletions spec/system/admin/budget_investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
expect(page).to have_link("Change name")
expect(page).to have_link("Plant trees")

select "Central Park", from: "heading_id"
select "Parks: Central Park", from: "heading_id"
click_button "Filter"

expect(page).not_to have_link("Realocate visitors")
Expand Down Expand Up @@ -1066,7 +1066,7 @@

fill_in "Title", with: "Potatoes"
fill_in "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"
check "budget_investment_selected"

Expand Down
21 changes: 18 additions & 3 deletions spec/system/budgets/investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,23 @@ def investments_order
expect(page).to have_content "Build a skyscraper"
end

scenario "Create with single group and multiple headings" do
budget = create(:budget)
group = create(:budget_group, name: "New group", budget: budget)
create(:budget_heading, budget: budget, group: group, name: "Culture")
create(:budget_heading, budget: budget, group: group, name: "Environment")

login_as(author)

visit new_budget_investment_path(budget)

expect(page).not_to have_content "New group"
select_options = find("#budget_investment_heading_id").all("option").map(&:text)
expect(select_options).to eq ["", "Culture", "Environment"]
end

scenario "Create with multiple headings" do
heading2 = create(:budget_heading, budget: budget)
heading2 = create(:budget_heading, budget: budget, group: group)
heading3 = create(:budget_heading, budget: budget)
login_as(author)

Expand All @@ -916,7 +931,7 @@ def investments_order
expect(page).to have_selector("option[value='#{heading3.id}']")
end

select heading2.name, from: "budget_investment_heading_id"
select "#{group.name}: #{heading2.name}", from: "budget_investment_heading_id"
fill_in "Title", with: "Build a skyscraper"
fill_in "Description", with: "I want to live in a high tower over the clouds"
fill_in "budget_investment_location", with: "City center"
Expand Down Expand Up @@ -1066,7 +1081,7 @@ def investments_order

select_options = find("#budget_investment_heading_id").all("option").map(&:text)
expect(select_options).to eq ["",
"Toda la ciudad",
"Toda la ciudad: Toda la ciudad",
"Health: More health professionals",
"Health: More hospitals"]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/system/management/budget_investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
expect(page).to have_content(low_investment.title)
end

select "Whole city: District Nine", from: "heading_id"
select "District Nine", from: "heading_id"
click_button("Search")

within "#budget-investments" do
Expand Down

0 comments on commit 296240f

Please sign in to comment.