Class: ActionDispatch::Routing::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/filesystem_explorer/router.rb

Instance Method Summary collapse

Instance Method Details

#filesystem_explorer(options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/filesystem_explorer/router.rb', line 20

def filesystem_explorer(options = {}, &block)
  route_config = FilesystemExplorer::FilesystemRouteOptions.new

  # Copy all applicable options from the parameter hash to the object
  %w(path as url).each do |option|
    route_config.send option, options[option.to_sym] if options[option.to_sym] && route_config.respond_to?(option)
  end

  route_config.instance_eval &block if block

  FilesystemExplorer::Engine.filesystem_explorer_route_options[route_config.url] ||= route_config

  FilesystemExplorer.routes << route_config

  Rails.application.routes.draw do
    # Download route
    route_options = { "#{route_config.url}/*path/download" => "filesystem_explorer/application#download" }
    route_options[:as] = :"#{route_config.as}_download" if route_config.as
    get route_options

    # Dynamic route
    route_options = { "#{route_config.url}/*path" => "filesystem_explorer/application#index" }
    get route_options

    # Root route
    route_options = { "#{route_config.url}" => "filesystem_explorer/application#index" }
    route_options[:as] = :"#{route_config.as}" if route_config.as
    get route_options
  end
end

#filesystem_routes(&block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/filesystem_explorer/router.rb', line 4

def filesystem_routes(&block)
  FilesystemExplorer.routes.clear

  config_file_path = Rails.root.join('config', 'filesystem_explorer.yml')

  if File.exists?(config_file_path)
    config_data = YAML.load_file(config_file_path)

    config_data.each do |route|
      filesystem_explorer path: route[:path] || route['path'], as: route[:as] || route['as'], url: route[:url] || route['url']
    end
  end

  block.call(FilesystemExplorer::Engine.filesystem_explorer_route_options || {}) if block
end