Extract setting icon and direction to methods
This commit is contained in:
committed by
Javi Martín
parent
0fe9ea08c3
commit
3fd7b3d216
@@ -5,13 +5,10 @@ module BudgetInvestmentsHelper
|
||||
|
||||
def link_to_investments_sorted_by(column)
|
||||
sort_by = column.downcase
|
||||
default_direction = "desc"
|
||||
current_direction = params[:direction].downcase if params[:direction]
|
||||
|
||||
direction = current_direction == default_direction ? "asc" : default_direction
|
||||
|
||||
icon = direction == default_direction ? "icon-arrow-top" : "icon-arrow-down"
|
||||
icon = sort_by == params[:sort_by] ? icon : ""
|
||||
direction = set_direction(current_direction)
|
||||
icon = set_sorting_icon(direction, sort_by)
|
||||
|
||||
translation = t("admin.budget_investments.index.sort_by.#{sort_by}")
|
||||
|
||||
@@ -21,6 +18,15 @@ module BudgetInvestmentsHelper
|
||||
)
|
||||
end
|
||||
|
||||
def set_sorting_icon(direction, sort_by)
|
||||
icon = direction == "desc" ? "icon-arrow-top" : "icon-arrow-down"
|
||||
icon = sort_by == params[:sort_by] ? icon : ""
|
||||
end
|
||||
|
||||
def set_direction(current_direction)
|
||||
current_direction == "desc" ? "asc" : "desc"
|
||||
end
|
||||
|
||||
def investments_minimal_view_path
|
||||
budget_investments_path(id: @heading.group.to_param,
|
||||
heading_id: @heading.to_param,
|
||||
|
||||
47
spec/helpers/budget_investments_helper_spec.rb
Normal file
47
spec/helpers/budget_investments_helper_spec.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe BudgetInvestmentsHelper, type: :helper do
|
||||
|
||||
describe "#set_direction" do
|
||||
|
||||
it "returns ASC if current_direction is DESC" do
|
||||
expect(set_direction("desc")).to eq "asc"
|
||||
end
|
||||
|
||||
it "returns DESC if current_direction is ASC" do
|
||||
expect(set_direction("asc")).to eq "desc"
|
||||
end
|
||||
|
||||
it "returns DESC if current_direction is nil" do
|
||||
expect(set_direction(nil)).to eq "desc"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#set_sorting_icon" do
|
||||
let(:sort_by) { "title" }
|
||||
let(:params) { { sort_by: sort_by } }
|
||||
|
||||
it "returns arrow down if current direction is ASC" do
|
||||
expect(set_sorting_icon("asc", sort_by)).to eq "icon-arrow-down"
|
||||
end
|
||||
|
||||
it "returns arrow top if current direction is DESC" do
|
||||
expect(set_sorting_icon("desc", sort_by)).to eq "icon-arrow-top"
|
||||
end
|
||||
|
||||
it "returns arrow down if sort_by present, but no direction" do
|
||||
expect(set_sorting_icon(nil, sort_by)).to eq "icon-arrow-down"
|
||||
end
|
||||
|
||||
it "returns no icon if sort_by and direction is missing" do
|
||||
params[:sort_by] = nil
|
||||
expect(set_sorting_icon(nil, sort_by)).to eq ""
|
||||
end
|
||||
|
||||
it "returns no icon if sort_by is incorrect" do
|
||||
params[:sort_by] = "random"
|
||||
expect(set_sorting_icon("asc", sort_by)).to eq ""
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user