Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 17, 2015 16:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save henrik/5637729 to your computer and use it in GitHub Desktop.
Save henrik/5637729 to your computer and use it in GitHub Desktop.
First stab at extracting breadcrumb trail building from the controller. Uses breadcrumbs_on_rails (https://github.com/weppos/breadcrumbs_on_rails) and attr_extras (https://github.com/barsoom/attr_extras).
class BaseBreadcrumbs
private
def c
controller
end
def edit?
%w[edit update].include?(controller.action_name)
end
def new?
%w[new create].include?(controller.action_name)
end
delegate :add_breadcrumb, :t,
to: :controller
end
class CustomerBreadcrumbs < BaseBreadcrumbs
pattr_initialize :controller, [:customer]
def build
add_breadcrumb t(:"admin.layout.breadcrumb_root"), c.admin_root_path
add_breadcrumb t(:"admin.customers.index.title"), c.admin_customers_path
if customer
add_breadcrumb customer.to_s, c.admin_customer_path(customer)
end
if new?
add_breadcrumb t(:"admin.customers.new.title"), nil
elsif edit?
add_breadcrumb t(:"admin.customers.edit.title"), nil
end
end
end
class Admin::CustomersController < Admin::BaseController
before_filter do
CustomerBreadcrumbs.new(self, customer: customer).build
end
# …
private
def customer
params[:id] && Customer.find(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment