Class: Adeia::ControllerResource
- Inherits:
-
Object
- Object
- Adeia::ControllerResource
- Defined in:
- lib/adeia/controller_resource.rb
Class Method Summary collapse
- .add_before_filter(controller_class, method, **args) ⇒ Object
- .load_resource_or_records_and_authorize(controller) ⇒ Object
- .require_login(controller) ⇒ Object
Instance Method Summary collapse
- #authorization ⇒ Object
- #authorize! ⇒ Object
- #authorized?(method, element, resource) ⇒ Boolean
- #check_permissions! ⇒ Object
-
#initialize(controller, **args) ⇒ ControllerResource
constructor
A new instance of ControllerResource.
- #load_records ⇒ Object
- #load_resource ⇒ Object
Constructor Details
#initialize(controller, **args) ⇒ ControllerResource
Returns a new instance of ControllerResource.
29 30 31 32 33 34 35 36 37 |
# File 'lib/adeia/controller_resource.rb', line 29 def initialize(controller, **args) @controller = controller @action_name = args.fetch(:action, @controller.action_name) @controller_name = args.fetch(:controller, @controller.controller_path) @token = args.fetch(:token, @controller.request.GET[:token]) @resource = args[:resource] @user = @controller.current_user @controller.store_location end |
Class Method Details
.add_before_filter(controller_class, method, **args) ⇒ Object
8 9 10 11 12 |
# File 'lib/adeia/controller_resource.rb', line 8 def self.add_before_filter(controller_class, method, **args) controller_class.send(:before_action, args.slice(:only, :except, :if, :unless)) do |controller| ControllerResource.send(method, controller) end end |
.load_resource_or_records_and_authorize(controller) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/adeia/controller_resource.rb', line 14 def self.(controller) case controller.action_name when "index" controller. when "show", "edit", "update", "destroy" controller. else controller. end end |
.require_login(controller) ⇒ Object
25 26 27 |
# File 'lib/adeia/controller_resource.rb', line 25 def self.require_login(controller) controller.require_login! end |
Instance Method Details
#authorization ⇒ Object
63 64 65 |
# File 'lib/adeia/controller_resource.rb', line 63 def @authorization ||= Authorization.new(@controller_name, @action_name, @token, @resource, @user) end |
#authorize! ⇒ Object
67 68 69 |
# File 'lib/adeia/controller_resource.rb', line 67 def . end |
#authorized?(method, element, resource) ⇒ Boolean
75 76 77 78 |
# File 'lib/adeia/controller_resource.rb', line 75 def (method, element, resource) @controller_name, @resource = get_controller_and_resource(element, resource) instance_variable_get_or_set(method) end |
#check_permissions! ⇒ Object
71 72 73 |
# File 'lib/adeia/controller_resource.rb', line 71 def . end |
#load_records ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/adeia/controller_resource.rb', line 48 def load_records rights = .read_rights.merge(.token_rights(:read)) { |key, v1, v2| v1 + v2 } rights, resource_ids = rights[:rights], rights[:resource_ids] @records ||= if rights.any? { |r| r. == "all_entries" } resource_class.all elsif rights.any? { |r| r. == "on_ownerships" } resource_class.where("user_id = ? OR id IN (?)", @user.id, resource_ids) elsif rights.any? { |r| r. == "on_entry" } resource_class.where(id: resource_ids) else resource_class.none end @controller.instance_variable_set("@#{resource_name.pluralize}", @records) end |
#load_resource ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/adeia/controller_resource.rb', line 39 def load_resource begin @resource ||= resource_class.find(@controller.params.fetch(:id)) @controller.instance_variable_set("@#{resource_name}", @resource) rescue KeyError raise MissingParams.new(:id) end end |