From 75d8c3324f06bd61b7d760506544229ced80e41e Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 27 Jun 2024 20:02:03 +0400 Subject: [PATCH] fa_icon_helper warn up when useless options --- app/helpers/fa_icon_helper.rb | 36 ++++++++++++++++++++---- app/views/scores/_score.html.erb | 4 +-- config/initializers/rails_live_reload.rb | 14 ++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/app/helpers/fa_icon_helper.rb b/app/helpers/fa_icon_helper.rb index 6c5d99b..f60d313 100644 --- a/app/helpers/fa_icon_helper.rb +++ b/app/helpers/fa_icon_helper.rb @@ -1,18 +1,42 @@ # Font Awesome Helper module FaIconHelper - # accept name without 'fa-' prefix, style: (solid by default), size:, class: and 'aria-hidden': + ## + # mandatory: + # name: without 'fa-' prefix + # might be a symbol, automatic conversion of undashed symbol + # for instance symbol :magnify_lens is converted to string 'magnify-lens' + # optional: + # stroke: [:solid|:regular|:brands] solid as default value + # size: [2x, 3x, ... ] + # class: extra classes, any tailwind keyword... + # aria-hidden: true as default value + # + # example: + # fa_icon(:user) + # fa_icon(:user, stroke: :regular) + # fa_icon(:user, size: '2x') + # fa_icon(:user, class: 'text-blue-500') + ## def fa_icon(name, html_options = {}) - html_options[:style] ||= :solid - html_options['aria-hidden'] ||= true + warn_useless_options html_options - content_class = "fa-#{to_dash(html_options[:style])} fa-#{to_dash(name)}" - content_class << " fa-#{to_dash(html_options[:size])}" if html_options.key? :size + icon = to_dash(name) + stroke = to_dash(html_options[:stroke] || :solid) + + content_class = "fa-#{icon} fa-#{stroke}" + content_class << " fa-#{html_options[:size]}" if html_options.key? :size content_class << " #{html_options[:class]}" if html_options.key? :class - tag.i(class: content_class, 'aria-hidden': html_options['aria-hidden']) + tag.i(class: content_class, 'aria-hidden': html_options['aria-hidden'] || true) end private def to_dash(symbol) = symbol.to_s.dasherize + + def warn_useless_options(hash) + hash.except(:stroke, :size, :class, 'aria-hidden').each_key do |key| + SemanticLogger[Module.nesting[0]].warn "useless option <#{key}>, please consider removal for safety reason!" + end + end end diff --git a/app/views/scores/_score.html.erb b/app/views/scores/_score.html.erb index 4f753d5..b65c5af 100644 --- a/app/views/scores/_score.html.erb +++ b/app/views/scores/_score.html.erb @@ -9,8 +9,8 @@ <% if action_name != "show" %> - <%= link_to score do %><%=fa_icon :eye, class: 'border rounded-2 mr-2' %>Show Score<% end %> - <%= link_to "Edit this score", edit_score_path(score), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to score do %><%=fa_icon :eye, stroke: :regular, size: '4x', class: 'border rounded-lg border-purple-800 m-1 p-2 bg-white' %>Show Score<% end %> + <%= link_to "Edit this score", edit_score_path(score), class: "rounded-xl border border-yellow-300 py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %> <%= link_to "Edit this score", edit_score_path(score), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %> <%= button_to "Destroy this score", score_path(score), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
diff --git a/config/initializers/rails_live_reload.rb b/config/initializers/rails_live_reload.rb index a4e10fa..7b3f29a 100644 --- a/config/initializers/rails_live_reload.rb +++ b/config/initializers/rails_live_reload.rb @@ -2,16 +2,16 @@ if defined?(RailsLiveReload) && Rails.env.development? RailsLiveReload.configure do |config| - # Default watched folders & files - config.watch %r{app/views/.+\.(erb|haml|slim)$} - - # Rk: prevent any reload for files ending with '.tailwind.css' - config.watch %r{(app|vendor)/(assets|javascript)/.+\.(css|js|html|png|jpg)(?