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
Index page title can be set using a proc #1861
Conversation
Hi @jamesalmond, owner of #1324 here. Glad to see you added this. It was originally included it in my PR, but I couldn't imagine a use-case for procs at the time. |
@@ -10,3 +10,12 @@ Feature: Index - Page Title | |||
end | |||
""" | |||
Then I should see the page title "Awesome Title" | |||
|
|||
Scenario: Set a string as the title |
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.
Shouldn't this say something like "Use a proc for the title"?
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.
Yep, my bad! Pushed a corrected title.
Hi @daxter. Yes, I saw you'd added it (and then removed it!) :) Coming across a few limitations to what I want to achieve with AA so glad to have the time to try and resolve some of them. I think it makes sense to have it in the case for nested resources, just not sure about the the context of the instance_eval. Any thoughts? |
AFAICT, |
@daxter I'm not really sure what you mean by "capture the context with a faux resource". As far as I could tell there was no resource to access in this context. |
Isn't there? In the example that you gave, what variable are you accessing? On Jan 16, 2013, at 10:26 AM, James Almond notifications@github.com wrote:
|
I'm accessing the parent in the belongs_to relationship. I believe inherited_resources sets it when you use belongs_to. I ended up instance_eval-ing against the controller as the way you'd written it in #1324 didn't appear to work any more. |
You're right, the version in my PR didn't quite work (because I wasn't sure in what way it would be used). Are you sure you want to instance_eval the controller? Wouldn't this be more appropriate? resource_class.instance_eval(&config[:title]) That way you have direct access to the resource: index :title => proc{"#{rand} #{self}"} |
For my use case, I don't think accessing the resource class would work. That would be using instance_eval against the Article class and wouldn't give me access to the brand that's instantiated by inherited_resources which is what I need to include in my title. So index :title => proc{"#{rand} #{self}"} would be the equivalent of doing index :title => proc{"#{Article.brand} #{Article}"} which wouldn't give you to access to any of the request's context. Something about using instance_eval against the controller bugs me but I'm not sure what else would be suitable whilst being able to achieve the custom titles I need. Unless I'm not understanding what resource_class actually is at this point? |
No you understood correctly (except I was calling BTW, with the version you're currently using, it would be more DRY to use In that case, you have the best implementation. Though you should add tests to confirm that the proc has access to the controller and |
I've added an example that uses resource_class in the proc title. You're right about the DRY-ness, I was just using the class explicitly in my example to show that using instance eval against it wouldn't give you access to the controller. |
✅ looks good to me! |
I would be glad to see this included in AA |
I would also be very happy to see this in ActiveAdmin. I just upgraded from 0.4.4 because I thought this was already available in 0.5.1 (the examples given here might warn unsuspecting users that procs and methods don't apply everywhere!). |
@ekortright sorry about that! Besides procs on index pages, are there any other places you've noticed this problem? |
No problem! That was the only place I noticed it. |
Looks good to me, going to merge in. |
Index page title can be set using a proc
I've implemented functionality that allows you to set dynamic titles on index actions, which should resolve #1324 and #1059 (closed due to inactivity?). The proc is instance eval-ed against the controller as there isn't a resource to apply it to in the index action.
I implemented it because needed to modify titles for belongs_to resources to reference their parent. For example, if an Article belongs to a Brand the article index action would be: