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

ActiveAdmin::Resource loads a resource for breadcrumbs #2315

Merged
merged 1 commit into from
Aug 5, 2013
Merged

ActiveAdmin::Resource loads a resource for breadcrumbs #2315

merged 1 commit into from
Aug 5, 2013

Conversation

amiel
Copy link
Contributor

@amiel amiel commented Jul 2, 2013

Here is an proof-of-concept attempt at #2266.

A couple of notes/thoughts:

  • I think this is better than having the loading logic in the breadcrumbs helper, but it still doesn't seem idea. All of the resource loading logic is already in the controller (through inherited resources, so not easy to refactor out).

  • Another possible direction would be to use ActiveAdmin::Resource#controller to load the resource. I started trying this, but it gets ugly pretty quick. Here's what it would look like:

      def resource_from_param(id)
        c = controller.new
        c.params = { id: id }
        c.send(:instance_variable_set, "@_session", session)
        # I didn't actually get this far, so I'm not sure what other hacks we would need...
        c.send(:resource)
      end

    So, yeah, not great, but at least the resource loading logic isn't duplicated.

@daxter, any thoughts?

@amiel
Copy link
Contributor Author

amiel commented Jul 2, 2013

Oh, by the way, I'm just spiking here. I can write tests once a direction is decided...

@seanlinsley
Copy link
Contributor

This is the direction I was imagining, except you still need to add the decorator wrapping :)

Instead of resource_from_param, couldn't you name it find_resource? Or is that method name already taken?

@amiel
Copy link
Contributor Author

amiel commented Jul 4, 2013

The decorator wrapping is already in there, I just need to fix all the tests and add a test for the decorated breadcrumb case.

find_resource sounds pretty good to me.

@seanlinsley
Copy link
Contributor

Ah, my apologies.

Cool. Any idea when you'll have time to update this?

@amiel
Copy link
Contributor Author

amiel commented Jul 4, 2013

I definitely won't have time before next week. I'll try to get it in on the early side of next week.

Thanks for your input :)

@seanlinsley
Copy link
Contributor

Not at all; thank you for going to the trouble to do this!

@amiel
Copy link
Contributor Author

amiel commented Jul 11, 2013

@daxter I just rebased this branch onto master.

I'm still getting a lot of test failures on master. Should master be all green right now and I have an environment problem? Or are there known failures on master?

BTW, I made sure to rm -rf spec/rails to make sure my spec/rails was regenerated.

@seanlinsley
Copy link
Contributor

No, master is passing just fine. I'll take a look into what's.causing the failures.

@amiel
Copy link
Contributor Author

amiel commented Jul 11, 2013

I've been looking into the failures. It looks like the database isn't set up (the failures are mostly Could not find table 'admin_users' or Could not find table 'posts'.

Here's what I've tried so far (to no avail):

cd spec/rails/rails-3.2.13
rake db:migrate
rake db:test:prepare
TEST_ENV_NUMBER=2 rake db:test:prepare
cd ../../..

rake # still fails

@amiel
Copy link
Contributor Author

amiel commented Jul 11, 2013

Anyway, it's helpful to know that it's working for you.

Given my last post, do you have any ideas?

@seanlinsley
Copy link
Contributor

Are you not able to run even master? This should work just fine:

rm -rf spec/rails
rspec spec/unit

@amiel
Copy link
Contributor Author

amiel commented Jul 15, 2013

running

rm -rf spec/rails
rspec spec/unit

passes.

rm -rf spec/rails
rake

fails on spec/unit/views/tabbed_navigation_spec.rb

I'll start writing some tests using rspec instead of rake thanks.

@seanlinsley
Copy link
Contributor

What about rake test? That should also work.

@amiel
Copy link
Contributor Author

amiel commented Jul 15, 2013

Well that's embarrassing, rake test is passing for me on master.

It looks like plain old rake default is failing because it runs rake parallel_tests instead of rake test (task :default => :parallel_tests).

Sorry to have bothered you with this, and thanks for helping troubleshoot.

@seanlinsley
Copy link
Contributor

No problem. IMO that's probably something that should be changed. Or more, the parallel_tests rake task shouldn't try to spin up X number of runners that each try to build the very same Rails app to use for testing.

BTW there are a couple other useful things in the CONTRIBUTING file

@amiel
Copy link
Contributor Author

amiel commented Jul 15, 2013

Yeah, thanks. I actually did read CONTRIBUTING, but I sort of assumed that the default rake was rake test. I should have checked.

@@ -136,7 +139,7 @@ def link_to(name, url); {:name => name, :path => url}; end
trail[1][:path].should == "/admin/posts"
end
it "should have a link to /admin/posts/1" do
trail[2][:name].should == "1"
trail[2][:name].should == "Hello World"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slight change in functionality, but I think for the better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I lied, it's just a change in the stub used by the whole test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What part of your changes caused this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amiel
Copy link
Contributor Author

amiel commented Jul 16, 2013

@daxter Ok, I think I'm all done here. Do you want me to squash this in to one commit? I've been asked to do that before in ActiveAdmin pull requests.

@amiel
Copy link
Contributor Author

amiel commented Jul 16, 2013

Hmmm, there are travis failures. I'm out of open-source time today. I can take a look in a couple of days if you haven't figured it out.

For reference, same failure is happening in #2350, and it is unrelated to this pull-request.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0%) when pulling c88fa7c671e4cd086f303789bfa3c5ced93ec60c on carnesmedia:config_loads_resource into ec99964 on gregbell:master.

@seanlinsley
Copy link
Contributor

@amiel can you rebase this on master? Tests are passing as of #2385

@amiel
Copy link
Contributor Author

amiel commented Aug 5, 2013

Ok, rebased, and tests pass for me locally. Hopefully Travis is happy now...

@amiel
Copy link
Contributor Author

amiel commented Aug 5, 2013

@daxter this should be good to go

@@ -13,7 +13,7 @@ def breadcrumb_links(path = request.path)
# 3. default to calling `titlecase` on the URL fragment
if part =~ /\A(\d+|[a-f0-9]{24})\z/ && parts[index-1]
config = active_admin_config.belongs_to_config.try(:target) || active_admin_config
obj = config.resource_class.where( config.resource_class.primary_key => part ).first
obj = config.find_resource(part)
name = display_name obj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you collapse the last two lines?

name = display_name config.find_resource(part)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will do

@amiel
Copy link
Contributor Author

amiel commented Aug 5, 2013

Would you like me to squash this in to one commit?

@seanlinsley
Copy link
Contributor

Yes, please. Feel free to add as much as you want to the commit message when you squash them.

Closes #2266

Add `ActiveAdmin::Resource#find_resource`. This is now a centralized place to
load a resource, with correct handling of primary key, decoration, etc.

Then, use this new way of loading resources in the Breadcrumbs logic.
This was the original impetus for for defining
`ActiveAdmin::Resource#find_resource`. Previously, the `BreadcrumbsHelper`
did a one-off load of the resource, which has historically been a source of
bugs and/or inconsistencies.

Please note that this is duplication with InheritedResources. Normally, the
resource is loaded by InheritedResources, but there are situations in which
ActiveAdmin needs to load resources as well (ie Breadcrumbs). It was not
reasonable to use InheritedResources to load the resource (see the discussion
at #2315).
@coveralls
Copy link

Coverage Status

Coverage increased (+0%) when pulling 8552e2f on carnesmedia:config_loads_resource into 33429ed on gregbell:master.

seanlinsley added a commit that referenced this pull request Aug 5, 2013
ActiveAdmin::Resource loads a resource for breadcrumbs
@seanlinsley seanlinsley merged commit 02ff162 into activeadmin:master Aug 5, 2013
@seanlinsley
Copy link
Contributor

Thanks for the contribution! 🐙

@amiel amiel deleted the config_loads_resource branch August 5, 2013 22:24
Aerlinger pushed a commit to Aerlinger/active_admin that referenced this pull request Jun 3, 2014
Closes activeadmin#2266

Add `ActiveAdmin::Resource#find_resource`. This is now a centralized place to
load a resource, with correct handling of primary key, decoration, etc.

Then, use this new way of loading resources in the Breadcrumbs logic.
This was the original impetus for for defining
`ActiveAdmin::Resource#find_resource`. Previously, the `BreadcrumbsHelper`
did a one-off load of the resource, which has historically been a source of
bugs and/or inconsistencies.

Please note that this is duplication with InheritedResources. Normally, the
resource is loaded by InheritedResources, but there are situations in which
ActiveAdmin needs to load resources as well (ie Breadcrumbs). It was not
reasonable to use InheritedResources to load the resource (see the discussion
at activeadmin#2315).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants