Module: Crummy::ControllerMethods::ClassMethods

Defined in:
lib/crummy/action_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_crumb(name, *args) ⇒ Object

Add a crumb to the crumbs array.

add_crumb("Home", "/")
add_crumb(lambda { |instance| instance.business_name }, "/")
add_crumb("Business") { |instance| instance.business_path }

Works like a before_filter so :only and except both work.

Raises:

  • (ArgumentError)


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
# File 'lib/crummy/action_controller.rb', line 11

def add_crumb(name, *args)
  options = args.extract_options!
  url = args.first
  raise ArgumentError, "Need more arguments" unless name or options[:record] or block_given?
  raise ArgumentError, "Cannot pass url and use block" if url && block_given?
  before_filter(options) do |instance|
    url = yield instance if block_given?
    url = instance.send url if url.is_a? Symbol
    
    if url.present?
      if url.kind_of? Array
        url.map! do |name|
          name.is_a?(Symbol) ? instance.instance_variable_get("@#{name}") : name
        end
      end
      if not url.kind_of? String
        url = instance.send :url_for, url
      end
    end

    # Get the return value of the name if its a proc.
    name = name.call(instance) if name.is_a?(Proc)

    _record = instance.instance_variable_get("@#{name}") unless name.kind_of?(String)
    if _record and _record.respond_to? :to_param
      instance.add_crumb(_record.to_s, url || instance.url_for(_record), options)
    else 
      instance.add_crumb(name, url, options)
    end
  
    # FIXME: url = instance.url_for(name) if name.respond_to?("to_param") && url.nil?
    # FIXME: Add ||= for the name, url above
  end
end

#clear_crumbsObject



46
47
48
49
50
# File 'lib/crummy/action_controller.rb', line 46

def clear_crumbs
  before_filter do |instance|
    instance.clear_crumbs
  end
end