Skip to content

Commit

Permalink
feat: set_all_resources_callback
Browse files Browse the repository at this point in the history
Co-authored-by: PedroAugustoRamalhoDuarte <pedro_aduarte@aluno.unb.br>
  • Loading branch information
Caio-Antonio and PedroAugustoRamalhoDuarte committed Oct 30, 2024
1 parent e0ddb4b commit 6abe69c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
9 changes: 8 additions & 1 deletion app/controllers/rest_api_generator/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/rest_api_generator/controller_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/controllers/cars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion spec/lib/rest_api_generator/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 6abe69c

Please sign in to comment.