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

breadcrumb for embedded documents #146

Open
raphox opened this issue Sep 28, 2019 · 1 comment
Open

breadcrumb for embedded documents #146

raphox opened this issue Sep 28, 2019 · 1 comment

Comments

@raphox
Copy link

raphox commented Sep 28, 2019

I create a class to override the default helper.

Use case:

# Models
class Company
  include Mongoid::Document

  embeds_many :catalogs, { class_name: 'Company::Catalog' }
end

class Company::Catalog
  include Mongoid::Document

  embedded_in :company

  breadcrumb do
    mongoid_breadcrumb_links
  end
end

# ActiveAdmin
ActiveAdmin.register Company do
end

ActiveAdmin.register Company::Catalog, as: 'Catalog' do
  belongs_to :company
end
module ActiveAdmin::ViewHelpers::BreadcrumbHelper
  # Returns an array of links to use in a breadcrumb
  def mongoid_breadcrumb_links(path = request.path)
    # remove leading "/" and split up the URL
    # and remove last since it's used as the page title
    parts = path.split('/').select(&:present?)[0..-2]

    resources = []

    parts.each_with_index.map do |part, index|
      # 1. try using `display_name` if we can locate a DB object
      # 2. try using the model name translation
      # 3. default to calling `titlecase` on the URL fragment
      if part =~ /\A(\d+|[a-f0-9]{24}|(?:[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}))\z/ && parts[index - 1]
        parent = active_admin_config.belongs_to_config.try :target
        config = parent && parent.resource_name.route_key == parts[index - 1] ? parent : active_admin_config
        parent_resource = resources.last
##
# main difference from https://github.com/activeadmin/activeadmin/blob/646bf004782e4eae912ff8821f36db691f49fcfb/lib/active_admin/view_helpers/breadcrumb_helper.rb
        resources << if parent_resource
                       collection = parent_resource.send(config.resource_name.collection)
                       collection.find part
                     else
                       config.find_resource part
                     end
##
        name = display_name resources.last
      end

      name ||= I18n.t "activerecord.models.#{part.singularize}",
                      { count: ::ActiveAdmin::Helpers::I18n::PLURAL_MANY_COUNT, default: part.titlecase }

      # Don't create a link if the resource's show action is disabled
      if !config || config.defined_actions.include?(:show)
        link_to name, '/' + parts[0..index].join('/')
      else
        name
      end
    end
  end
end

TIP: Disable company filter in ActiveAdmin.register Company::Catalog because Formtastic doen't work very well with embedded reference.

@grzegorz-jakubiak
Copy link
Collaborator

@raphox thanks a lot, would you be willing to submit a pull request?

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

No branches or pull requests

2 participants