Module: Jets::Router::Dsl

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

Defined Under Namespace

Modules: Mount

Instance Method Summary collapse

Methods included from Mount

#mount

Instance Method Details

#collectionObject



117
118
119
120
121
# File 'lib/jets/router/dsl.rb', line 117

def collection
  @on_option = :collection
  yield
  @on_option = nil
end

#default_param(has_block, name, options) ⇒ Object

If a block has pass then we assume the resources will be nested and then prefix the param name with the resource. IE: post_id instead of id This avoids an API Gateway parent sibling variable collision.



126
127
128
129
# File 'lib/jets/router/dsl.rb', line 126

def default_param(has_block, name, options)
  default_param = has_block ? "#{name.to_s.singularize}_id".to_sym : :id
  options[:param] || default_param
end

#each_resource(name, options = {}, has_block = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jets/router/dsl.rb', line 97

def each_resource(name, options={}, has_block=nil)
  o = Resources::Options.new(name, options.merge(singular_resource: true))
  f = Resources::Filter.new(name, options)

  get "#{name}/new", o.build(:new) if f.yes?(:new) && !api_mode?
  get name, o.build(:show) if f.yes?(:show)
  post name, o.build(:create) if f.yes?(:create)
  get "#{name}/edit", o.build(:edit) if f.yes?(:edit) && !api_mode?
  put name, o.build(:update) if f.yes?(:update)
  post name, o.build(:update) if f.yes?(:update) # for binary uploads
  patch name, o.build(:update) if f.yes?(:update)
  delete name, o.build(:delete) if f.yes?(:delete)
end

#each_resources(name, options = {}, has_block = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jets/router/dsl.rb', line 70

def each_resources(name, options={}, has_block=nil)
  o = Resources::Options.new(name, options)
  f = Resources::Filter.new(name, options)
  param = default_param(has_block, name, options)

  get name, o.build(:index) if f.yes?(:index)
  get "#{name}/new", o.build(:new) if f.yes?(:new) && !api_mode?
  get "#{name}/:#{param}", o.build(:show) if f.yes?(:show)
  post name, o.build(:create) if f.yes?(:create)
  get "#{name}/:#{param}/edit", o.build(:edit) if f.yes?(:edit) && !api_mode?
  put "#{name}/:#{param}", o.build(:update) if f.yes?(:update)
  post "#{name}/:#{param}", o.build(:update) if f.yes?(:update) # for binary uploads
  patch "#{name}/:#{param}", o.build(:update) if f.yes?(:update)
  delete "#{name}/:#{param}", o.build(:delete) if f.yes?(:delete)
end

#memberObject



111
112
113
114
115
# File 'lib/jets/router/dsl.rb', line 111

def member
  @on_option = :member
  yield
  @on_option = nil
end

#namespace(ns, &block) ⇒ Object



12
13
14
# File 'lib/jets/router/dsl.rb', line 12

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

#prefix(prefix, &block) ⇒ Object



16
17
18
# File 'lib/jets/router/dsl.rb', line 16

def prefix(prefix, &block)
  scope(prefix: prefix, &block)
end

#resource(*items, **options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/jets/router/dsl.rb', line 86

def resource(*items, **options)
  items.each do |item|
    scope_options = scope_options!(item, options)
    scope_options[:from] = :resource # flag for MethodCreator logic: to handle method_name_leaf and more
    scope(scope_options) do
      each_resource(item, options, block_given?)
      yield if block_given?
    end
  end
end

#resources(*items, **options) ⇒ Object

resources macro expands to all the routes



42
43
44
45
46
47
48
49
50
51
# File 'lib/jets/router/dsl.rb', line 42

def resources(*items, **options)
  items.each do |item|
    scope_options = scope_options!(item, options)
    scope_options[:from] = :resources # flag for MethodCreator logic: to handle method_name_leaf and more
    scope(scope_options) do
      each_resources(item, options, block_given?)
      yield if block_given?
    end
  end
end

#root(to, options = {}) ⇒ Object

root “posts#index”



132
133
134
135
136
137
# File 'lib/jets/router/dsl.rb', line 132

def root(to, options={})
  default = {path: '', to: to, method: :get, root: true}
  options = default.merge(options)
  MethodCreator.new(options, @scope).create_root_helper
  @routes << Route.new(options, @scope)
end

#scope(args) ⇒ Object

scope supports three options: module, prefix and as. Jets vs Rails:

module - module
prefix - path
as - as


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jets/router/dsl.rb', line 25

def scope(args)
  # normalizes `scope(:admin)` as `scope(prefix: :admin)`
  options = case args
  when Hash
    args
  when String, Symbol
    { prefix: args }
  end

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

#scope_options!(item, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jets/router/dsl.rb', line 53

def scope_options!(item, options)
  prefix = if options[:prefix]
    # prefix given from the resources macro get automatically prepended to the item name
    p = options.delete(:prefix)
    "#{p}/#{item}"
  else
    item
  end

  {
    as: options.delete(:as) || item, # delete as or it messes with create_route
    prefix: prefix,
    param: options[:param],
    # module: options.delete(:module) || item, # NOTE: resources does not automatically set module, but namespace does
  }
end