Class: Jets::Router::Route::Info
- Inherits:
-
Object
- Object
- Jets::Router::Route::Info
- 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
-
#action ⇒ Object
IE: index.
-
#controller ⇒ Object
IE: posts.
-
#initialize(options, scope) ⇒ Info
constructor
A new instance of Info.
- #is_collection?(scope) ⇒ Boolean
- #is_member?(scope) ⇒ Boolean
- #scoped_module_segments ⇒ Object
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(, scope) @options, @scope = , scope end |
Instance Method Details
#action ⇒ Object
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 |
#controller ⇒ Object
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
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
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_segments ⇒ Object
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 |