Class: JsFromRoutes::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/js_from_routes/generator.rb

Overview

Internal: A presenter for an individual Rails action.

Instance Method Summary collapse

Constructor Details

#initialize(route, mappings:, controller:, index: 0) ⇒ Route

Returns a new instance of Route.



61
62
63
# File 'lib/js_from_routes/generator.rb', line 61

def initialize(route, mappings:, controller:, index: 0)
  @route, @mappings, @controller, @index = route, mappings, controller, index
end

Instance Method Details

#exportObject

Public: The ‘export` setting specified for the action.



66
67
68
# File 'lib/js_from_routes/generator.rb', line 66

def export
  @route.defaults[:export]
end

#helperObject

Public: The name of the JS helper for the action. Example: ‘destroyAll’



81
82
83
84
85
86
87
88
# File 'lib/js_from_routes/generator.rb', line 81

def helper
  action = @route.requirements.fetch(:action)
  if @index > 0
    action = @route.name&.sub(@controller.tr(":/", "_"), "") || "#{action}#{verb.titleize}"
  end
  helper = action.camelize(:lower)
  @mappings.fetch(helper, helper)
end

#inspectObject

Internal: Useful as a cache key for the route, and for debugging purposes.



91
92
93
# File 'lib/js_from_routes/generator.rb', line 91

def inspect
  "#{verb} #{helper} #{path}"
end

#pathObject

Public: The path for the action. Example: ‘/users/:id/edit’



76
77
78
# File 'lib/js_from_routes/generator.rb', line 76

def path
  @route.path.spec.to_s.chomp("(.:format)")
end

#verbObject

Public: The HTTP verb for the action. Example: ‘patch’



71
72
73
# File 'lib/js_from_routes/generator.rb', line 71

def verb
  @route.verb.split("|").last.downcase
end