Module: Loaf::ViewExtensions

Includes:
OptionsValidator
Defined in:
lib/loaf/view_extensions.rb

Overview

A mixin to define view extensions

Instance Method Summary collapse

Methods included from OptionsValidator

#valid?

Instance Method Details

Adds breadcrumbs inside view.

Parameters:

  • name (String)

    the breadcrumb name

  • url (Object)

    the breadcrumb url

  • options (Hash) (defaults to: {})

    the breadcrumb options



37
38
39
# File 'lib/loaf/view_extensions.rb', line 37

def breadcrumb(name, url, options = {})
  _breadcrumbs << Loaf::Crumb.new(name, url, options)
end

Renders breadcrumbs inside view.

Parameters:

  • options (Hash) (defaults to: {})


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/loaf/view_extensions.rb', line 47

def breadcrumb_trail(options = {})
  return enum_for(:breadcrumb_trail, options) unless block_given?

  valid?(options)

  _breadcrumbs.each do |crumb|
    name = title_for(crumb.name)
    path = url_for(_expand_url(crumb.url))
    current = current_crumb?(path, options.fetch(:match) { crumb.match })

    yield(Loaf::Breadcrumb[name, path, current])
  end
end

Checks to see if any breadcrumbs have been added

Returns:

  • (Boolean)


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

def breadcrumbs?
  _breadcrumbs.present?
end

#current_crumb?(path, pattern = :inclusive) ⇒ Boolean

Check if breadcrumb is current based on the pattern

Parameters:

  • path (String)
  • pattern (Object) (defaults to: :inclusive)

    the pattern to match on

Returns:

  • (Boolean)


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

#initializeObject



13
14
15
16
# File 'lib/loaf/view_extensions.rb', line 13

def initialize(*)
  @_breadcrumbs ||= []
  super
end