Skip to content

Commit

Permalink
Merge pull request #43 from SwitchDreams/feat/set_all_resources_callback
Browse files Browse the repository at this point in the history
feat: set_all_resources_callback
  • Loading branch information
PedroAugustoRamalhoDuarte authored Oct 30, 2024
2 parents e0ddb4b + 8ae2566 commit 48012ac
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
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: 6 additions & 1 deletion docs/features/callbacks.md
Original file line number Diff line number Diff line change
@@ -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
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
2 changes: 1 addition & 1 deletion lib/rest_api_generator/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RestApiGenerator
VERSION = "0.3.0"
VERSION = "0.4.0"
end
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 48012ac

Please sign in to comment.