Class: MuchRails::Action::Router
- Inherits:
-
BaseRouter
- Object
- BaseRouter
- MuchRails::Action::Router
- Defined in:
- lib/much-rails/action/router.rb
Defined Under Namespace
Classes: URL
Constant Summary
Constants inherited from BaseRouter
Instance Attribute Summary collapse
-
#controller_name ⇒ Object
readonly
Returns the value of attribute controller_name.
Attributes inherited from BaseRouter
#definitions, #name, #request_type_set, #url_set
Class Method Summary collapse
- .ACTION_CLASS_PARAM_NAME ⇒ Object
- .CONTROLLER_CALL_ACTION_METHOD_NAME ⇒ Object
- .CONTROLLER_NOT_FOUND_METHOD_NAME ⇒ Object
- .DEFAULT_CONTROLLER_NAME ⇒ Object
- .load(routes_file_name, controller_name: nil) ⇒ Object
- .url_class ⇒ Object
Instance Method Summary collapse
-
#apply_to(application_routes_draw_scope) ⇒ Object
(also: #draw)
Example: MyRouter = MuchRails::Action::Router.new { … } Rails.application.routes.draw do root “/” MyRouter.draw(self) end.
-
#initialize(name = nil, controller_name: nil, &block) ⇒ Router
constructor
A new instance of Router.
Methods inherited from BaseRouter
#base_url, #delete, #get, #patch, #path_for, #post, #put, #request_type, #unrouted_urls, #url, #url_class, #url_for, #validate!
Constructor Details
#initialize(name = nil, controller_name: nil, &block) ⇒ Router
Returns a new instance of Router.
52 53 54 55 56 |
# File 'lib/much-rails/action/router.rb', line 52 def initialize(name = nil, controller_name: nil, &block) super(name, &block) @controller_name = controller_name || self.class.DEFAULT_CONTROLLER_NAME end |
Instance Attribute Details
#controller_name ⇒ Object (readonly)
Returns the value of attribute controller_name.
50 51 52 |
# File 'lib/much-rails/action/router.rb', line 50 def controller_name @controller_name end |
Class Method Details
.ACTION_CLASS_PARAM_NAME ⇒ Object
21 22 23 |
# File 'lib/much-rails/action/router.rb', line 21 def self.ACTION_CLASS_PARAM_NAME :much_rails_action_class_name end |
.CONTROLLER_CALL_ACTION_METHOD_NAME ⇒ Object
13 14 15 |
# File 'lib/much-rails/action/router.rb', line 13 def self.CONTROLLER_CALL_ACTION_METHOD_NAME :much_rails_call_action end |
.CONTROLLER_NOT_FOUND_METHOD_NAME ⇒ Object
17 18 19 |
# File 'lib/much-rails/action/router.rb', line 17 def self.CONTROLLER_NOT_FOUND_METHOD_NAME :much_rails_not_found end |
.DEFAULT_CONTROLLER_NAME ⇒ Object
9 10 11 |
# File 'lib/much-rails/action/router.rb', line 9 def self.DEFAULT_CONTROLLER_NAME "application" end |
.load(routes_file_name, controller_name: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/much-rails/action/router.rb', line 29 def self.load(routes_file_name, controller_name: nil) if routes_file_name.to_s.strip.empty? raise( ArgumentError, "expected a routes file name, given `#{routes_file_name.inspect}`.", ) end file_path = ::Rails.root.join("config/routes/#{routes_file_name}.rb") unless file_path.exist? raise ArgumentError, "routes file `#{file_path.inspect}` does not exist." end new( routes_file_name.to_s.split(File::SEPARATOR).join("_"), controller_name: controller_name, ) do instance_eval(File.read(file_path), file_path.to_s, 1) end end |
Instance Method Details
#apply_to(application_routes_draw_scope) ⇒ Object Also known as: draw
Example:
MyRouter = MuchRails::Action::Router.new { ... }
Rails.application.routes.draw do
root "/"
MyRouter.draw(self)
end
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/much-rails/action/router.rb', line 64 def apply_to(application_routes_draw_scope) validate! draw_url_to = "#{controller_name}##{self.class.CONTROLLER_NOT_FOUND_METHOD_NAME}" draw_route_to = "#{controller_name}##{self.class.CONTROLLER_CALL_ACTION_METHOD_NAME}" definition_names = Set.new definitions.each do |definition| definition.request_type_actions.each do |request_type_action| application_routes_draw_scope.public_send( definition.http_method, definition.path, to: draw_route_to, as: (definition.name if definition_names.add?(definition.name)), defaults: definition.default_params.merge({ self.class.ACTION_CLASS_PARAM_NAME => request_type_action.class_name, "format" => request_type_action.format, }), constraints: request_type_action.constraints_lambda, ) end next unless definition.has_default_action_class_name? application_routes_draw_scope.public_send( definition.http_method, definition.path, to: draw_route_to, as: (definition.name if definition_names.add?(definition.name)), defaults: definition.default_params.merge({ self.class.ACTION_CLASS_PARAM_NAME => definition.default_action_class_name, "format" => definition.default_action_format, }), ) end # Draw each URL that doesn't have a route definition so that we can generate # them using MuchRails::RailsRoutes. unrouted_urls.each do |url| application_routes_draw_scope.get(url.path, to: draw_url_to, as: url.name) end end |