Class: ApplicationController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- ApplicationController
- Defined in:
- lib/ecrire/app/controllers/application_controller.rb
Overview
Base controller for every controller in Ecrire (Theme & Admin) Ecrire::ThemeController inherits from this class so there are no reason why you should inherit from this class.
The controller handles user authentication, and CSRF protection.
It also provides a url
method to build complex URL using a very light syntax.
Direct Known Subclasses
Admin::ApplicationController, Ecrire::ThemeController, OnboardingController, SessionsController
Instance Method Summary collapse
-
#current_user ⇒ Object
Return current signed
user
or nil, if the user is not signed in. -
#signed_in? ⇒ Boolean
Returns
true
if the user is signed in. -
#url(path, options = {}) ⇒ Object
Returns a URL based on the path and options provided.
Instance Method Details
#current_user ⇒ Object
Return current signed user
or nil, if the user is not signed in
20 21 22 |
# File 'lib/ecrire/app/controllers/application_controller.rb', line 20 def current_user warden.user end |
#signed_in? ⇒ Boolean
Returns true
if the user is signed in
26 27 28 |
# File 'lib/ecrire/app/controllers/application_controller.rb', line 26 def signed_in? !warden.user.nil? end |
#url(path, options = {}) ⇒ Object
Returns a URL based on the path and options provided. It does not try to validate the URL, it only generates it and assume it’s a valid URL.
path
: The relative path of the URL you want to build.
options
: Hash containing options for rendering the url.
The path
and options
are linked together via a mapping construct that you can use to map records to part of the URL.
To map records to the path
, you need to specify the record & the method you want to use inside the path.
url('/admin/posts/:posts.id', post: @post)
-> '/admin/posts/32'
The method looks for every occurence of “:[key].” and will look in options
for that key and call the given method on that key.
This means the object can be anything as long as it handles the method.
Here are a other examples:
url('/users/:user.id/tags/:tag.id/', user: @user, tag: @tag)
-> '/users/12/tags/12'
url('/users')
-> '/users'
The options
also looks for the absolute_path
key. If it’s set to true
, the method will return an absolute path.
url('/users/:user.id/tags/:tag.id/', user: @user, tag: @tag, absolute_path: true)
-> 'http://localhost:3000/users/12/tags/12'
url('/users', absolute_path: true)
-> 'http://localhost:3000/users'
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ecrire/app/controllers/application_controller.rb', line 71 def url(path, = {}) records = .with_indifferent_access regex = /(:([a-z]+)\.([a-z]+))/i path = path.gsub regex do |match| records[$2].send($3) end if .delete(:absolute_path) [:path] = path [:host] ||= request.host [:protocol] ||= request.protocol [:port] ||= request.port ActionDispatch::Http::URL.full_url_for() else path end end |