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 = {}) ⇒ Route

Returns a new instance of Route.



36
37
38
# File 'lib/js_from_routes/generator.rb', line 36

def initialize(route, mappings = {})
  @route, @mappings = route, mappings
end

Instance Method Details

#export_settingObject

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



46
47
48
# File 'lib/js_from_routes/generator.rb', line 46

def export_setting
  @route.defaults[:export]
end

#helperObject

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



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

def helper
  action = @route.requirements.fetch(:action).camelize(:lower)
  name = @mappings.fetch(action, action)
  path_only? ? "#{ name }Path" : name
end

#inspectObject

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



73
74
75
# File 'lib/js_from_routes/generator.rb', line 73

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

#pathObject

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



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

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

#path_only?Boolean

Public: Whether it should export only the path.

Returns:

  • (Boolean)


41
42
43
# File 'lib/js_from_routes/generator.rb', line 41

def path_only?
  export_setting == :path_only
end

#request_method?Boolean

Public: Whether it should export only the path.

Returns:

  • (Boolean)


51
52
53
# File 'lib/js_from_routes/generator.rb', line 51

def request_method?
  !path_only?
end

#verbObject

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



56
57
58
# File 'lib/js_from_routes/generator.rb', line 56

def verb
  @route.verb.downcase
end