Module: Jets::Router::Route::Path

Included in:
Jets::Router::Route
Defined in:
lib/jets/router/route/path.rb

Instance Method Summary collapse

Instance Method Details

#add_path_suffix(all_segments) ⇒ Object

Accounts for path names options map. Example:

{path_names: {new: "sign_up", edit: "edit"}}

new posts/new => posts/sign_up edit posts/:id/edit => posts/:id/edit



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jets/router/route/path.rb', line 47

def add_path_suffix(all_segments)
  # rip apart last path as segments
  # work with and modify segments to be returned
  segments = all_segments.last.to_s.split('/')
  current_suffix = segments.last

  new_or_edit = %w[new edit].include?(current_suffix)
  additional_action = @options[:on].is_a?(Symbol) && @options[:on] != :member && @options[:on] != :collection
  will_replace = new_or_edit || additional_action

  if will_replace
    segments_without_last = all_segments[0..-2]
    # reassemble path with additional on action
    segments_without_last << @options[:on].to_s if additional_action
    # reassemble with new suffix from path_names map
    new_suffix = @path_names[current_suffix.to_sym] || current_suffix
    segments_without_last << new_suffix
    new_last = segments_without_last.join('/')
    segments[-1] = new_last # replace
    segments
  else
    all_segments # original
  end
end

#format_path(format, path) ⇒ Object

IE: standard: posts/:id/edit

api_gateway: posts/{id}/edit


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jets/router/route/path.rb', line 80

def format_path(format, path)
  path = case format
  when :api_gateway
    api_gateway_format(path)
  when :raw
    path
  else # jets format
    ensure_jets_format(path)
  end
  path = "/#{path}" unless path.starts_with?('/') # ensure starts with / be forgiving if / accidentally not included
  path
end

#path(format = :jets) ⇒ Object

Note: The @options is missing prefix and is not support via direct create_route. This is because it can be added directly to the path. IE:

get "myprefix/posts", to: "posts#index"

Also, this helps to keep the method creator logic simpler.



10
11
12
13
14
15
# File 'lib/jets/router/route/path.rb', line 10

def path(format=:jets)
  segments = path_prefixes + [path_option]
  segments = add_path_suffix(segments)
  path = segments.reject(&:blank?).join('/')
  format_path(format, path)
end

#path_optionObject



33
34
35
36
37
38
# File 'lib/jets/router/route/path.rb', line 33

def path_option
  node = Node.new(self, @scope)
  path = @options[:path].to_s.delete_prefix('/') # IE: new or edit
  path.sub!(@scope.param_placeholder, ":#{node.resolved_param}")
  path
end

#path_prefixesObject

When scope used directly. IE: not coming from namespace or resources When coming from namespace or resources, the path is already accounted for.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jets/router/route/path.rb', line 19

def path_prefixes
  list = []
  @scope.from_top.each do |scope|
    node = Node.new(self, scope)
    # Update @path_names as we walk down
    @path_names.merge!(scope.options[:path_names] || {})

    list << scope.resolved_path if node.append_path?
    list << node.resolved_param if node.append_param?
  end
  list.reject!(&:blank?) # allows for path: '' to remove resource name from path
  list
end

#path_suffixesObject



72
73
74
75
76
# File 'lib/jets/router/route/path.rb', line 72

def path_suffixes
  list = []
  list << action_suffix if action_suffix
  list
end