From 837f48ff8196f79e7b52186be8d4978c8c60001d Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 5 Jul 2018 10:24:26 +0200 Subject: [PATCH] Reduce checks helper method complexity --- app/controllers/checks_controller.rb | 4 ++-- app/helpers/checks_helper.rb | 28 +++++++++++++++++++--------- app/models/check.rb | 2 +- app/views/checks/_table.html.erb | 12 +----------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb index 7610ab3..d252411 100644 --- a/app/controllers/checks_controller.rb +++ b/app/controllers/checks_controller.rb @@ -9,7 +9,7 @@ class ChecksController < ApplicationController has_scope :recurrent_failures, type: :boolean def index - @checks = apply_scopes(policy_scope(Check)).order(current_sort).page(params[:page]) + @checks = apply_scopes(policy_scope(Check)).order(Hash[*current_sort]).page(params[:page]) end def new @@ -99,6 +99,6 @@ class ChecksController < ApplicationController return unless valid_fields.include?(field) return unless valid_directions.include?(direction) - { field => direction } + [field, direction] end end diff --git a/app/helpers/checks_helper.rb b/app/helpers/checks_helper.rb index af4ae58..81dfd3d 100644 --- a/app/helpers/checks_helper.rb +++ b/app/helpers/checks_helper.rb @@ -18,19 +18,29 @@ module ChecksHelper def checks_sort_link(field, direction) classes = "btn btn-light btn-sm mx-1 mx-1 px-1 py-0" - current_sort_str = current_sort.to_a.join("_") - - sort = "#{field}_#{direction}" + sort = [field, direction] icon = direction == :asc ? "chevron-up" : "chevron-down" html = Octicons::Octicon.new(icon).to_svg.html_safe - filter_params = current_criterias.merge(sort: sort) - link_to_unless sort == current_sort_str, html, checks_path(filter_params), class: classes do + sort_path = checks_path(current_criterias.merge(sort: sort.join("_"))) + link_to_unless sort == current_sort, html, sort_path, class: classes do content_tag(:span, html, class: classes + " active") end end + def check_in_error(check) + content_tag( + :span, + Octicons::Octicon.new("alert", class: "ml-1").to_svg.html_safe, + class: "in-error text-danger", + data: { + toggle: "tooltip", + placement: "bottom", + title: check_last_success_title(check) + }) + end + def current_criterias current_scopes.merge(sort: params[:sort]) end @@ -53,10 +63,10 @@ module ChecksHelper def check_button_scope_class(scope = nil) "btn btn-sm " + if scope && scoped_with?(scope) - "btn-info active" - else - "btn-outline-info" - end + "btn-info active" + else + "btn-outline-info" + end end def check_last_success_title(check) diff --git a/app/models/check.rb b/app/models/check.rb index 958f5ca..de2d32d 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -69,7 +69,7 @@ class Check < ApplicationRecord } def self.default_sort - { domain_expires_at: :asc } + [:domain_expires_at, :asc] end def in_error? diff --git a/app/views/checks/_table.html.erb b/app/views/checks/_table.html.erb index 776d1de..a9d8cfc 100644 --- a/app/views/checks/_table.html.erb +++ b/app/views/checks/_table.html.erb @@ -26,17 +26,7 @@ <%= t(".kind_labels.#{check.kind}") %> - <%- if check.in_error? %> - <%== content_tag( - :span, - Octicons::Octicon.new("alert", class: "ml-1").to_svg.html_safe, - class: "in-error text-danger", - data: { - toggle: "tooltip", - placement: "bottom", - title: check_last_success_title(check) - }) %> - <% end %> + <%= check_in_error(check) if check.in_error? %> <%= check.domain %>