Module: Loaf::ViewExtensions
- Includes:
- OptionsValidator
- Defined in:
- lib/loaf/view_extensions.rb
Overview
A mixin to define view extensions
Instance Method Summary collapse
-
#breadcrumb(name, url, options = {}) ⇒ Object
(also: #add_breadcrumb)
Adds breadcrumbs inside view.
-
#breadcrumb_trail(options = {}) ⇒ Object
Renders breadcrumbs inside view.
-
#breadcrumbs? ⇒ Boolean
Checks to see if any breadcrumbs have been added.
-
#current_crumb?(path, pattern = :inclusive) ⇒ Boolean
Check if breadcrumb is current based on the pattern.
- #initialize ⇒ Object
Methods included from OptionsValidator
Instance Method Details
#breadcrumb(name, url, options = {}) ⇒ Object Also known as:
Adds breadcrumbs inside view.
37 38 39 |
# File 'lib/loaf/view_extensions.rb', line 37 def (name, url, = {}) << Loaf::Crumb.new(name, url, ) end |
#breadcrumb_trail(options = {}) ⇒ Object
Renders breadcrumbs inside view.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/loaf/view_extensions.rb', line 47 def ( = {}) return enum_for(:breadcrumb_trail, ) unless block_given? valid?() .each do |crumb| name = title_for(crumb.name) path = url_for((crumb.url)) current = current_crumb?(path, .fetch(:match) { crumb.match }) yield(Loaf::Breadcrumb[name, path, current]) end end |
#breadcrumbs? ⇒ Boolean
Checks to see if any breadcrumbs have been added
23 24 25 |
# File 'lib/loaf/view_extensions.rb', line 23 def .present? end |
#current_crumb?(path, pattern = :inclusive) ⇒ Boolean
Check if breadcrumb is current based on the pattern
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 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/loaf/view_extensions.rb', line 68 def current_crumb?(path, pattern = :inclusive) return false unless request.get? || request.head? origin_path = URI::DEFAULT_PARSER.unescape(path).force_encoding(Encoding::BINARY) request_uri = request.fullpath request_uri = URI::DEFAULT_PARSER.unescape(request_uri) request_uri.force_encoding(Encoding::BINARY) # strip away trailing slash if origin_path.start_with?('/') && origin_path != '/' origin_path.chomp!('/') request_uri.chomp!('/') end if %r{^\w+://} =~ origin_path origin_path.chomp!('/') request_uri.insert(0, "#{request.protocol}#{request.host_with_port}") end case pattern when :inclusive !request_uri.match(/^#{Regexp.escape(origin_path)}(\/.*|\?.*)?$/).nil? when :exclusive !request_uri.match(/^#{Regexp.escape(origin_path)}\/?(\?.*)?$/).nil? when :exact request_uri == origin_path when :force true when Regexp !request_uri.match(pattern).nil? when Hash query_params = URI.encode_www_form(pattern) !request_uri.match(/^#{Regexp.escape(origin_path)}\/?(\?.*)?.*?#{query_params}.*$/).nil? else raise ArgumentError, "Unknown `:#{pattern}` match option!" end end |
#initialize ⇒ Object
13 14 15 16 |
# File 'lib/loaf/view_extensions.rb', line 13 def initialize(*) @_breadcrumbs ||= [] super end |