-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add context to Page #2457
base: main
Are you sure you want to change the base?
Add context to Page #2457
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -5,11 +5,13 @@ class ApplicationController < ActionController::Base | |||||||
def index | ||||||||
authorize_resource(resource_class) | ||||||||
search_term = params[:search].to_s.strip | ||||||||
resources = filter_resources(scoped_resource, search_term: search_term) | ||||||||
authorized_scope = authorize_scope(scoped_resource) | ||||||||
resources = filter_resources(authorized_scope, search_term: search_term) | ||||||||
resources = apply_collection_includes(resources) | ||||||||
resources = order.apply(resources) | ||||||||
resources = paginate_resources(resources) | ||||||||
page = Administrate::Page::Collection.new(dashboard, order: order) | ||||||||
page.context = self | ||||||||
|
||||||||
render locals: { | ||||||||
resources: resources, | ||||||||
|
@@ -20,28 +22,39 @@ def index | |||||||
end | ||||||||
|
||||||||
def show | ||||||||
page = Administrate::Page::Show.new(dashboard, requested_resource) | ||||||||
page.context = self | ||||||||
render locals: { | ||||||||
page: Administrate::Page::Show.new(dashboard, requested_resource) | ||||||||
page: page | ||||||||
} | ||||||||
end | ||||||||
|
||||||||
def new | ||||||||
resource = new_resource | ||||||||
authorize_resource(resource) | ||||||||
resource = new_resource.tap do |resource| | ||||||||
authorize_resource(resource) | ||||||||
contextualize_resource(resource) | ||||||||
end | ||||||||
|
||||||||
page = Administrate::Page::Form.new(dashboard, resource) | ||||||||
page.context = self | ||||||||
render locals: { | ||||||||
page: Administrate::Page::Form.new(dashboard, resource) | ||||||||
page: page | ||||||||
} | ||||||||
end | ||||||||
|
||||||||
def edit | ||||||||
page = Administrate::Page::Form.new(dashboard, requested_resource) | ||||||||
page.context = self | ||||||||
render locals: { | ||||||||
page: Administrate::Page::Form.new(dashboard, requested_resource) | ||||||||
page: page | ||||||||
} | ||||||||
end | ||||||||
|
||||||||
def create | ||||||||
resource = new_resource(resource_params) | ||||||||
authorize_resource(resource) | ||||||||
resource = new_resource(resource_params).tap do |resource| | ||||||||
authorize_resource(resource) | ||||||||
contextualize_resource(resource) | ||||||||
end | ||||||||
|
||||||||
if resource.save | ||||||||
yield(resource) if block_given? | ||||||||
|
@@ -50,8 +63,10 @@ def create | |||||||
notice: translate_with_resource("create.success") | ||||||||
) | ||||||||
else | ||||||||
page = Administrate::Page::Form.new(dashboard, resource) | ||||||||
page.context = self | ||||||||
render :new, locals: { | ||||||||
page: Administrate::Page::Form.new(dashboard, resource) | ||||||||
page: page | ||||||||
}, status: :unprocessable_entity | ||||||||
end | ||||||||
end | ||||||||
|
@@ -64,8 +79,10 @@ def update | |||||||
status: :see_other | ||||||||
) | ||||||||
else | ||||||||
page = Administrate::Page::Form.new(dashboard, requested_resource) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this one missing the context assignment?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, you're right. Sorry. |
||||||||
page.context = self | ||||||||
render :edit, locals: { | ||||||||
page: Administrate::Page::Form.new(dashboard, requested_resource) | ||||||||
page: page | ||||||||
}, status: :unprocessable_entity | ||||||||
end | ||||||||
end | ||||||||
|
@@ -178,17 +195,24 @@ def sorting_params | |||||||
end | ||||||||
|
||||||||
def dashboard | ||||||||
@dashboard ||= dashboard_class.new | ||||||||
@dashboard ||= dashboard_class.new.tap do |d| | ||||||||
d.context = self | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
def requested_resource | ||||||||
@requested_resource ||= find_resource(params[:id]).tap do |resource| | ||||||||
authorize_resource(resource) | ||||||||
contextualize_resource(resource) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
def find_resource(param) | ||||||||
scoped_resource.find(param) | ||||||||
authorize_scope(scoped_resource).find(param) | ||||||||
end | ||||||||
|
||||||||
def authorize_scope(scope) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this introduction of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right, this change is off-topic. I appreciate your accurate understanding of my intention. |
||||||||
scope | ||||||||
end | ||||||||
|
||||||||
def scoped_resource | ||||||||
|
@@ -285,6 +309,13 @@ def authorize_resource(resource) | |||||||
end | ||||||||
end | ||||||||
|
||||||||
# Override this if you want to contextualize the resource differently. | ||||||||
# | ||||||||
# @param resource A resource to be contextualized. | ||||||||
# @return nothing | ||||||||
def contextualize_resource(resource) | ||||||||
end | ||||||||
|
||||||||
def paginate_resources(resources) | ||||||||
resources.page(params[:_page]).per(records_per_page) | ||||||||
end | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ def required? | |
end | ||
|
||
attr_reader :attribute, :data, :options, :page, :resource | ||
attr_accessor :context | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following on my comment about
contextualize_resource
, I wonder if this is what it should be doing instead? Acting on the page instead of the resource here:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the Controller object is almost essential as a Context for the Page, so how about passing it to the initializer? Like the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially that makes sense, but then the developers wouldn't be able to alter the context easily with a hook like
contextualize_page
. Having said that, perhaps there isn't a use case for that...? I have no idea yet. I'm experimenting now to see the possibilities of your PR.