Module: Fortitude::Rails::Helpers

Defined in:
lib/fortitude/rails/helpers.rb

Constant Summary collapse

ALL_BUILTIN_HELPER_MODULES =
{
  ActionView::Helpers => %w{
    ActiveModelHelper
    ActiveModelInstanceTag
    AssetTagHelper
    AssetUrlHelper
    AtomFeedHelper
    CacheHelper
    CaptureHelper
    CsrfHelper
    DateHelper
    DebugHelper
    FormHelper
    FormOptionsHelper
    FormTagHelper
    JavaScriptHelper
    NumberHelper
    OutputSafetyHelper
    RecordTagHelper
    SanitizeHelper
    TagHelper
    TextHelper
    TranslationHelper
    UrlHelper
  }
}
FORM_FOR_YIELDED_METHODS_TO_OUTPUT =

form_helper

%w{button check_box color_field date_field datetime_field datetime_local_field} +
%w{email_field file_field hidden_field label month_field number_field password_field phone_field} +
%w{radio_button range_field search_field submit telephone_field text_area text_field time_field url_field} +
%w{week_field} +

# From form_options_helper
%w{select collection_select grouped_collection_select time_zone_select options_for_select} +
%w{options_from_collection_for_select option_groups_from_collection_for_select grouped_options_for_select} +
%w{time_zone_options_for_select collection_radio_buttons collection_check_boxes} +

# And these can nest inside each other
%w{form_for fields_for}

Class Method Summary collapse

Class Method Details

.apply_refined_helpers_to!(o) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fortitude/rails/helpers.rb', line 14

def apply_refined_helpers_to!(o)
  @helpers.each do |name, options|
    o.helper(name, options)
  end

  url_helpers_module = ::Rails.application.routes.url_helpers

  test_base_class = Class.new
  test_base_instance = test_base_class.new

  test_class = Class.new
  test_class.send(:include, url_helpers_module)
  test_instance = test_class.new

  metaclass = o.instance_eval("class << self; self; end")

  metaclass.send(:define_method, :_fortitude_allow_helper_even_without_automatic_helper_access?) do |method_name|
    test_instance.respond_to?(method_name) && (! test_base_instance.respond_to?(method_name))
  end
end

.declare_all_builtin_rails_helpers!Object

We could use the mechanism used above for the url_helpers_module, but this makes access to these helpers much faster – they end up with real methods defined for them, instead of using method_missing magic every time. We don’t necessarily want to do that for the url_helpers_module, because there can be tons and tons of methods in there…and because we initialize early-enough on that methods aren’t defined there yet, anyway.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fortitude/rails/helpers.rb', line 67

def declare_all_builtin_rails_helpers!
  ALL_BUILTIN_HELPER_MODULES.each do |base_module, constant_names|
    constant_names.each do |constant_name|
      if base_module.const_defined?(constant_name)
        helper_module = base_module.const_get(constant_name)
        helper_module.public_instance_methods.each do |helper_method_name|
          # This is because ActionView::Helpers::FormTagHelper exposes #embed_authenticity_token_in_remote_forms=
          # as a public instance method. This seems like it should not be included as a helper.
          # next if helper_method_name.to_s == 'embed_authenticity_token_in_remote_forms='
          helper helper_method_name
        end
      end
    end
  end

  helper :default_url_options
end

.helper(name, options = { }) ⇒ Object



5
6
7
8
# File 'lib/fortitude/rails/helpers.rb', line 5

def helper(name, options = { })
  @helpers ||= { }
  @helpers[normalize_helper_name(name)] = options
end

.helper_options(name) ⇒ Object



10
11
12
# File 'lib/fortitude/rails/helpers.rb', line 10

def helper_options(name)
  @helpers[normalize_helper_name(name)]
end