/
action_controller.rb
93 lines (74 loc) · 2.28 KB
/
action_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#--
# Breadcrumbs On Rails
#
# A simple Ruby on Rails plugin for creating and managing a breadcrumb navigation.
#
# Copyright (c) 2009-2012 Simone Carletti <weppos@weppos.net>
#++
module BreadcrumbsOnRails
module ActionController
extend ActiveSupport::Concern
included do
extend ClassMethods
helper HelperMethods
helper_method :add_breadcrumb, :breadcrumbs
end
protected
def add_breadcrumb(name, path = nil, options = {})
self.breadcrumbs << Breadcrumbs::Element.new(name, path, options)
end
def breadcrumbs
@breadcrumbs ||= []
end
module Utils
def self.instance_proc(string)
if string.kind_of?(String)
proc { |controller| controller.instance_eval(string) }
else
string
end
end
# This is an horrible method with an horrible name.
#
# convert_to_set_of_strings(nil, [:foo, :bar])
# # => nil
# convert_to_set_of_strings(true, [:foo, :bar])
# # => ["foo", "bar"]
# convert_to_set_of_strings(:foo, [:foo, :bar])
# # => ["foo"]
# convert_to_set_of_strings([:foo, :bar, :baz], [:foo, :bar])
# # => ["foo", "bar", "baz"]
#
def self.convert_to_set_of_strings(value, keys)
if value == true
keys.map(&:to_s).to_set
elsif value
Array(value).map(&:to_s).to_set
end
end
end
module ClassMethods
def add_breadcrumb(name, path = nil, filter_options = {})
# This isn't really nice here
if eval = Utils.convert_to_set_of_strings(filter_options.delete(:eval), %w(name path))
name = Utils.instance_proc(name) if eval.include?("name")
path = Utils.instance_proc(path) if eval.include?("path")
end
before_filter(filter_options) do |controller|
controller.send(:add_breadcrumb, name, path)
end
end
end
module HelperMethods
def render_breadcrumbs(options = {}, &block)
builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs, options)
content = builder.render.html_safe
if block_given?
capture(content, &block)
else
content
end
end
end
end
end