-
Notifications
You must be signed in to change notification settings - Fork 12
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
Scoping resources #43
Comments
hey! I don't have any If you have a simple example with the repository and just one call ( class PostRepository
def all_for_user(user)
if user.admin?
self
else
exclude(draft: true)
end
end
end But if you want to use module Post
class BaseResources
include Kan::Abilities
role(:base) { |_user, _| true }
register(:index) { |_user, repo| repo }
end
class AdminResources
include Kan::Abilities
role(:admin) { |user, _| user.admin? }
register(:index) { |_user, repo| repo.exclude(draft: true) }
end
end resources = Kan::Application.new(
post: [Post::BaseResources.new, Post::AdminResources.new]
)
ResourcesImport = Dry::AutoInject(resources) class Service
include ResourcesImport[resource: 'post.index']
include Import['post_repo']
def call(user, params)
resource.call(user, post_repo) #=> relation with condition
# ...
end
end |
Also, I get an idea to introduce module Post
class BaseDataSource
include Kan::DataSource
role(:base) { |_user, _| true }
register(:index) { |_user, repo| repo }
end
class AdminDataSource
include Kan::DataSource
role(:admin) { |user, _| user.admin? }
register(:index) { |_user, repo| repo.exclude(draft: true) }
end
end app = Kan::Application.new(
data_sources: { post: [Post::BaseDataSource.new, Post::AdminDataSource.new] }
)
KanImport = Dry::AutoInject(app) class Service
include KanImport[data_source: 'data_source.post.index']
include Import['post_repo']
def call(user, params)
source = data_source.call(user, post_repo) #=> relation with condition
# ...
end
end In this case, you can get a specific repository or data source based on your role and use it for getting or setting data. WDYT? |
@davydovanton we are looking at using the above approach, although we are unsure of who this would be used with something like |
After giving this some thought, I see 3 separate solutions if you are using the repository pattern:
Additionally wrapping this up in a
|
I really dig this pattern. |
Is it possible to scope resources for viewing? This way, we could scope relations for a given user. Something like:
Is it possible to do this already?
The text was updated successfully, but these errors were encountered: