Class: EasyCrumbs::Breadcrumb

Inherits:
Object
  • Object
show all
Defined in:
lib/easycrumbs/breadcrumb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Breadcrumb

Breadcrumb object: object - just object from application. Could be a model or controller name - printed name path - path to this object



9
10
11
12
13
# File 'lib/easycrumbs/breadcrumb.rb', line 9

def initialize(object, options = {})
  @object = set_object(object)
  @name = set_name(options)
  @path = set_path(options[:path], options[:blank_links])
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/easycrumbs/breadcrumb.rb', line 3

def name
  @name
end

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/easycrumbs/breadcrumb.rb', line 3

def object
  @object
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/easycrumbs/breadcrumb.rb', line 3

def path
  @path
end

Instance Method Details

#action_name(action, i18n, name) ⇒ Object

Return name of action.



87
88
89
# File 'lib/easycrumbs/breadcrumb.rb', line 87

def action_name(action, i18n, name)
  i18n == true ? I18n.t("breadcrumbs.actions.#{action}", :name => name) : "#{action.titlecase} #{name}"
end

#add_prefix(object_name, action, prefix, i18n) ⇒ Object

Add specyfic prefix if action is passed prefix = :every - add prefix for every action :none - do not add prefix

array of symbols
  • add prefix only for actions in array

Example

:show, :new
  • add prefix only for show and new



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/easycrumbs/breadcrumb.rb', line 70

def add_prefix(object_name, action, prefix, i18n)
  name = object_name
  unless action.nil?
    prefix = case prefix
      when :every
        [action.to_sym]
      when :none
        []
      else
        prefix || [:new, :edit]
    end
    name = action_name(action, i18n, name) if prefix.include?(action.to_sym)
  end
  name
end

#default_controller_nameObject

Return default name for controller object



58
59
60
# File 'lib/easycrumbs/breadcrumb.rb', line 58

def default_controller_name
  @object.class == ApplicationController ? "Home" : @object.controller_name.titlecase
end

#default_model_nameObject

Return default name for model object



53
54
55
# File 'lib/easycrumbs/breadcrumb.rb', line 53

def default_model_name
  @object.class.to_s
end

#name_for_controller(i18n) ⇒ Object

Set name for controller



44
45
46
47
48
49
50
# File 'lib/easycrumbs/breadcrumb.rb', line 44

def name_for_controller(i18n)
  if @object.respond_to? :breadcrumb
    @object.breadcrumb
  else
    i18n == true ? I18n.t("breadcrumbs.controllers.#{@object.controller_name}") : default_controller_name
  end
end

#name_for_model(name_column, i18n) ⇒ Object

Set name for model Model has to have column equal to name_column



35
36
37
38
39
40
41
# File 'lib/easycrumbs/breadcrumb.rb', line 35

def name_for_model(name_column, i18n)
  if @object.respond_to? name_column
    @object.send name_column
  else
    i18n == true ? I18n.t("breadcrumbs.models.#{object.class.to_s.downcase}") : default_model_name
  end
end

#set_name(options = {}) ⇒ Object

Set name for model or controller



23
24
25
26
27
28
29
30
31
# File 'lib/easycrumbs/breadcrumb.rb', line 23

def set_name(options = {})
  if object.is_a?(ActiveRecord::Base)
    options[:name_column] ||= "breadcrumb"
    name = name_for_model(options[:name_column], options[:i18n])
  else
    name = name_for_controller(options[:i18n])
  end
  add_prefix(name, options[:action], options[:prefix], options[:i18n])
end

#set_object(object) ⇒ Object

Object from application must be a model or controller



17
18
19
20
# File 'lib/easycrumbs/breadcrumb.rb', line 17

def set_object(object)
  raise EasyCrumbs::InvalidObject unless object.is_a?(ActionController::Base) || object.is_a?(ActiveRecord::Base)
  object
end

#set_path(path, blank_links) ⇒ Object

Set path using hash from Rails.application.routes.recognize_path Example looks like: => “1”, :movie_id => “1”, :id => “1”, :action => “show”, :controller => “movies”



94
95
96
97
98
99
# File 'lib/easycrumbs/breadcrumb.rb', line 94

def set_path(path, blank_links)
  path.nil? || path.empty? ? "/" : Rails.application.routes.generate_extras(path).first
  rescue ActionController::RoutingError => e
    raise EasyCrumbs::NoPath.new(e.message) unless blank_links == true
    nil
end