Class: Jets::Router::Route::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/router/route/info.rb

Overview

Organize to.controller and to.action into a class so don’t have to keep repeating the logic.

Instance Method Summary collapse

Constructor Details

#initialize(options, scope) ⇒ Info

Returns a new instance of Info.



5
6
7
# File 'lib/jets/router/route/info.rb', line 5

def initialize(options, scope)
  @options, @scope = options, scope
end

Instance Method Details

#actionObject

IE: index



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jets/router/route/info.rb', line 37

def action
  if @options.key?(:action)
    @options[:action].to_s
  elsif is_collection?(@scope) || is_member?(@scope)
    @options[:path].to_s.delete_prefix('/') # action
  elsif @options[:on] && @options[:on] != :member && @options[:on] != :collection
    @options[:on].to_s
  elsif @options[:to]
    @options[:to].split('#').last
  else # {"/"=>"jets/welcome#index", :internal=>true}
    map = @options.find { |k,v| k.is_a?(String) }
    path, to = map[0], map[1]
    to.split('#').last
  end
end

#controllerObject

IE: posts



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jets/router/route/info.rb', line 10

def controller
  return if @options[:engine] # IE: { engine: Blorgh::Engine, path: "/blog" }

  controller = if @options[:controller]
      @options[:controller]
    elsif @options[:to]
      @options[:to].split('#').first # IE: posts#index => posts
    elsif @scope.virtual_controller
      @scope.virtual_controller
    else # {"/jets/info/properties"=>"jets/info#properties"}
      map = @options.find { |k,v| k.is_a?(String) }
      path, to = map[0], map[1]
      to.split('#').first
    end

  if controller.starts_with?('/')
    # absolute controller path specified. use without scope adjustments
    return controller.delete_prefix('/') # remove leading slash
  end

  # no controller found yet, imply from the scope
  segments = scoped_module_segments
  segments << controller if controller
  segments.compact.join('/') # add module
end

#is_collection?(scope) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/jets/router/route/info.rb', line 65

def is_collection?(scope)
  scope.from == :collection || @options[:on] == :collection
end

#is_member?(scope) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/jets/router/route/info.rb', line 69

def is_member?(scope)
  scope.from == :member || @options[:on] == :member
end

#scoped_module_segmentsObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jets/router/route/info.rb', line 53

def scoped_module_segments
  segments = []
  @scope.from_top.each do |scope|
    segments << scope.module if scope.module
    if @scope == scope # last scope node
      last_segment = @options[:module]
      segments << last_segment if last_segment
    end
  end
  segments
end