Use correct scope to sort headings by name

This commit is contained in:
Julian Herrero
2019-02-08 19:45:13 +01:00
parent 29a704bd60
commit a963a99c55
7 changed files with 57 additions and 26 deletions

View File

@@ -1,7 +1,7 @@
module BudgetHeadingsHelper module BudgetHeadingsHelper
def budget_heading_select_options(budget) 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] [heading.name_scoped_by_group, heading.id]
end end
end end

View File

@@ -27,12 +27,14 @@ class Budget
delegate :budget, :budget_id, to: :group, allow_nil: true delegate :budget, :budget_id, to: :group, allow_nil: true
scope :i18n, -> { includes(:translations) } 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) } 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 def name_scoped_by_group
group.single_heading_group? ? name : "#{group.name}: #{name}" group.single_heading_group? ? name : "#{group.name}: #{name}"
end end

View File

@@ -28,7 +28,7 @@
<div class="row margin"> <div class="row margin">
<div id="headings" class="small-12 medium-7 column select-district"> <div id="headings" class="small-12 medium-7 column select-district">
<div class="row"> <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"> <div class="small-6 medium-4 column end">
<% slice.each do |heading| %> <% slice.each do |heading| %>
<span id="<%= dom_id(heading) %>" <span id="<%= dom_id(heading) %>"

View File

@@ -70,7 +70,7 @@
<% current_budget.groups.each do |group| %> <% current_budget.groups.each do |group| %>
<h2 id="<%= group.name.parameterize %>"><%= group.name %></h2> <h2 id="<%= group.name.parameterize %>"><%= group.name %></h2>
<ul class="no-bullet" data-equalizer data-equalizer-on="medium"> <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> <li class="heading small-12 medium-4 large-2" data-equalizer-watch>
<% unless current_budget.informing? || current_budget.finished? %> <% unless current_budget.informing? || current_budget.finished? %>
<%= link_to budget_investments_path(current_budget.id, <%= link_to budget_investments_path(current_budget.id,

View File

@@ -60,9 +60,19 @@ feature 'Budgets' do
end end
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 scenario "Show groups and headings for missing translations" do
group1 = create(:budget_group, budget: last_budget) group1 = create(:budget_group, budget: budget)
group2 = create(:budget_group, budget: last_budget) group2 = create(:budget_group, budget: budget)
heading1 = create(:budget_heading, group: group1) heading1 = create(:budget_heading, group: group1)
heading2 = create(:budget_heading, group: group2) 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 group1.name
expect(page).to have_content group2.name expect(page).to have_content group2.name
expect(page).to have_content heading1.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 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
end end

View 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

View File

@@ -280,21 +280,8 @@ describe Budget::Heading do
end end
end end
describe "scope order_by_group_name" do describe ".sort_by_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")
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 it "returns headings sorted by DESC group name first and then ASC heading name" do
last_group = create(:budget_group, name: "Group A") last_group = create(:budget_group, name: "Group A")
first_group = create(:budget_group, name: "Group B") 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") heading1 = create(:budget_heading, group: first_group, name: "Name C")
sorted_headings = [heading1, heading2, heading3, heading4] 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 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 end
describe "scope allow_custom_content" do describe "scope allow_custom_content" do