Skip to content

Commit

Permalink
Clean up view helpers, aka fix bugs by following convention
Browse files Browse the repository at this point in the history
* don't nest view helpers in module for no reason
* rename view helpers per the convention
* add explicit csrf tags helper instead of applicationhelper
* isolate_namespace !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • Loading branch information
corytheboyd-cirrusmd committed Jun 25, 2021
1 parent 5b8aeb4 commit 59ed4ab
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 72 deletions.
17 changes: 17 additions & 0 deletions app/helpers/adminsimple/flash_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Adminsimple
module FlashHelper
def flash_messages
messages = ''
[:alert, :notice].each do |prop|
messages << flash_message(prop) if flash[prop].present?
end
messages.html_safe
end

def flash_message(prop = :notice)
close_button = content_tag(:button, '&times;'.html_safe, class: 'close', data: {dismiss: 'alert'})
content = content_tag(:strong, "#{prop.to_s.titleize}! ") + flash[prop]
content_tag(:div, "#{content}#{close_button}".html_safe, class: "alert alert-#{prop}")
end
end
end
8 changes: 0 additions & 8 deletions app/helpers/adminsimple/modules/application_helper.rb

This file was deleted.

11 changes: 0 additions & 11 deletions app/helpers/adminsimple/modules/page_header_helpers.rb

This file was deleted.

23 changes: 0 additions & 23 deletions app/helpers/adminsimple/modules/widget_box_helpers.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/helpers/adminsimple/page_header_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Adminsimple
module PageHeaderHelper
def page_header(options = {}, &block)
options[:id] ||= "adminsimple_page_header"
options[:title] ||= controller.controller_name.to_s
content = block ? capture(&block) : nil
locals = {content: content, title: options.delete(:title)}
content_tag(:div, render(partial: 'adminsimple/modules/page_header', locals: locals), options)
end
end
end
10 changes: 10 additions & 0 deletions app/helpers/adminsimple/rescued_csrf_meta_tags_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Adminsimple
module RescuedCsrfMetaTagsHelper
def rescued_csrf_meta_tags
csrf_meta_tags
rescue ArgumentError
request.reset_session
csrf_meta_tags
end
end
end
17 changes: 0 additions & 17 deletions app/helpers/adminsimple/view_helpers.rb

This file was deleted.

23 changes: 23 additions & 0 deletions app/helpers/adminsimple/widget_box_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Adminsimple
module WidgetBoxHelper
def widget_box(options = {}, &block)
options[:class] = options[:class] ? "#{options[:class]} adminsimple-widget-box" : 'adminsimple-widget-box'

if options[:icon] == :checkbox
options[:icon] = content_tag(:input, '', type: 'checkbox')
elsif options[:icon].present?
options[:icon] = content_tag(:i, '', class: "icon-#{options[:icon]}")
end

if options[:label].is_a?(Hash)
labels = options[:label].map { |key, value| content_tag(:span, value, class: "label label-#{key}") }
options[:label] = labels.join('').html_safe
elsif options[:label].present?
options[:label] = content_tag(:span, options[:label], class: 'label')
end

locals = {content: capture(&block), title: options.delete(:title), icon: options.delete(:icon), label: options.delete(:label)}
content_tag(:div, render(partial: 'adminsimple/modules/widget_box', locals: locals), options)
end
end
end
14 changes: 1 addition & 13 deletions lib/adminsimple/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

module Adminsimple
class Engine < ::Rails::Engine
engine_name 'adminsimple'

initializer "adminsimple.view_helpers" do

ActiveSupport.on_load :action_view do
Rails.application.reloader.to_prepare do
ActionView::Base.send :include, Adminsimple::ViewHelpers
ActionView::Base.send :include, Adminsimple::Modules::WidgetBoxHelpers
ActionView::Base.send :include, Adminsimple::Modules::PageHeaderHelpers
end
end
end

isolate_namespace Adminsimple
end
end

0 comments on commit 59ed4ab

Please sign in to comment.