easy_breadcrumbs 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/easy_breadcrumbs.gemspec +32 -0
- data/lib/assets/breadcrumbs.eruby +14 -0
- data/lib/easy_breadcrumbs.rb +26 -0
- data/lib/easy_breadcrumbs/breadcrumb.rb +76 -0
- data/lib/easy_breadcrumbs/version.rb +3 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fcf9be5056635f695ed430f771d5ca29e4246a22
|
4
|
+
data.tar.gz: 32c198b059cad879a487b54ff286c29b53451bf1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e55a3be879f425b60a06f768171ce920dd644f11043b34cec8c99e31a2a9c07761bc77f45e9e39ab97c1ef30674834359f80d7d7fac2e1d3fae1c95a476dff1a
|
7
|
+
data.tar.gz: c584c9494c1eca7fd9b53ce63cdce7b723106cbb80397ee0f34891a34214b99a4f2effd6a710fe3990a240f1ac52510249eb523657c425658fdfdb0dcfc5d883
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Eliathah Boda
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# EasyBreadcrumbs
|
2
|
+
|
3
|
+
Provides an `easy_breadcrumbs` view helper for automatically generating bootstrap breadcrumbs for your Sinatra Application.
|
4
|
+
|
5
|
+
* It's able to properly generate the html for many different types of complex routes (See Below).
|
6
|
+
* It only generates breadcrumbs for the routes you've defined, so if you have a route `/categories/10/contacts/5`, but haven't defined an index view route for `/categories`, that item will be left out.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'easy_breadcrumbs'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install easy_breadcrumbs
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Step 1
|
27
|
+
|
28
|
+
If you're building a classic style Sinatra app, simply add this line to your sinatra application file:
|
29
|
+
|
30
|
+
require "easy_breadcrumbs"
|
31
|
+
|
32
|
+
If you're building a modular style Sinatra app, you'll also need to explicitly register the module within your application.
|
33
|
+
|
34
|
+
class HelloApp < Sinatra::Base
|
35
|
+
helpers Sinatra::EasyBreadcrumbs
|
36
|
+
|
37
|
+
get "/hello" do
|
38
|
+
h "1 < 2"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
### Step 2
|
43
|
+
|
44
|
+
And then use the helper method in your layout or view:
|
45
|
+
|
46
|
+
<%= easy_breadcrumbs %>
|
47
|
+
|
48
|
+
### Step 3
|
49
|
+
|
50
|
+
And last but not least, make sure you have bootstrap installed. You can install bootstrap's css by adding this line to your applications layout template.
|
51
|
+
|
52
|
+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
|
53
|
+
|
54
|
+
## Details
|
55
|
+
|
56
|
+
Easy Breadcrumbs is able to handle a variety of complex routes. Here are some examples:
|
57
|
+
|
58
|
+
### Simple path to page
|
59
|
+
```
|
60
|
+
Path: /about
|
61
|
+
Breadcrumb: Home > About
|
62
|
+
```
|
63
|
+
|
64
|
+
### Path to resource index page
|
65
|
+
```
|
66
|
+
Path: /contacts
|
67
|
+
Breadcrumb: Home > Contacts
|
68
|
+
```
|
69
|
+
|
70
|
+
###Path to specific resource
|
71
|
+
```
|
72
|
+
Path: /contacts/28
|
73
|
+
Breadcrumb: Home > Contacts > Contact
|
74
|
+
```
|
75
|
+
|
76
|
+
### Path to resource new view
|
77
|
+
```
|
78
|
+
Path: /contacts/new
|
79
|
+
Breadcrumb: Home > Contacts > New Contact
|
80
|
+
```
|
81
|
+
|
82
|
+
### Path to edit specific resource
|
83
|
+
```
|
84
|
+
Path: /contacts/28/edit
|
85
|
+
Breadcrumb: Home > Contacts > Contact > Edit Contact
|
86
|
+
```
|
87
|
+
|
88
|
+
### Path to nested resource index page
|
89
|
+
```
|
90
|
+
Path: /categories/5/contacts
|
91
|
+
Breadcrumb: Home > Categories > Category > Contacts
|
92
|
+
```
|
93
|
+
|
94
|
+
### Path to specific nested resource
|
95
|
+
```
|
96
|
+
Path: /categories/5/contacts/10
|
97
|
+
Breadcrumb: Home > Categories > Category > Contacts > Contact
|
98
|
+
```
|
99
|
+
|
100
|
+
### Path to nested resource new view
|
101
|
+
```
|
102
|
+
Path: /categories/5/contacts/new
|
103
|
+
Breadcrumb: Home > Categories > Category > Contacts > New Contact
|
104
|
+
```
|
105
|
+
|
106
|
+
### Path to edit specific nested resource
|
107
|
+
```
|
108
|
+
Path: /categories/5/contacts/10/edit
|
109
|
+
Breadcrumb: Home > Categories > Category > Contacts > Contact > Edit Contact
|
110
|
+
```
|
111
|
+
|
112
|
+
## Road Map
|
113
|
+
|
114
|
+
* Add configuration for showing individual resource name/title as breadcrumb (e.g. "Ada Lovelace") anchor text rather than resource type name (e.g. "Contact")
|
115
|
+
* Integration for Rails
|
116
|
+
* A simple means of configuration for things like:
|
117
|
+
* Turning on and off specific routes
|
118
|
+
* Setting breadcrumb styles (maybe provide an option for a different framework like Foundation, or just provide a few different styling presets)
|
119
|
+
|
120
|
+
## Contributing
|
121
|
+
|
122
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nerboda/easy_breadcrumbs.
|
123
|
+
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
128
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "easy_breadcrumbs"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'easy_breadcrumbs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "easy_breadcrumbs"
|
8
|
+
spec.version = EasyBreadcrumbs::VERSION
|
9
|
+
spec.authors = ["Eliathah Boda"]
|
10
|
+
spec.email = ["nerboda@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Automatically add bootstrap breadcrumbs to your Sinatra app.}
|
13
|
+
spec.description = %q{Provides an `easy_breadcrumbs` view helper for automatically generating bootstrap breadcrumbs for your Sinatra Application.}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "sinatra"
|
25
|
+
spec.add_dependency "activesupport"
|
26
|
+
spec.add_dependency "erubis"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "rack-test"
|
32
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<ol class="breadcrumb">
|
2
|
+
<% if @links.empty? %>
|
3
|
+
<li class="breadcrumb-item active">Home</li>
|
4
|
+
<% else %>
|
5
|
+
<li class="breadcrumb-item"><a href="/">Home</a></li>
|
6
|
+
<% end %>
|
7
|
+
<% @links.each do |link| %>
|
8
|
+
<% if link.path == @links.last.path %>
|
9
|
+
<li class="breadcrumb-item active"><%= link.text %></li>
|
10
|
+
<% else %>
|
11
|
+
<li class="breadcrumb-item"><a href="<%= link.path %>"><%= link.text %></a></li>
|
12
|
+
<% end %>
|
13
|
+
<% end %>
|
14
|
+
</ol>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'easy_breadcrumbs/version'
|
3
|
+
require 'easy_breadcrumbs/breadcrumb'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
include ::EasyBreadcrumbs
|
7
|
+
|
8
|
+
module EasyBreadcrumbs
|
9
|
+
Breadcrumb = ::EasyBreadcrumbs::Breadcrumb
|
10
|
+
|
11
|
+
def easy_breadcrumbs
|
12
|
+
# Path from current Rack::Request object
|
13
|
+
request_path = request.path
|
14
|
+
|
15
|
+
# All GET request routes
|
16
|
+
route_matchers = self.class.routes['GET'].map { |route| route[0] }
|
17
|
+
|
18
|
+
# The rest is handled by Breadcrumb class
|
19
|
+
breadcrumb = Breadcrumb.new(request_path, route_matchers)
|
20
|
+
breadcrumb.to_html
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Register helper with Sinatra::Application
|
25
|
+
helpers EasyBreadcrumbs
|
26
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'erubis'
|
2
|
+
require 'active_support/inflector/methods'
|
3
|
+
|
4
|
+
module EasyBreadcrumbs
|
5
|
+
# Breadcrumb class. Converts a URL path into Bootstrap breadcrumbs
|
6
|
+
class Breadcrumb
|
7
|
+
include ActiveSupport::Inflector
|
8
|
+
|
9
|
+
def initialize(request_path, route_matchers, options = {})
|
10
|
+
@configuration = options
|
11
|
+
@routes = route_matchers
|
12
|
+
@directories = request_path.scan(%r{\/[^\/]+})
|
13
|
+
@links = []
|
14
|
+
|
15
|
+
build_links!
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_html
|
19
|
+
path = File.expand_path('../../assets/breadcrumbs.eruby', __FILE__)
|
20
|
+
template = File.read(path)
|
21
|
+
|
22
|
+
eruby = Erubis::Eruby.new(template)
|
23
|
+
eruby.result(binding)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
Link = Struct.new(:path, :text)
|
29
|
+
|
30
|
+
def build_links!
|
31
|
+
@directories.each_with_index do |directory, index|
|
32
|
+
full_path = @directories[0..index].join
|
33
|
+
|
34
|
+
if defined_route?(full_path)
|
35
|
+
anchor_text = to_anchor_text(directory, index)
|
36
|
+
@links << Link.new(full_path, anchor_text)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_anchor_text(directory, index)
|
42
|
+
if directory =~ /\d/
|
43
|
+
format_for_specific_resource(index)
|
44
|
+
elsif %w(/edit /new).include?(directory)
|
45
|
+
format_with_view_prefix(directory, index)
|
46
|
+
else
|
47
|
+
clean_and_capitalize(directory)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def format_for_specific_resource(index)
|
52
|
+
previous_directory = @directories[index - 1]
|
53
|
+
text_for_anchor = clean_and_capitalize(previous_directory)
|
54
|
+
singularize(text_for_anchor)
|
55
|
+
end
|
56
|
+
|
57
|
+
def format_with_view_prefix(directory, index)
|
58
|
+
previous_index = directory == "/edit" ? index - 2 : index - 1
|
59
|
+
previous_directory = @directories[previous_index]
|
60
|
+
|
61
|
+
view_prefix = clean_and_capitalize(directory)
|
62
|
+
rest_of_anchor = clean_and_capitalize(previous_directory)
|
63
|
+
singularized = singularize(rest_of_anchor)
|
64
|
+
|
65
|
+
"#{view_prefix} #{singularized}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def clean_and_capitalize(directory)
|
69
|
+
directory.delete('/').capitalize
|
70
|
+
end
|
71
|
+
|
72
|
+
def defined_route?(path)
|
73
|
+
@routes.any? { |route| path =~ route }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_breadcrumbs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eliathah Boda
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: erubis
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.13'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rack-test
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Provides an `easy_breadcrumbs` view helper for automatically generating
|
112
|
+
bootstrap breadcrumbs for your Sinatra Application.
|
113
|
+
email:
|
114
|
+
- nerboda@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- easy_breadcrumbs.gemspec
|
129
|
+
- lib/assets/breadcrumbs.eruby
|
130
|
+
- lib/easy_breadcrumbs.rb
|
131
|
+
- lib/easy_breadcrumbs/breadcrumb.rb
|
132
|
+
- lib/easy_breadcrumbs/version.rb
|
133
|
+
homepage: ''
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.6.8
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Automatically add bootstrap breadcrumbs to your Sinatra app.
|
157
|
+
test_files: []
|