Class: FoxPage::Router
- Inherits:
-
Object
- Object
- FoxPage::Router
- Defined in:
- lib/fox_page/router.rb
Constant Summary collapse
- RESOURCE_ACTIONS =
i[index show].freeze
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
Instance Method Summary collapse
- #draw_routes(&block) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
-
#map(*args) ⇒ Object
compatibility method for old route definition syntax.
- #resources(name, path: name, only: RESOURCE_ACTIONS) ⇒ Object
- #root(target, params: {}) ⇒ Object
-
#route(source, to:, params: {}, single_file: false) ⇒ Object
defines a new route.
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
15 16 17 |
# File 'lib/fox_page/router.rb', line 15 def initialize @routes = {} end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
13 14 15 |
# File 'lib/fox_page/router.rb', line 13 def routes @routes end |
Class Method Details
.draw_routes(&block) ⇒ Object
9 10 11 |
# File 'lib/fox_page/router.rb', line 9 def self.draw_routes(&block) new.draw_routes(&block) end |
Instance Method Details
#draw_routes(&block) ⇒ Object
19 20 21 22 |
# File 'lib/fox_page/router.rb', line 19 def draw_routes(&block) instance_eval(&block) routes end |
#map(*args) ⇒ Object
compatibility method for old route definition syntax
‘def map(mapping, params: {}, single_file: false)`
see Router#route for the new style
example DEPRECATED usage:
map "/uses" => "uses#index"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fox_page/router.rb', line 36 def map(*args) warn "Router#map is deprecated, use Router#route instead" mapping = args.shift rest = args.shift || {} params = rest.fetch(:params, {}) single_file = rest.fetch(:single_file, false) pp( mapping:, params:, single_file:, ) mapping.each do |path, target| routes[path] = parse_target(target, params: params, single_file: single_file) end end |
#resources(name, path: name, only: RESOURCE_ACTIONS) ⇒ Object
62 63 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 |
# File 'lib/fox_page/router.rb', line 62 def resources(name, path: name, only: RESOURCE_ACTIONS) actions = only.map(&:to_sym) base_name = name.to_s controller = controller_for(base_name) base_path = "/#{path}" # insert a route with id 0 as index if generate_all was specified, but without the /id in the path if actions.include?(:index) validate_controller_method(controller, :index) routes[base_path] = make_target( base_name: base_name, controller: controller, method_name: :index, params: { id: 0 }, generate_all: nil, generate_all_ids: false ) end actions.each do |action| method_name = action validate_controller_method(controller, method_name) generate_all = controller.instance_variable_get(:@__generate_all_for)&.[](method_name) generate_all_ids = controller.instance_variable_get(:@__generate_all_ids_for)&.[](method_name) route_path = method_name == :index ? base_path : "#{base_path}/#{method_name}" if generate_all # :show gets a pretty id, whereas :index (and others) get it prefixed with /page route_path = if generate_all_ids "#{base_path}/%<id>s" else method_name == :show ? "#{base_path}/%<id>s" : "#{route_path}/page/%<id>s" end end routes[route_path] ||= make_target( base_name: base_name, controller: controller, method_name: method_name, params: {}, generate_all: generate_all, generate_all_ids: generate_all_ids ) end end |
#root(target, params: {}) ⇒ Object
24 25 26 |
# File 'lib/fox_page/router.rb', line 24 def root(target, params: {}) routes["/"] = parse_target(target, params: params) end |
#route(source, to:, params: {}, single_file: false) ⇒ Object
defines a new route
example usage:
route "/uses", to: "uses#index"
56 57 58 |
# File 'lib/fox_page/router.rb', line 56 def route(source, to:, params: {}, single_file: false) routes[source] = parse_target(to, params: params, single_file: single_file) end |