Module: Adeia::ControllerMethods
- Defined in:
- lib/adeia/controller_methods.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #authorize!(**args) ⇒ Object
- #authorize_and_load_records!(**args) ⇒ Object
- #can?(action, element, resource = nil) ⇒ Boolean
- #load_and_authorize!(**args) ⇒ Object
- #load_records(**args) ⇒ Object
-
#redirect_back_or(default, **args) ⇒ Object
Redirect the user to the stored url or the default one provided.
- #require_login! ⇒ Object
- #rights?(action, element, resource = nil) ⇒ Boolean
-
#store_location ⇒ Object
Store the current url in a cookie.
Class Method Details
.included(base) ⇒ Object
20 21 22 23 |
# File 'lib/adeia/controller_methods.rb', line 20 def self.included(base) base.extend ClassMethods base.helper_method :can?, :rights? end |
Instance Method Details
#authorize!(**args) ⇒ Object
43 44 45 |
# File 'lib/adeia/controller_methods.rb', line 43 def (**args) ControllerResource.new(self, **args). end |
#authorize_and_load_records!(**args) ⇒ Object
32 33 34 35 36 |
# File 'lib/adeia/controller_methods.rb', line 32 def (**args) controller_resource = ControllerResource.new(self, **args) controller_resource. return controller_resource.load_records end |
#can?(action, element, resource = nil) ⇒ Boolean
54 55 56 57 |
# File 'lib/adeia/controller_methods.rb', line 54 def can?(action, element, resource=nil) controller, resource = ControllerResource.get_controller_and_resource(element, resource) ControllerResource.new(self, action: action, controller: controller, resource: resource).(:can?) end |
#load_and_authorize!(**args) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/adeia/controller_methods.rb', line 25 def (**args) controller_resource = ControllerResource.new(self, **args) resource = controller_resource.load_resource controller_resource. return resource end |
#load_records(**args) ⇒ Object
38 39 40 41 |
# File 'lib/adeia/controller_methods.rb', line 38 def load_records(**args) controller_resource = ControllerResource.new(self, **args) return controller_resource.load_records end |
#redirect_back_or(default, **args) ⇒ Object
Redirect the user to the stored url or the default one provided
-
Args :
-
default path to redirect to
-
-
Returns :
71 72 73 74 |
# File 'lib/adeia/controller_methods.rb', line 71 def redirect_back_or(default, **args) redirect_to([:return_to] || default, **args) .delete(:return_to) end |
#require_login! ⇒ Object
47 48 49 50 51 52 |
# File 'lib/adeia/controller_methods.rb', line 47 def require_login! unless signed_in? store_location raise LoginRequired end end |
#rights?(action, element, resource = nil) ⇒ Boolean
59 60 61 62 |
# File 'lib/adeia/controller_methods.rb', line 59 def rights?(action, element, resource=nil) controller, resource = ControllerResource.get_controller_and_resource(element, resource) ControllerResource.new(self, action: action, controller: controller, resource: resource).(:rights?) end |
#store_location ⇒ Object
Store the current url in a cookie
-
Args :
-
Returns :
82 83 84 85 86 |
# File 'lib/adeia/controller_methods.rb', line 82 def store_location if request.get? && Adeia.api_patterns.all? { |pattern| /^#{pattern}$/.match(request.path).nil? } [:return_to] = { value: request.fullpath, expires: 2.minutes.from_now } end end |