Skip to content

Support nested belongs_to #376

@davidmathei

Description

@davidmathei

Using belongs_to during registration of a resource naturally uses the "parent" as a scope for the "childs" overview. The childs controller expects a "parent_id" for the index page and the routes ar
e crafted accordingly.

As it stands, though, the "parent_id" is necessary for the overview but is not processed in all url's where it would be necessary.

Imagine a nesting of Continent, Country and City resources:

ActiveAdmin.register Continent do
  #...
end

ActiveAdmin.register Country do
  belongs_to :continent
end

ActiveAdmin.register City do
  belongs_to :country
end

Now the breadcrumb path shown while looking at a city or a city overview does not mention Continent (because it's parsed from the URL which does not contain that datapoint)
and the part linking to the country misses the continent_id. Following the link fails. I found a workaround at least for the breadcrumbs. Works for me, but is possibly not generalizable.

Adding a comment under a country or city fails in both cases: they only infer admin_city_path and admin_country_path respectively as targets, not admin_country_city_path etc. Would not work anyway because they are not given the extra parameter on invocation.

Finally the json/csv/xml exports ignore the parent relationship, I managed to work around that by monkeypatching but I'm not sure if that covers all families of URL's to be expected.

For the time being handling the comments is more problematic as the routes are defined during namespace registration.

Could it be that I'm on the wrong path using belongs_to? I can imaging that scope_to would work also, at the expense of adding the parent_id in al URL's which would fail also in the comment case.
Anyone else ran into this problem? Might also be the case that I overlooked something completely obvious ...

Cheers and thanks a lot so far!

download link fix:

class PaginatedCollection < ActiveAdmin::Component
   protected
   def build_download_format_links(formats = [:csv, :xml, :json])
      links = formats.collect do |format|
        #old: link_to format.to_s.upcase, { :format => format}.merge(request.query_parameters.except(:commit, :format))
        tmp_p = request.fullpath.split('?')
        new_path = tmp_p[0] + ".#{format}?" + tmp_p[1..-1].join('?') #the ugly fix
        link_to(format.to_s.upcase, new_path)
      end
      text_node [I18n.t('active_admin.download'), links].flatten.join("&nbsp;").html_safe
   end
end

The breadcrumb fix tries to establish the first notion of an instance mentioned in the path and works itself up along the belongs_to path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions