Module: Coco::ActionViewHelper
- Extended by:
- ActionViewHelper
- Includes:
- ActionView::Helpers::UrlHelper
- Included in:
- ActionViewHelper
- Defined in:
- lib/coco/action_view_helper.rb
Constant Summary collapse
- STRINGIFIED_COMMON_METHODS =
{ get: "get", delete: "delete", patch: "patch", post: "post", put: "put" }.freeze
Instance Method Summary collapse
- #add_method_to_attributes!(html_options, method) ⇒ Object
- #convert_options_to_data_attributes(options, html_options) ⇒ Object
- #link_to_remote_options?(options) ⇒ Boolean
- #method_for_options(options) ⇒ Object
- #method_not_get_method?(method) ⇒ Boolean
- #method_tag(method) ⇒ Object
- #remove_trailing_slash!(url_string) ⇒ Object
-
#to_form_params(attribute, namespace = nil) ⇒ Object
Returns an array of hashes each containing :name and :value keys suitable for use as the names and values of form input fields:.
- #token_tag(token = nil, form_options: {}) ⇒ Object
- #url_target(name, options) ⇒ Object
Instance Method Details
#add_method_to_attributes!(html_options, method) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/coco/action_view_helper.rb', line 35 def add_method_to_attributes!(, method) if method_not_get_method?(method) && !["rel"]&.match?(/nofollow/) ["rel"] = if ["rel"].blank? "nofollow" else "#{["rel"]} nofollow" end end ["data-method"] = method end |
#convert_options_to_data_attributes(options, html_options) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/coco/action_view_helper.rb', line 6 def (, ) if = .stringify_keys ["data-remote"] = "true" if () || () method = .delete("method") add_method_to_attributes!(, method) if method else () ? {"data-remote" => "true"} : {} end end |
#link_to_remote_options?(options) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/coco/action_view_helper.rb', line 29 def () if .is_a?(Hash) .delete("remote") || .delete(:remote) end end |
#method_for_options(options) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/coco/action_view_helper.rb', line 46 def () if .is_a?(Array) (.last) elsif .respond_to?(:persisted?) .persisted? ? :patch : :post elsif .respond_to?(:to_model) (.to_model) end end |
#method_not_get_method?(method) ⇒ Boolean
64 65 66 67 |
# File 'lib/coco/action_view_helper.rb', line 64 def method_not_get_method?(method) return false unless method (STRINGIFIED_COMMON_METHODS[method] || method.to_s.downcase) != "get" end |
#method_tag(method) ⇒ Object
83 84 85 |
# File 'lib/coco/action_view_helper.rb', line 83 def method_tag(method) tag("input", type: "hidden", name: "_method", value: method.to_s, autocomplete: "off") end |
#remove_trailing_slash!(url_string) ⇒ Object
129 130 131 132 |
# File 'lib/coco/action_view_helper.rb', line 129 def remove_trailing_slash!(url_string) trailing_index = (url_string.index("?") || 0) - 1 url_string[trailing_index] = "" if url_string[trailing_index] == "/" end |
#to_form_params(attribute, namespace = nil) ⇒ Object
Returns an array of hashes each containing :name and :value keys suitable for use as the names and values of form input fields:
to_form_params(name: 'David', nationality: 'Danish')
# => [{name: 'name', value: 'David'}, {name: 'nationality', value: 'Danish'}]
to_form_params(country: { name: 'Denmark' })
# => [{name: 'country[name]', value: 'Denmark'}]
to_form_params(countries: ['Denmark', 'Sweden']})
# => [{name: 'countries[]', value: 'Denmark'}, {name: 'countries[]', value: 'Sweden'}]
An optional namespace can be passed to enclose key names:
to_form_params({ name: 'Denmark' }, 'country')
# => [{name: 'country[name]', value: 'Denmark'}]
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/coco/action_view_helper.rb', line 103 def to_form_params(attribute, namespace = nil) attribute = if attribute.respond_to?(:permitted?) attribute.to_h else attribute end params = [] case attribute when Hash attribute.each do |key, value| prefix = namespace ? "#{namespace}[#{key}]" : key params.push(*to_form_params(value, prefix)) end when Array array_prefix = "#{namespace}[]" attribute.each do |value| params.push(*to_form_params(value, array_prefix)) end else params << {name: namespace.to_s, value: attribute.to_param} end params.sort_by { |pair| pair[:name] } end |
#token_tag(token = nil, form_options: {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/coco/action_view_helper.rb', line 69 def token_tag(token = nil, form_options: {}) if token != false && defined?(protect_against_forgery?) && protect_against_forgery? token = if token == true || token.nil? form_authenticity_token(form_options: .merge(authenticity_token: token)) else token end tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token, autocomplete: "off") else "" end end |
#url_target(name, options) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/coco/action_view_helper.rb', line 21 def url_target(name, ) if name.respond_to?(:model_name) && .is_a?(Hash) && .empty? url_for(name) else url_for() end end |