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.



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

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



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

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



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

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

#masked_authenticity_tokenObject



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

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

#protect_against_forgery?Boolean

Returns:

  • (Boolean)


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

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

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

for forgery protection



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

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
23
# File 'lib/jets/overrides/rails/url_helper.rb', line 8

def url_for(options = nil) # :nodoc:
  url = case options
        when String
          options
        when :back
          _back_url
        when ActiveRecord::Base
          _handle_model(options)
        when Array
          _handle_array(options)
        else
          raise ArgumentError, "Please provided a String or ActiveRecord model to link_to as the the second argument. The Jets link_to helper takes as the second argument."
        end

  add_stage_name(url)
end