Module: Jets::Router::Dsl

Includes:
Mount
Included in:
RouteSet
Defined in:
lib/jets/router/dsl.rb,
lib/jets/router/dsl/mount.rb

Defined Under Namespace

Modules: Mount

Constant Summary collapse

HANDLED_BY_SCOPE =

Important: Options as, module, etc are handled by scope and should not be passed to the route

[:as, :module, :path, :shallow].freeze

Instance Method Summary collapse

Methods included from Mount

#mount, #mount_class_at, #mount_engine

Instance Method Details

#collection(&block) ⇒ Object



58
59
60
# File 'lib/jets/router/dsl.rb', line 58

def collection(&block)
  scope(from: :collection, &block)
end

#constraints(constraints, &block) ⇒ Object



50
51
52
# File 'lib/jets/router/dsl.rb', line 50

def constraints(constraints, &block)
  scope(from: :constraints, constraints: constraints, &block)
end

#create_route(options) ⇒ Object



44
45
46
47
48
# File 'lib/jets/router/dsl.rb', line 44

def create_route(options)
  one_apigw_method_for_all_routes_warning(options)
  route = Route.new(options, @scope)
  @routes << route
end

#defaults(data = {}, &block) ⇒ Object



62
63
64
# File 'lib/jets/router/dsl.rb', line 62

def defaults(data={}, &block)
  scope(from: :defaults, defaults: data, &block)
end

#each_resource(resource_name, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jets/router/dsl.rb', line 119

def each_resource(resource_name, options={})
  HANDLED_BY_SCOPE.each do |opt|
    options.delete(opt)
  end
  o = Resources::Options.new(resource_name, options)
  f = Resources::Filter.new(resource_name, options)

  # Looks a little weird with '' but the path is handled by the scope
  get '', o.build(:index) if f.yes?(:index) && !options[:singular_resource]
  post '', o.build(:create) if f.yes?(:create)
  get 'new', o.build(:new) if f.yes?(:new) && !api_mode?
  get 'edit', o.build(:edit) if f.yes?(:edit) && !api_mode?
  get '', o.build(:show) if f.yes?(:show)
  put '', o.build(:update) if f.yes?(:update)
  # post to update wont work with singular_resource because it's a route collision
  # Also makes it so that route.to is always changed
  # Leaving in case need it for some reason. Will remove later.
  # post '', o.build(:update) if f.yes?(:update) # for binary uploads
  patch '', o.build(:update) if f.yes?(:update)
  delete '', o.build(:destroy) if f.yes?(:destroy)
end

#match(path, options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/jets/router/dsl.rb', line 36

def match(path, options={})
  via = options.delete(:via) || :any
  Array(via).each do |http_method|
    http_method = :any if via == :all
    send http_method, path, options
  end
end

#member(&block) ⇒ Object



54
55
56
# File 'lib/jets/router/dsl.rb', line 54

def member(&block)
  scope(from: :member, &block)
end

#namespace(ns, &block) ⇒ Object



70
71
72
# File 'lib/jets/router/dsl.rb', line 70

def namespace(ns, &block)
  scope(from: :namespace, path: ns, module: ns, as: ns, &block)
end

#normalize_path_to_controller_map_option!(args, options) ⇒ Object

Normally first args is a String that is the path

get "/posts", to: "posts#index"

But it can also be a Hash that maps the path to the controller/action

get "/jets/info" => "jets/info#index"

This logic normalize options to support both cases.



26
27
28
29
30
31
32
33
34
# File 'lib/jets/router/dsl.rb', line 26

def normalize_path_to_controller_map_option!(args, options)
  if !options[:to] && !args.first.is_a?(String) && !args.first.is_a?(Symbol)
    map = options.find { |k,v| k.is_a?(String) }
    path, to = map[0], map[1]
    options[:to] = to
    options[:path] = path
    options.delete(path)
  end
end

#path(path, &block) ⇒ Object



66
67
68
# File 'lib/jets/router/dsl.rb', line 66

def path(path, &block)
  scope(from: :path, path: path, &block)
end

#resource(*args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/jets/router/dsl.rb', line 106

def resource(*args)
  options = args.extract_options!
  resource_names = args
  resource_names.each do |resource_name|
    scope(options.merge(from: :resource, resource_name: resource_name)) do
      each_resource(resource_name, options.merge(singular_resource: true))
      yield if block_given?
    end
  end
end

#resources(*args) ⇒ Object

resources macro expands to all the routes



95
96
97
98
99
100
101
102
103
104
# File 'lib/jets/router/dsl.rb', line 95

def resources(*args)
  options = args.extract_options!
  resource_names = args
  resource_names.each do |resource_name|
    scope(options.merge(from: :resources, resource_name: resource_name)) do
      each_resource(resource_name, options)
      yield if block_given?
    end
  end
end

#root(*args) ⇒ Object

root “posts#index” root to: “posts#index”



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/jets/router/dsl.rb', line 143

def root(*args)
  if args.size == 1
    options = args.first
    if options.is_a?(String) # root "posts#index"
      to = options
      options = {}
    elsif options.is_a?(Hash) # root to: "posts#index"
      to = options.delete(:to)
    end
  else
    to = args[0]
    options = args[1] || {}
  end

  http_method = options.delete(:via)
  http_method ||= Jets.config.api.cors ? :any : :get
  default = {path: '/', to: to, http_method: http_method, root: true}
  options = default.merge(options)
  create_route(options)
end

#scope(*args) ⇒ Object

Examples

scope :admin
scope path: :admin
scope 'admin', as: 'admin'


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jets/router/dsl.rb', line 82

def scope(*args)
  options = args.extract_options!
  path = args.first
  options[:path] = path.to_s if path

  root_level = @scope.nil?
  @scope = root_level ? Scope.new(options) : @scope.new(options)
  yield
ensure
  @scope = @scope.parent if @scope
end

#shallow(&block) ⇒ Object



74
75
76
# File 'lib/jets/router/dsl.rb', line 74

def shallow(&block)
  scope(from: :shallow, &block)
end