Class: BreadnButter::Crumb

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

Constant Summary collapse

JOINER =
"»".html_safe
HIDDEN_ELEMENTS =
[]
HOME_URL =
'/'
HOME_TEXT =
'HOME'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#textObject

Returns the value of attribute text.



17
18
19
# File 'lib/breadnbutter.rb', line 17

def text
  @text
end

#urlObject

Returns the value of attribute url.



17
18
19
# File 'lib/breadnbutter.rb', line 17

def url
  @url
end

Class Method Details

.generate_crumbs_from_url(in_url) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/breadnbutter.rb', line 86

def self.generate_crumbs_from_url(in_url)
  output = []
  used_elements = []
  elements = in_url.split('/')
  last_index = elements.length - 1
  elements.each_with_index do |element, index|
    used_elements << element
    url = used_elements.join('/')
    previous_element = elements[index -1]
    text = self.get_text_from_element(element, previous_element)
    output_url = (index < last_index) ? url : nil
    new_crumb = self.new_from_hash(url: output_url, text: text)
    output << new_crumb unless new_crumb.hidden?
  end
  self.override_first(output)
end

.get_text_from_element(element, previous_element) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/breadnbutter.rb', line 66

def self.get_text_from_element(element, previous_element)
  if element =~ /^[0-9]+$/      
    model = previous_element.classify.constantize
    begin
      text = model.find(element).name
    rescue NoMethodError
      text = element
    end
  else
    text = element
  end
  text.titleize
end

.home_textObject



43
44
45
46
47
48
49
# File 'lib/breadnbutter.rb', line 43

def self.home_text
  begin
    Rails.configuration.breadnbutter_home_text
  rescue NoMethodError
    HOME_TEXT
  end
end

.home_urlObject



51
52
53
54
55
56
57
# File 'lib/breadnbutter.rb', line 51

def self.home_url
  begin
    Rails.configuration.breadnbutter_home_url
  rescue NoMethodError
    HOME_URL
  end
end

.new_from_hash(hash) ⇒ Object



59
60
61
62
63
64
# File 'lib/breadnbutter.rb', line 59

def self.new_from_hash(hash)
  new_crumb = self.new
  new_crumb.text = value_from_key(:text, hash)
  new_crumb.url = value_from_key(:url, hash)
  new_crumb
end

.override_first(array) ⇒ Object



80
81
82
83
84
# File 'lib/breadnbutter.rb', line 80

def self.override_first(array)
  first = self.new_from_hash(text: home_text, url: home_url)
  array[0] = first
  array
end

Instance Method Details

#hidden?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/breadnbutter.rb', line 23

def hidden?
  hidden_elements.include? text
end

#hidden_elementsObject



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

def hidden_elements
  begin
    Rails.configuration.breadnbutter_hidden_elements
  rescue NoMethodError
    HIDDEN_ELEMENTS
  end
end

#joinerObject



27
28
29
30
31
32
33
# File 'lib/breadnbutter.rb', line 27

def joiner
  begin
    Rails.configuration.breadnbutter_joiner
  rescue NoMethodError
    JOINER
  end
end

#linkableObject



19
20
21
# File 'lib/breadnbutter.rb', line 19

def linkable
  url.present?
end