Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using callable objects as scopes #261 #263

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/scoping.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ class PostPolicy < ApplicationPolicy
end
```

Or just pass class name where included logic of scope to `relation_scope` and write `.call` method:

```ruby
class PostsController < ApplicationController
def index
@posts = authorized_scope(Post.all)
end
end

class PostPolicy < ApplicationPolicy
relation_scope AuthorizedPosts
end

class AuthorizedPosts
class << self
def call(policy, relation)
user = policy.user
user.admin? ? relation : relation.where(user: user)
end
end
end
```

## Define scopes

To define scope you should use either `scope_for` or `smth_scope` methods in your policy:
Expand Down
13 changes: 12 additions & 1 deletion lib/action_policy/policy/scoping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ def lookup_type_from_target(target)

module ClassMethods # :nodoc:
# Register a new scoping method for the `type`
def scope_for(type, name = :default, &block)
def scope_for(type, name = :default, callable = nil, &block)
name, callable = prepare_args(name, callable)

mid = :"__scoping__#{type}__#{name}"
block = ->(target) { callable.call(self, target) } if callable

define_method(mid, &block)

Expand Down Expand Up @@ -154,6 +157,14 @@ def scope_matchers
[]
end
end

private

def prepare_args(name, callable)
return [name, callable] if name && callable
return [name, nil] if name.is_a?(Symbol) || name.is_a?(String)
[:default, name]
end
end
end
end
Expand Down
90 changes: 90 additions & 0 deletions test/action_policy/rails/scope_matchers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,96 @@ def test_named_filtered_params_admin
end
end

class TestRailsScopeMatchersViaClass < ActionController::TestCase
class AuthorizedPosts
class << self
def call(policy, relation)
user = policy.user
scope = user.admin? ? relation : relation.where(user: user)
scope.order(:title)
end
end
end

class PostPolicy < ActionPolicy::Base
relation_scope AuthorizedPosts
end

class PostsController < ActionController::Base
authorize :user, through: :current_user

def index
render json: authorized_scope(AR::Post.all)
end

private

def current_user
@current_user ||= AR::User.find(params[:user_id])
end
end

tests PostsController

attr_reader :admin, :guest

def setup
ActiveRecord::Base.connection.begin_transaction(joinable: false)
@guest = AR::User.create!(name: "Jack")
@admin = AR::User.create!(name: "John", role: "admin")
end

def teardown
ActiveRecord::Base.connection.rollback_transaction
end

def json_body
@json_body ||= JSON.parse(response.body)
end

def test_authorized_relation_guest
get :index, params: {user_id: guest.id}

assert_equal 0, json_body.size
end

def test_authorized_relation_admin
get :index, params: {user_id: admin.id}

assert_equal 0, json_body.size
end

def test_authorized_association
AR::Post.create!(title: "[wip]", user: admin)
AR::Post.create!(title: "Good news!", user: guest)

get :index, params: {user_id: guest.id}

assert_equal 1, json_body.size
assert_equal "Good news!", json_body.first["title"]
end

def test_authorized_association_2
AR::Post.create!(title: "[wip]", user: admin)
AR::Post.create!(title: "Good news!", user: guest)

get :index, params: {user_id: admin.id}

assert_equal 2, json_body.size
assert_equal ["Good news!", "[wip]"], json_body.map { _1["title"] }
end

def test_authorized_association_3
AR::Post.create!(title: "[wip]", user: guest)
AR::Post.create!(title: "Admin news", user: admin)
AR::Post.create!(title: "Good news!", user: guest)

get :index, params: {user_id: guest.id}

assert_equal 2, json_body.size
end
end

# See https://github.com/palkan/action_policy/issues/101
class TestRelationMutability < ActionController::TestCase
class UserPolicy < ActionPolicy::Base
Expand Down
Loading