Class: EasyCrumbs::Breadcrumb
- Inherits:
-
Object
- Object
- EasyCrumbs::Breadcrumb
- Defined in:
- lib/easycrumbs/breadcrumb.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#action_name(action, i18n, name) ⇒ Object
Return name of action.
-
#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.
-
#initialize(object, options = {}) ⇒ Breadcrumb
constructor
Breadcrumb object: object - just object from application.
-
#name_for_controller(i18n) ⇒ Object
Set name for controller.
-
#name_for_model(name_column, i18n) ⇒ Object
Set name for model Model has to have column equal to name_column.
-
#name_for_nil(object, i18n) ⇒ Object
Set name for object if it is nil.
-
#set_name(options = {}) ⇒ Object
Set name for model or controller.
-
#set_object(object) ⇒ Object
Object from application must be a model or controller.
-
#set_path(path, blank_links) ⇒ Object
Set path using hash from ActionController::Routing::Routes.recognize_path Example looks like: => “1”, :movie_id => “1”, :id => “1”, :action => “show”, :controller => “movies”.
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, = {}) @object = set_object(object) @name = set_name() @path = set_path([:path], [:blank_links]) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/easycrumbs/breadcrumb.rb', line 3 def name @name end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
3 4 5 |
# File 'lib/easycrumbs/breadcrumb.rb', line 3 def object @object end |
#path ⇒ Object (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.
80 81 82 |
# File 'lib/easycrumbs/breadcrumb.rb', line 80 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
-
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/easycrumbs/breadcrumb.rb', line 63 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 |
#name_for_controller(i18n) ⇒ Object
Set name for controller
47 48 49 50 51 52 53 |
# File 'lib/easycrumbs/breadcrumb.rb', line 47 def name_for_controller(i18n) if @object.respond_to? :breadcrumb @object. else i18n == true ? I18n.t("breadcrumbs.controllers.#{@object.controller_name}") : @object.controller_name.titlecase 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 |
# File 'lib/easycrumbs/breadcrumb.rb', line 35 def name_for_model(name_column, i18n) raise EasyCrumbs::NoName.new(@object.class, name_column) unless @object.respond_to? name_column name = @object.send name_column name.nil? ? name_for_nil(@object, i18n) : name end |
#name_for_nil(object, i18n) ⇒ Object
Set name for object if it is nil
42 43 44 |
# File 'lib/easycrumbs/breadcrumb.rb', line 42 def name_for_nil(object, i18n) i18n == true ? I18n.t("breadcrumbs.models.#{object.class.to_s.downcase}") : @object.class.to_s 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( = {}) if object.is_a?(ActiveRecord::Base) [:name_column] ||= "breadcrumb" name = name_for_model([:name_column], [:i18n]) else name = name_for_controller([:i18n]) end add_prefix(name, [:action], [:prefix], [: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 ActionController::Routing::Routes.recognize_path Example looks like: => “1”, :movie_id => “1”, :id => “1”, :action => “show”, :controller => “movies”
87 88 89 90 91 92 |
# File 'lib/easycrumbs/breadcrumb.rb', line 87 def set_path(path, blank_links) path.nil? || path.empty? ? "/" : ActionController::Routing::Routes.generate_extras(path).first rescue ActionController::RoutingError => e raise EasyCrumbs::NoPath.new(e.) unless blank_links == true nil end |