From cf31c24d0293011bb052efba36c1b06e9f353080 Mon Sep 17 00:00:00 2001 From: Kaspar Vollenweider Date: Sun, 5 May 2019 13:03:12 +0200 Subject: [PATCH] remove contextual link helpers --- .../stylesheets/i18n_rails_helpers.scss | 12 -- app/helpers/bootstrap_helper.rb | 174 ------------------ app/helpers/contextual_link_helpers.rb | 152 --------------- app/helpers/list_link_helpers.rb | 76 -------- app/views/application/_list.html.haml | 9 - app/views/application/edit.html.haml | 5 - app/views/application/index.html.haml | 5 - app/views/application/new.html.haml | 5 - app/views/application/show.html.haml | 5 - lib/boot_form_builder.rb | 9 - lib/i18n_rails_helpers.rb | 3 +- lib/i18n_rails_helpers/engine.rb | 2 - spec/helpers/contextual_link_helpers_spec.rb | 25 --- spec/helpers/list_link_helpers_spec.rb | 9 - 14 files changed, 1 insertion(+), 490 deletions(-) delete mode 100644 app/assets/stylesheets/i18n_rails_helpers.scss delete mode 100644 app/helpers/bootstrap_helper.rb delete mode 100644 app/helpers/contextual_link_helpers.rb delete mode 100644 app/helpers/list_link_helpers.rb delete mode 100644 app/views/application/_list.html.haml delete mode 100644 app/views/application/edit.html.haml delete mode 100644 app/views/application/index.html.haml delete mode 100644 app/views/application/new.html.haml delete mode 100644 app/views/application/show.html.haml delete mode 100644 lib/boot_form_builder.rb delete mode 100644 spec/helpers/contextual_link_helpers_spec.rb delete mode 100644 spec/helpers/list_link_helpers_spec.rb diff --git a/app/assets/stylesheets/i18n_rails_helpers.scss b/app/assets/stylesheets/i18n_rails_helpers.scss deleted file mode 100644 index b951ee0..0000000 --- a/app/assets/stylesheets/i18n_rails_helpers.scss +++ /dev/null @@ -1,12 +0,0 @@ -// Fix spacing for contextual buttons on page headers -.ctx { - float: right; -} - -.ctx-page-header, .ctx-h1, .ctx-h2, .ctx-h3, .ctx-h4 { - margin-top: 5px; -} - -.page-header { - margin-top: 0; -} diff --git a/app/helpers/bootstrap_helper.rb b/app/helpers/bootstrap_helper.rb deleted file mode 100644 index 0fa3ea0..0000000 --- a/app/helpers/bootstrap_helper.rb +++ /dev/null @@ -1,174 +0,0 @@ -module BootstrapHelper - if defined?(SimpleForm) - require 'boot_form_builder' - - def boot_form_for(object, *args, &block) - options = args.extract_options! - simple_form_for(object, *(args << options.merge(builder: BootFormBuilder)), &block) - end - end - - def boot_page_title(action_or_title = nil, model = nil, &block) - if block_given? - title = capture(&block) - else - if action_or_title.is_a? String - title = action_or_title - else - action = action_or_title || action_name - if action.to_s == 'show' && defined?(resource) && resource.present? - title = resource.display_name if resource.respond_to?(:display_name) - title ||= resource.to_s - else - title = t_title(action, model) - end - end - end - - content_for :page_title, title - content_tag(:div, :class => 'page-header') do - content_tag(:h1, title) - end - end - - # Icons - # ===== - def boot_icon(type) - classes = I18nRailsHelpers.boot_icon_class_template % [type] - content_tag(:i, '', :class => classes) - end - - # Labels - # ====== - def boot_label(content, type = nil) - return "" unless content.present? - - classes = ['label', "label-#{type}"].compact.join(' ') - content_tag(:span, content, :class => classes) - end - - # Modal - # ===== - def modal_header(title) - content_tag(:div, :class => 'modal-header') do - content_tag(:button, '×'.html_safe, :type => 'button', :class => 'close', 'data-dismiss' => 'modal') + - content_tag(:h3, title) - end - end - - # Messages - # ======== - # Map common rails flash types to bootstrap alert names. - def boot_alert_name(type) - case type - when 'alert' - 'danger' - when 'notice' - 'info' - else - type - end - end - - def boot_alert(*args, &block) - if block_given? - type = args[0] - content = capture(&block) - else - content = args[0] - type = args[1] - end - - type ||= 'info' - content_tag(:div, :class => "alert alert-#{boot_alert_name(type)}") do - link_to('×'.html_safe, '#', :class => 'close', 'data-dismiss' => 'alert') + content - end - end - - def boot_no_entry_alert - boot_alert t('alerts.empty') - end - - # Navigation - # ========== - def boot_nav_header(title, refresh_action = false) - if title.is_a? Symbol - title = t("#{title}.title") - end - - content_tag(:li, :class => 'nav-header') do - content = [title] - if refresh_action - content << content_tag(:button, :type => "submit", :style => 'border: none; background-color: transparent; float: right') do - content_tag(:i, "", :class => 'icon-refresh') - end - end - - content.join("\n").html_safe - end - end - - def boot_nav_li_link(filter_name, param_value, title = nil, param_name = filter_name, current_value = params[param_name], classes = [], &content) - classes << "active" if current_value.to_s == param_value.to_s - title ||= param_value - title = t("#{filter_name.to_s}.#{title}", :default => title) - - if block_given? - content_tag(:li, :class => classes, &content) - else - content_tag(:li, :class => classes) do - url = url_for(params.merge({param_name => param_value})) - link_to title, url - end - end - end - - def boot_nav_li_checkbox(filter_name, param_value, title = nil, param_name = filter_name, current_value = params[param_name], classes = [], &content) - active = current_value.include? param_value.to_s - classes << "active" if active - - title ||= param_value - title = t("#{filter_name.to_s}.#{title}", :default => title) - - if block_given? - content_tag(:li, :class => classes, &content) - else - content_tag(:li, :class => classes) do - content_tag 'label', :class => 'checkbox' do - content = [] - content << content_tag('input', '', :type => 'checkbox', :checked => active, :name => "#{param_name}[]", :value => param_value) - content << title - - content.join("\n").html_safe - end - end - end - end - - def boot_nav_filter(name, entries, type = :link) - refresh_action = (type == :checkbox) - - content_tag(:ul, :class => ['nav', 'nav-list']) do - content = [] - content << boot_nav_header(name, refresh_action) - - entries.each do |entry| - if entry.is_a? Hash - title = entry[:title] - value = entry[:value] - else - title = value = entry - end - - case type - when :link - content << boot_nav_li_link(name, value, title) - when :checkbox - content << boot_nav_li_checkbox(name, value, title) - end - end - - content.join("\n").html_safe - end - end -end diff --git a/app/helpers/contextual_link_helpers.rb b/app/helpers/contextual_link_helpers.rb deleted file mode 100644 index 1933ecf..0000000 --- a/app/helpers/contextual_link_helpers.rb +++ /dev/null @@ -1,152 +0,0 @@ -module ContextualLinkHelpers - # CRUD helpers - def action_to_icon(action) - case action.to_s - when 'new' - "plus" - when 'show' - "eye-open" - when 'edit' - "edit" - when 'delete', 'destroy' - "trash" - when "index", "list" - "list-alt" - when "update" - "refresh" - when "copy" - "repeat" - else - action - end - end - - def icon_link_to(action, url = nil, options = {}) - classes = [] - if class_options = options.delete(:class) - classes << class_options.split(' ') - end - - classes << I18nRailsHelpers.contextual_link_class - - if action.is_a? Symbol - url ||= {:action => action} - title = t_action(action) - else - title = action - end - - icon = options.delete(:icon) - icon ||= action - - type = options.delete(:type) - classes << "btn-#{type}" unless type.blank? - - options.merge!(:class => classes.join(" ")) - link_to(url_for(url), options) do - boot_icon(action_to_icon(icon)) + " " + title - end - end - - def contextual_link_to(action, resource_or_model = nil, link_options = {}) - # We don't want to change the passed in link_options - options = link_options.dup - - # Handle both symbols and strings - action = action.to_sym - - # Resource and Model setup - # Use controller name to guess resource or model if not specified - case action - when :new, :index - default_model = controller_name.singularize.camelize.constantize - model = resource_or_model || default_model - explicit_resource_or_model = default_model != model - when :show, :edit, :delete, :destroy - default_resource = instance_variable_get("@#{controller_name.singularize}") - resource = resource_or_model || default_resource - model = resource.class - explicit_resource_or_model = default_resource != resource - end - model_name = model.to_s.underscore - - unless resource_or_model.is_a?(String) - # No link if CanCan is used and current user isn't authorized to call this action - return if respond_to?(:cannot?) and cannot?(action.to_sym, model) - end - - # Option generation - case action - when :delete, :destroy - options.merge!(:confirm => t_confirm_delete(resource), :method => :delete) - end - - begin - if resource_or_model.is_a?(String) - path = resource_or_model - else - # Path generation - case action - when :index - if explicit_resource_or_model - path = polymorphic_path(model) - else - path = url_for(:action => nil) - end - when :delete, :destroy - if explicit_resource_or_model - path = polymorphic_path(resource) - else - path = url_for(:action => :destroy) - end - else - if explicit_resource_or_model - path = polymorphic_path(resource_or_model, :action => action) - else - path = url_for(:action => action) - end - end - end - - return icon_link_to(action, path, options) - - rescue ActionController::UrlGenerationError - # This handles cases where we did exclude crud actions in the routing map. - end - end - - def contextual_links_for(action = nil, resource_or_model = nil, options = {}) - # Use current action if not specified - action ||= action_name - - # Handle both symbols and strings - action = action.to_sym - - actions = [] - case action - when :new, :create - actions << :index - when :show - actions += [:edit, :destroy, :index] - when :edit, :update - actions += [:show, :destroy, :index] - when :index - actions << :new - end - - links = actions.map{|link_for| contextual_link_to(link_for, resource_or_model, options)} - - return links.join("\n").html_safe - end - - def contextual_links(action = nil, resource_or_model = nil, options = {}, &block) - content_tag('div', :class => I18nRailsHelpers.contextual_class) do - content = contextual_links_for(action, resource_or_model, options) - if block_given? - additional_content = capture(&block) - content += ("\n" + additional_content).html_safe unless additional_content.nil? - end - content - end - end -end diff --git a/app/helpers/list_link_helpers.rb b/app/helpers/list_link_helpers.rb deleted file mode 100644 index ab900ae..0000000 --- a/app/helpers/list_link_helpers.rb +++ /dev/null @@ -1,76 +0,0 @@ -module ListLinkHelpers - include ContextualLinkHelpers - - # List link helpers - def list_link_to(action, url, options = {}) - icon = options.delete(:icon) - icon ||= action - - link_to(url_for(url), options) do - boot_icon action_to_icon(icon) - end - end - - def list_link_for(action, resource_or_model = nil, options = {}) - # Handle both symbols and strings - action = action.to_s - - # Resource and Model setup - # Support nested resources - if resource_or_model.is_a? Array - main_resource_or_model = resource_or_model.last - else - main_resource_or_model = resource_or_model - end - - # Use controller name to guess resource or model if not specified - case action - when 'new', 'index' - model = main_resource_or_model || controller_name.singularize.camelize.constantize - when 'show', 'edit', 'delete' - resource = main_resource_or_model || instance_variable_get("@#{controller_name.singularize}") - model = resource.class - end - model_name = model.to_s.underscore - - # No link if CanCan is used and current user isn't authorized to call this action - return if respond_to?(:can?) and cannot?(action.to_sym, model) - - # Link generation - case action - when 'index', 'show' - path = polymorphic_path(resource_or_model) - when 'delete' - path = polymorphic_path(resource_or_model) - options.merge!(:confirm => t_confirm_delete(main_resource_or_model), :method => :delete) - else - path = polymorphic_path(resource_or_model, :action => action) - end - - return list_link_to(action, path, options) - end - - def list_links_for(action = nil, resource_or_model = nil) - # Use current action if not specified - action ||= action_name - - # Handle both symbols and strings - action = action.to_s - - actions = [] - case action - when 'new', 'create' - actions << 'index' - when 'show' - actions += ['edit', 'delete', 'index'] - when 'edit', 'update' - actions += ['show', 'delete', 'index'] - when 'index' - actions << 'new' - end - - links = actions.map{|link_for| contextual_link_to(link_for, resource_or_model)} - - return links.join("\n").html_safe - end -end diff --git a/app/views/application/_list.html.haml b/app/views/application/_list.html.haml deleted file mode 100644 index a6e3837..0000000 --- a/app/views/application/_list.html.haml +++ /dev/null @@ -1,9 +0,0 @@ -= paginate collection if defined?(paginate) - -%table.table.table-striped.table-hover - %thead - %tr - = render 'list_header' - - %body - = render collection diff --git a/app/views/application/edit.html.haml b/app/views/application/edit.html.haml deleted file mode 100644 index a7e73f9..0000000 --- a/app/views/application/edit.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= contextual_links - -= boot_page_title - -= render 'form' diff --git a/app/views/application/index.html.haml b/app/views/application/index.html.haml deleted file mode 100644 index 15c9866..0000000 --- a/app/views/application/index.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= contextual_links - -= boot_page_title - -= render "list" diff --git a/app/views/application/new.html.haml b/app/views/application/new.html.haml deleted file mode 100644 index a7e73f9..0000000 --- a/app/views/application/new.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= contextual_links - -= boot_page_title - -= render 'form' diff --git a/app/views/application/show.html.haml b/app/views/application/show.html.haml deleted file mode 100644 index 1da6f0f..0000000 --- a/app/views/application/show.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= contextual_links - -= boot_page_title - -= render "show" diff --git a/lib/boot_form_builder.rb b/lib/boot_form_builder.rb deleted file mode 100644 index 1f41d7f..0000000 --- a/lib/boot_form_builder.rb +++ /dev/null @@ -1,9 +0,0 @@ -class BootFormBuilder < SimpleForm::FormBuilder - def buttons(*args, &block) - @template.content_tag 'div', class: 'form-groups' do - @template.content_tag 'div', class: ['col-sm-offset-3', 'col-sm-9'] do - button(:submit, *args, &block) - end - end - end -end diff --git a/lib/i18n_rails_helpers.rb b/lib/i18n_rails_helpers.rb index b801a3d..a94d294 100644 --- a/lib/i18n_rails_helpers.rb +++ b/lib/i18n_rails_helpers.rb @@ -9,8 +9,7 @@ module I18nRailsHelpers # Use this to configure I18nRailsHelpers in an initializer. # # I18nRailsHelpers.setup do |config| - # config.contextual_class = 'pull-right' - # config.contextual_link_class = 'btn-sm' + # config.option = 'value' # end # def self.setup diff --git a/lib/i18n_rails_helpers/engine.rb b/lib/i18n_rails_helpers/engine.rb index fb9b49a..0752305 100644 --- a/lib/i18n_rails_helpers/engine.rb +++ b/lib/i18n_rails_helpers/engine.rb @@ -6,8 +6,6 @@ module I18nRailsHelpers class Engine < Rails::Engine initializer 'i18n_rails_helpers.helper' do ActionView::Base.send :include, I18nHelpers - ActionView::Base.send :include, ContextualLinkHelpers - ActionView::Base.send :include, ListLinkHelpers ActionController::Base.class_eval { include ControllerHelpers } ActiveRecord::Base.class_eval do diff --git a/spec/helpers/contextual_link_helpers_spec.rb b/spec/helpers/contextual_link_helpers_spec.rb deleted file mode 100644 index 6d2e325..0000000 --- a/spec/helpers/contextual_link_helpers_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -require "rails_helper" - -describe ContextualLinkHelpers do - describe "#icon_link_to" do - it "adds class options" do - expect(helper.icon_link_to(:show, '/dummy', class: 'test')).to have_css('a.test') - end - end - - describe "#action_to_icon" do - it "returns the action parameter if no action matches" do - expect(helper.action_to_icon('unknown action')).to eq 'unknown action' - end - end - - describe "#contextual_links" do - it "should work with block returning nil" do - expect{ - helper.contextual_links('action') do - nil - end - }.to_not raise_exception - end - end -end diff --git a/spec/helpers/list_link_helpers_spec.rb b/spec/helpers/list_link_helpers_spec.rb deleted file mode 100644 index 223041e..0000000 --- a/spec/helpers/list_link_helpers_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -require "rails_helper" - -describe ListLinkHelpers do - describe "#list_link_to" do - it "adds class options" do - expect(helper.list_link_to(:show, '/dummy', class: 'test')).to have_css('a.test') - end - end -end