Module: Jets::UrlHelper

Includes:
CommonMethods
Defined in:
lib/jets/overrides/rails/url_helper.rb

Overview

hackety hack

Instance Method Summary collapse

Methods included from CommonMethods

#add_stage_name, #on_aws?

Instance Method Details

#_handle_array(array) ⇒ Object

Convention is that the model class name is the method name. Doesnt work if user is using as.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jets/overrides/rails/url_helper.rb', line 36

def _handle_array(array)
  contains_nil = !array.select(&:nil?).empty?
  if contains_nil
    raise "ERROR: You passed a nil value in the Array. #{array.inspect}."
  end

  last_persisted = nil
  items = array.map do |x|
    if x.is_a?(ActiveRecord::Base)
      last_persisted = x.persisted?
      x.persisted? ? x.model_name.singular_route_key : x.model_name.route_key
    else
      x
    end
  end
  meth = items.join('_') + "_path"

  args = array.clone
  args.shift if args.first.is_a?(Symbol) # drop the first element if its a symbol
  args = last_persisted ? args : args[0..-2]

  # post_comment_path(post_id) - keep all args - for update
  # post_comments_path - drop last arg - for create
  send(meth, *args)
end

#_handle_model(record) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/jets/overrides/rails/url_helper.rb', line 24

def _handle_model(record)
  model = record.to_model
  if model.persisted?
    meth = model.model_name.singular_route_key + "_path"
    send(meth, record) # Example: post_path(record)
  else
    meth = model.model_name.route_key + "_path"
    send(meth) # Example: posts_path
  end
end

#csrf_meta_tagsObject



78
79
80
81
82
# File 'lib/jets/overrides/rails/url_helper.rb', line 78

def csrf_meta_tags
  if protect_against_forgery?
    tag("meta", name: "csrf-token", content: masked_authenticity_token).html_safe
  end
end

#masked_authenticity_tokenObject



69
70
71
72
# File 'lib/jets/overrides/rails/url_helper.rb', line 69

def masked_authenticity_token
  @masked_authenticity_token ||= SecureRandom.hex(32)
  session[:authenticity_token] = @masked_authenticity_token
end

#protect_against_forgery?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/jets/overrides/rails/url_helper.rb', line 74

def protect_against_forgery?
  @_jets[:controller].class.forgery_protection_enabled?
end

#token_tag(token = nil, form_options: {}) ⇒ Object

for forgery protection



63
64
65
66
67
# File 'lib/jets/overrides/rails/url_helper.rb', line 63

def token_tag(token = nil, form_options: {})
  return '' unless protect_against_forgery?

  hidden_field_tag 'authenticity_token', masked_authenticity_token
end

#url_for(options = nil) ⇒ Object

Basic implementation of url_for to allow use helpers without routes existence



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jets/overrides/rails/url_helper.rb', line 8

def url_for(options = nil) # :nodoc:
  url = if options.is_a?(String)
          options
        elsif options == :back
          _back_url
        elsif options.respond_to?(:to_model)
          _handle_model(options)
        elsif options.is_a?(Array)
          _handle_array(options)
        else
          raise ArgumentError, "The Jets link_to helper only supports some types of arguments. Please provided a String or an object that supports ActiveModel to link_to as the the second argument."
        end

  add_stage_name(url)
end