From 6abe69c9a9fccbc5394fafe7fad854fe071b387e Mon Sep 17 00:00:00 2001 From: Caio Almeida Date: Wed, 30 Oct 2024 12:24:15 -0300 Subject: [PATCH 1/4] feat: set_all_resources_callback Co-authored-by: PedroAugustoRamalhoDuarte --- .../rest_api_generator/resource_controller.rb | 9 ++++++++- lib/rest_api_generator/controller_callbacks.rb | 7 +++++++ spec/dummy/app/controllers/cars_controller.rb | 6 ++++++ .../resource_controller_spec.rb | 16 +++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/controllers/rest_api_generator/resource_controller.rb b/app/controllers/rest_api_generator/resource_controller.rb index 6c29550..1af2f99 100644 --- a/app/controllers/rest_api_generator/resource_controller.rb +++ b/app/controllers/rest_api_generator/resource_controller.rb @@ -7,8 +7,9 @@ class ResourceController < RestApiGenerator.configuration.parent_controller.cons include Serializable before_action :set_resource, only: [:show, :update, :destroy] + def index - @resources = resource_class.all + set_all_resources @resources = @resources.filter_resource(params_for_filter) if resource_class.include?(Filterable) @resources = @resources.order(ordering_params(params[:sort])) if params[:sort] if pagination @@ -68,6 +69,12 @@ def set_resource end end + def set_all_resources + run_callbacks :set_all_resources do + @resources = resource_class.all + end + end + # UsersController => User def resource_by_controller_name(controller_name = self.class.to_s) controller_name.split(Regexp.union(["Controller", "::"]))[-1].singularize.constantize diff --git a/lib/rest_api_generator/controller_callbacks.rb b/lib/rest_api_generator/controller_callbacks.rb index f4f3383..1e4f544 100644 --- a/lib/rest_api_generator/controller_callbacks.rb +++ b/lib/rest_api_generator/controller_callbacks.rb @@ -8,6 +8,7 @@ module ControllerCallbacks included do define_callbacks :set_resource define_callbacks :set_parent_resource + define_callbacks :set_all_resources end module ClassMethods @@ -24,6 +25,12 @@ module ClassMethods set_callback(:set_parent_resource, callback, name, options) end end + + define_method :"#{callback}_set_all_resources" do |*names, &blk| + _insert_callbacks(names, blk) do |name, options| + set_callback(:set_all_resources, callback, name, options) + end + end end end end diff --git a/spec/dummy/app/controllers/cars_controller.rb b/spec/dummy/app/controllers/cars_controller.rb index d1ac5e5..a10371a 100644 --- a/spec/dummy/app/controllers/cars_controller.rb +++ b/spec/dummy/app/controllers/cars_controller.rb @@ -2,6 +2,12 @@ class CarsController < RestApiGenerator::ResourceController after_set_resource :authorize_logic + after_set_all_resources :authorized_scope + + def authorized_scope + # Custom authorization logic + # @resource = authorized_scope @resource + end def authorize_logic # Custom authorization logic diff --git a/spec/lib/rest_api_generator/resource_controller_spec.rb b/spec/lib/rest_api_generator/resource_controller_spec.rb index 29b2a0a..9c39ad7 100644 --- a/spec/lib/rest_api_generator/resource_controller_spec.rb +++ b/spec/lib/rest_api_generator/resource_controller_spec.rb @@ -121,7 +121,7 @@ end describe "callbacks" do - context "when callbacks exists" do + context "when set resource callbacks exists" do it "calls callbacks" do car = Car.create!(name: "Car") controller = CarsController.new @@ -135,5 +135,19 @@ controller.send(:set_resource) end end + + context "when set all resources callbacks exists" do + it "calls callbacks" do + Car.create!(name: "Car") + controller = CarsController.new + allow(controller).to receive(:params).and_return(ActionController::Parameters.new({})) + + # rubocop:disable RSpec/MessageSpies + expect(controller).to receive(:authorized_scope) + # rubocop:enable RSpec/MessageSpies + + controller.send(:set_all_resources) + end + end end end From 06af5b5f63aef3cf109578c38bcf6f029ba1fa7b Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Wed, 30 Oct 2024 14:16:40 -0300 Subject: [PATCH 2/4] set_all_resource callback for nested_routes controller --- .../rest_api_generator/child_resource_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/rest_api_generator/child_resource_controller.rb b/app/controllers/rest_api_generator/child_resource_controller.rb index 6b7462a..1e66e42 100644 --- a/app/controllers/rest_api_generator/child_resource_controller.rb +++ b/app/controllers/rest_api_generator/child_resource_controller.rb @@ -10,7 +10,7 @@ class ChildResourceController < RestApiGenerator.configuration.parent_controller before_action :set_resource, only: [:show, :update, :destroy] def index - @resources = resources + set_all_resources @resources = @resources.filter_resource(params_for_filter) if resource_class.include?(Filterable) @resources = @resources.order(ordering_params(params[:sort])) if params[:sort] if pagination @@ -40,6 +40,12 @@ def destroy private + def set_all_resources + run_callbacks :set_all_resources do + @resources = resources + end + end + def resources @parent_resource.send(resource_class.to_s.downcase.pluralize) end From b821e8889b55ac74d1779e52a4012cfc475846a7 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Wed, 30 Oct 2024 14:16:51 -0300 Subject: [PATCH 3/4] improves docs for callback --- docs/features/callbacks.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/features/callbacks.md b/docs/features/callbacks.md index 2554662..6f3d53f 100644 --- a/docs/features/callbacks.md +++ b/docs/features/callbacks.md @@ -1,13 +1,18 @@ # Callbacks You can use the callbacks in the controller to add some logic before, around or after `set_resource` -or `set_parent_resource`: +, `set_parent_resource` or `set_all_resources`: + +- The `set_resource` method is called in [:show, :update, :destroy] actions +- The `set_parent_resource` method is called in all actions if you are using nested resources +- The `set_all_resources` method is called in [:index] actions ```ruby # frozen_string_literal: true class CarsController < RestApiGenerator::ResourceController after_set_resource :authorize_logic + set_all_resources -> { @resources = authorized_resource(@resources) } def authorize_logic # Custom authorization logic From 8ae256671f4e7537437f21518887ec5a8ec16f53 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Wed, 30 Oct 2024 14:25:41 -0300 Subject: [PATCH 4/4] v0.4.0 --- lib/rest_api_generator/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rest_api_generator/version.rb b/lib/rest_api_generator/version.rb index 8ad16ae..f7aa6b0 100644 --- a/lib/rest_api_generator/version.rb +++ b/lib/rest_api_generator/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RestApiGenerator - VERSION = "0.3.0" + VERSION = "0.4.0" end