Module: Jets::Router::Dsl

Defined in:
lib/kingsman/jets/routes.rb

Instance Method Summary collapse

Instance Method Details

#kingsman_confirmation(mapping, controllers) ⇒ Object

:nodoc:



72
73
74
75
# File 'lib/kingsman/jets/routes.rb', line 72

def kingsman_confirmation(mapping, controllers) #:nodoc:
  resource :confirmation, only: [:new, :create, :show],
    path: mapping.path_names[:confirmation], controller: controllers[:confirmations]
end

#kingsman_for(*resources) ⇒ Object



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
50
51
52
53
54
55
56
57
# File 'lib/kingsman/jets/routes.rb', line 25

def kingsman_for(*resources)
  options = resources.extract_options!

  # options[:as]          ||= @scope[:as]     if @scope[:as].present?
  # options[:module]      ||= @scope[:module] if @scope[:module].present?
  # options[:path_prefix] ||= @scope[:path]   if @scope[:path].present?
  # options[:path_names]    = (@scope[:path_names] || {}).merge(options[:path_names] || {})
  # options[:constraints]   = (@scope[:constraints] || {}).merge(options[:constraints] || {})
  # options[:defaults]      = (@scope[:defaults] || {}).merge(options[:defaults] || {})
  # options[:options]       = @scope[:options] || {}
  # options[:options][:format] = false if options[:format] == false

  resources.map!(&:to_sym)

  resources.each do |resource|
    mapping = Kingsman.add_mapping(resource, options)

    if options[:controllers] && options[:controllers][:omniauth_callbacks]
      unless mapping.omniauthable?
        raise ArgumentError, "Mapping omniauth_callbacks on a resource that is not omniauthable\n" \
          "Please add `kingsman :omniauthable` to the `#{mapping.class_name}` model"
      end
    end

    routes = mapping.used_routes

    kingsman_scope mapping.name do
      with_kingsman_exclusive_scope mapping.fullpath, mapping.name, options do
        routes.each { |mod| send("kingsman_#{mod}", mapping, mapping.controllers) }
      end
    end
  end
end

#kingsman_omniauth_callback(mapping, controllers) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kingsman/jets/routes.rb', line 103

def kingsman_omniauth_callback(mapping, controllers) #:nodoc:
  if mapping.fullpath =~ /:[a-zA-Z_]/
    raise <<-ERROR
Kingsman does not support scoping OmniAuth callbacks under a dynamic segment
and you have set #{mapping.fullpath.inspect}. You can work around by passing
`skip: :omniauth_callbacks` to the `kingsman_for` call and extract omniauth
options to another `kingsman_for` call outside the scope. Here is an example:

  kingsman_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'users/omniauth_callbacks'}

  scope '/(:locale)', locale: /ru|en/ do
kingsman_for :users, skip: :omniauth_callbacks
  end
ERROR
  end

  current_scope = @scope.dup

  # Jets routes are all very lazily evaluated.  This is a problem for Kingsman
  # because devise sets the scope[:path] to nil and expects the call to match
  # to use it as it's evaluated. And then it restores it to the previous value.
  # This is a problem for Jets because Jets routes are lazily evaluated and
  # none of this will matter. Unsure how to handle at the moment.

  if @scope.respond_to? :new
    @scope = @scope.new path: nil
  else
    @scope[:path] = nil
  end

  path_prefix = Kingsman.omniauth_path_prefix || "/#{mapping.fullpath}/auth".squeeze("/")
  set_omniauth_path_prefix!(path_prefix)
  path_prefix = "/auth" # HACK

  mapping.to.omniauth_providers.each do |provider|
    match "#{path_prefix}/#{provider}",
      to: "#{controllers[:omniauth_callbacks]}#passthru",
      as: "#{provider}_omniauth_authorize",
      via: OmniAuth.config.allowed_request_methods
      # via: [:get, :post]

    match "#{path_prefix}/#{provider}/callback",
      to: "#{controllers[:omniauth_callbacks]}##{provider}",
      as: "#{provider}_omniauth_callback",
      via: [:get, :post]
  end
ensure
  @scope = current_scope
end

#kingsman_password(mapping, controllers) ⇒ Object

:nodoc:



67
68
69
70
# File 'lib/kingsman/jets/routes.rb', line 67

def kingsman_password(mapping, controllers) #:nodoc:
  resource :password, only: [:new, :create, :edit, :update],
    path: mapping.path_names[:password], controller: controllers[:passwords]
end

#kingsman_registration(mapping, controllers) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kingsman/jets/routes.rb', line 84

def kingsman_registration(mapping, controllers) #:nodoc:
  path_names = {
    new: mapping.path_names[:sign_up],
    edit: mapping.path_names[:edit],
    cancel: mapping.path_names[:cancel]
  }

  options = {
    only: [:new, :create, :edit, :update, :destroy],
    path: mapping.path_names[:registration],
    path_names: path_names,
    controller: controllers[:registrations]
  }

  resource :registration, options do
    get :cancel
  end
end

#kingsman_scope(scope) ⇒ Object Also known as: as



165
166
167
168
169
170
171
172
173
174
# File 'lib/kingsman/jets/routes.rb', line 165

def kingsman_scope(scope)
  constraint = lambda do |request|
    request.env["kingsman.mapping"] = Kingsman.mappings[scope]
    true
  end

  constraints(constraint) do
    yield
  end
end

#kingsman_session(mapping, controllers) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
# File 'lib/kingsman/jets/routes.rb', line 59

def kingsman_session(mapping, controllers) #:nodoc:
  resource :session, only: [], controller: controllers[:sessions], path: "" do
    get   :new,     path: mapping.path_names[:sign_in],  as: "new"
    post  :create,  path: mapping.path_names[:sign_in]
    match :destroy, path: mapping.path_names[:sign_out], as: "destroy", via: mapping.sign_out_via
  end
end

#kingsman_unlock(mapping, controllers) ⇒ Object

:nodoc:



77
78
79
80
81
82
# File 'lib/kingsman/jets/routes.rb', line 77

def kingsman_unlock(mapping, controllers) #:nodoc:
  if mapping.to.unlock_strategy_enabled?(:email)
    resource :unlock, only: [:new, :create, :show],
      path: mapping.path_names[:unlock], controller: controllers[:unlocks]
  end
end

#set_omniauth_path_prefix!(path_prefix) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
160
161
162
# File 'lib/kingsman/jets/routes.rb', line 153

def set_omniauth_path_prefix!(path_prefix) #:nodoc:
  if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
    raise "Wrong OmniAuth configuration. If you are getting this exception, it means that either:\n\n" \
      "1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one\n" \
      "2) You are setting :omniauthable in more than one model\n" \
      "3) You changed your Devise routes/OmniAuth setting and haven't restarted your server"
  else
    ::OmniAuth.config.path_prefix = path_prefix
  end
end

#with_kingsman_exclusive_scope(new_path, new_as, options) ⇒ Object

:nodoc:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/kingsman/jets/routes.rb', line 177

def with_kingsman_exclusive_scope(new_path, new_as, options) #:nodoc:
  current_scope = @scope.dup

  exclusive = { as: new_as, path: new_path, module: nil }
  exclusive.merge!(options.slice(:constraints, :defaults, :options))

  if @scope.respond_to? :new
    @scope = @scope.new exclusive
  else
    exclusive.each_pair { |key, value| @scope[key] = value }
  end

  yield
ensure
  @scope = current_scope
end