Use correct scope to sort headings by name
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
module BudgetHeadingsHelper
|
||||
|
||||
def budget_heading_select_options(budget)
|
||||
budget.headings.order_by_name.map do |heading|
|
||||
budget.headings.sort_by_name.map do |heading|
|
||||
[heading.name_scoped_by_group, heading.id]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,12 +27,14 @@ class Budget
|
||||
delegate :budget, :budget_id, to: :group, allow_nil: true
|
||||
|
||||
scope :i18n, -> { includes(:translations) }
|
||||
scope :with_group, -> { includes(group: :translations) }
|
||||
scope :order_by_group_name, -> { i18n.with_group.order("budget_group_translations.name DESC") }
|
||||
scope :order_by_heading_name, -> { i18n.with_group.order("budget_heading_translations.name") }
|
||||
scope :order_by_name, -> { i18n.with_group.order_by_group_name.order_by_heading_name }
|
||||
scope :allow_custom_content, -> { i18n.where(allow_custom_content: true).order(:name) }
|
||||
|
||||
def self.sort_by_name
|
||||
all.sort do |heading, other_heading|
|
||||
[other_heading.group.name, heading.name] <=> [heading.group.name, other_heading.name]
|
||||
end
|
||||
end
|
||||
|
||||
def name_scoped_by_group
|
||||
group.single_heading_group? ? name : "#{group.name}: #{name}"
|
||||
end
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
<div class="row margin">
|
||||
<div id="headings" class="small-12 medium-7 column select-district">
|
||||
<div class="row">
|
||||
<% @group.headings.order_by_group_name.each_slice(7) do |slice| %>
|
||||
<% @group.headings.sort_by_name.each_slice(7) do |slice| %>
|
||||
<div class="small-6 medium-4 column end">
|
||||
<% slice.each do |heading| %>
|
||||
<span id="<%= dom_id(heading) %>"
|
||||
|
||||
@@ -70,7 +70,7 @@
|
||||
<% current_budget.groups.each do |group| %>
|
||||
<h2 id="<%= group.name.parameterize %>"><%= group.name %></h2>
|
||||
<ul class="no-bullet" data-equalizer data-equalizer-on="medium">
|
||||
<% group.headings.order_by_group_name.each do |heading| %>
|
||||
<% group.headings.sort_by_name.each do |heading| %>
|
||||
<li class="heading small-12 medium-4 large-2" data-equalizer-watch>
|
||||
<% unless current_budget.informing? || current_budget.finished? %>
|
||||
<%= link_to budget_investments_path(current_budget.id,
|
||||
|
||||
@@ -60,9 +60,19 @@ feature 'Budgets' do
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Show headings ordered by name" do
|
||||
group = create(:budget_group, budget: budget)
|
||||
last_heading = create(:budget_heading, group: group, name: "BBB")
|
||||
first_heading = create(:budget_heading, group: group, name: "AAA")
|
||||
|
||||
visit budgets_path
|
||||
|
||||
expect(first_heading.name).to appear_before(last_heading.name)
|
||||
end
|
||||
|
||||
scenario "Show groups and headings for missing translations" do
|
||||
group1 = create(:budget_group, budget: last_budget)
|
||||
group2 = create(:budget_group, budget: last_budget)
|
||||
group1 = create(:budget_group, budget: budget)
|
||||
group2 = create(:budget_group, budget: budget)
|
||||
|
||||
heading1 = create(:budget_heading, group: group1)
|
||||
heading2 = create(:budget_heading, group: group2)
|
||||
@@ -73,9 +83,9 @@ feature 'Budgets' do
|
||||
expect(page).to have_content group1.name
|
||||
expect(page).to have_content group2.name
|
||||
expect(page).to have_content heading1.name
|
||||
expect(page).to have_content last_budget.formatted_heading_price(heading1)
|
||||
expect(page).to have_content budget.formatted_heading_price(heading1)
|
||||
expect(page).to have_content heading2.name
|
||||
expect(page).to have_content last_budget.formatted_heading_price(heading2)
|
||||
expect(page).to have_content budget.formatted_heading_price(heading2)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
19
spec/features/budgets/groups_spec.rb
Normal file
19
spec/features/budgets/groups_spec.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require "rails_helper"
|
||||
|
||||
feature "Budget Groups" do
|
||||
|
||||
let(:budget) { create(:budget) }
|
||||
let(:group) { create(:budget_group, budget: budget) }
|
||||
|
||||
context "Show" do
|
||||
scenario "Headings are sorted by name" do
|
||||
last_heading = create(:budget_heading, group: group, name: "BBB")
|
||||
first_heading = create(:budget_heading, group: group, name: "AAA")
|
||||
|
||||
visit budget_group_path(budget, group)
|
||||
|
||||
expect(first_heading.name).to appear_before(last_heading.name)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -280,21 +280,8 @@ describe Budget::Heading do
|
||||
end
|
||||
end
|
||||
|
||||
describe "scope order_by_group_name" do
|
||||
it "sorts headings using the group name (DESC) in any locale" do
|
||||
last_group = create(:budget_group, name_en: "CCC", name_es: "AAA")
|
||||
first_group = create(:budget_group, name_en: "DDD", name_es: "BBB")
|
||||
describe ".sort_by_name" do
|
||||
|
||||
last_heading = create(:budget_heading, group: last_group, name: "Name")
|
||||
first_heading = create(:budget_heading, group: first_group, name: "Name")
|
||||
|
||||
expect(Budget::Heading.order_by_group_name.count).to be 2
|
||||
expect(Budget::Heading.order_by_group_name.first).to eq first_heading
|
||||
expect(Budget::Heading.order_by_group_name.last).to eq last_heading
|
||||
end
|
||||
end
|
||||
|
||||
describe "scope order_by_name" do
|
||||
it "returns headings sorted by DESC group name first and then ASC heading name" do
|
||||
last_group = create(:budget_group, name: "Group A")
|
||||
first_group = create(:budget_group, name: "Group B")
|
||||
@@ -305,8 +292,21 @@ describe Budget::Heading do
|
||||
heading1 = create(:budget_heading, group: first_group, name: "Name C")
|
||||
|
||||
sorted_headings = [heading1, heading2, heading3, heading4]
|
||||
expect(Budget::Heading.order_by_name.to_a).to eq sorted_headings
|
||||
expect(Budget::Heading.sort_by_name).to eq sorted_headings
|
||||
end
|
||||
|
||||
it "only sort headings using the group name (DESC) in the current locale" do
|
||||
last_group = create(:budget_group, name_en: "CCC", name_es: "BBB")
|
||||
first_group = create(:budget_group, name_en: "DDD", name_es: "AAA")
|
||||
|
||||
last_heading = create(:budget_heading, group: last_group, name: "Name")
|
||||
first_heading = create(:budget_heading, group: first_group, name: "Name")
|
||||
|
||||
expect(Budget::Heading.sort_by_name.size).to be 2
|
||||
expect(Budget::Heading.sort_by_name.first).to eq first_heading
|
||||
expect(Budget::Heading.sort_by_name.last).to eq last_heading
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "scope allow_custom_content" do
|
||||
|
||||
Reference in New Issue
Block a user