loaf 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +15 -3
- data/Gemfile.lock +1 -1
- data/README.md +27 -7
- data/lib/loaf/configuration.rb +0 -12
- data/lib/loaf/translation.rb +12 -4
- data/lib/loaf/version.rb +3 -1
- data/lib/loaf/view_extensions.rb +11 -3
- data/spec/unit/configuration_spec.rb +3 -9
- data/spec/unit/options_validator_spec.rb +1 -1
- data/spec/unit/translation_spec.rb +8 -0
- data/spec/unit/view_extensions/breadcrumb_trail_spec.rb +18 -6
- metadata +3 -6
- data/lib/loaf/crumb_formatter.rb +0 -24
- data/spec/unit/crumb_formatter_spec.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1345a409b2a725846ac573193095c6a53b398a23e87bcace96c0cc06d0ff1c27
|
4
|
+
data.tar.gz: 3f60d6a1921ead6fd51654aa2f3e80fbd85ee53955f9e3aa40cd850fd1d00a86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7689bbb410e7b440ba0a4bec01ad93f17d4d42412913732f530e862c6abed8a4a4519bf0d1064566da2346c798e9c04673edf97c8e3e76543bc21cb7f041adc5
|
7
|
+
data.tar.gz: ee103f98f0a5ddde85c4594e3efafeebe76acfc316147524e6922dca990282fc334d626550091eec0a50d0bc14c35b7bdefd08be690001d977b86cbc81584d00
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.8.0] - 2018-08-07
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change Translation to skip translating nil and empty string
|
7
|
+
* Change view extension to only lookup breadcrumb name translation
|
8
|
+
* Remove Configuration #crumb_length and #capitalize options
|
9
|
+
* Remove CrumbFormatter to skip truncating and formatting crumb names
|
10
|
+
|
11
|
+
## Fix
|
12
|
+
* Fix issue with breadcrumb names being modified
|
13
|
+
|
3
14
|
## [v0.7.0] - 2018-06-20
|
4
15
|
|
5
16
|
### Added
|
@@ -67,7 +78,7 @@
|
|
67
78
|
### Fixed
|
68
79
|
* Fix bug with url parameter to allow for regular rails path variables
|
69
80
|
|
70
|
-
## [
|
81
|
+
## [v0.3.0] - 2012-02-25
|
71
82
|
|
72
83
|
### Added
|
73
84
|
* Add loaf gem errors
|
@@ -78,13 +89,13 @@
|
|
78
89
|
* Renamed main gem helpers for adding breadcrumbs from `add_breadcrumb` to
|
79
90
|
`breadcrumb`, both inside controllers and views.
|
80
91
|
|
81
|
-
## [
|
92
|
+
## [v0.2.1] - 2012-02-22
|
82
93
|
|
83
94
|
### Added
|
84
95
|
* Add more integration tests and fixed bug with adding breadcrumbs inside view
|
85
96
|
* Add specs for controller extensions
|
86
97
|
|
87
|
-
## [
|
98
|
+
## [v0.2.0] - 2012-02-18
|
88
99
|
|
89
100
|
### Added
|
90
101
|
* Add integration tests for breadcrumbs view rendering
|
@@ -100,6 +111,7 @@
|
|
100
111
|
|
101
112
|
* Initial implementation and release
|
102
113
|
|
114
|
+
[v0.8.0]: https://github.com/piotrmurach/tty-spinner/compare/v0.7.0...v0.8.0
|
103
115
|
[v0.7.0]: https://github.com/piotrmurach/tty-spinner/compare/v0.6.2...v0.7.0
|
104
116
|
[v0.6.2]: https://github.com/piotrmurach/tty-spinner/compare/v0.6.1...v0.6.2
|
105
117
|
[v0.6.1]: https://github.com/piotrmurach/tty-spinner/compare/v0.6.0...v0.6.1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -163,6 +163,18 @@ class CommentsController < ApplicationController
|
|
163
163
|
end
|
164
164
|
```
|
165
165
|
|
166
|
+
You may wish to define breadcrumbs over a collection. This is easy within views, and controller actions (just loop your collection), but if you want to do this in the controller class you can use the `before_action` approach:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
before_action do
|
170
|
+
ancestors.each do |ancestor|
|
171
|
+
breadcrumb ancestor.name, [:admin, ancestor]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
Assume `ancestors` method is defined inside the controller.
|
177
|
+
|
166
178
|
#### 2.1.2 view
|
167
179
|
|
168
180
|
**Loaf** adds `breadcrumb` helper also to the views. Together with controller breadcrumbs, the view breadcrumbs are appended as the last in breadcrumb trail. For instance, to specify view breadcrumb do:
|
@@ -228,9 +240,10 @@ For example, you can add the following semantic markup to show breadcrumbs using
|
|
228
240
|
</ol>
|
229
241
|
</nav>
|
230
242
|
```
|
243
|
+
For Bootstrap 4:
|
231
244
|
|
232
|
-
For bootstrap,
|
233
245
|
```erb
|
246
|
+
<% #erb %>
|
234
247
|
<nav aria-label="breadcrumb">
|
235
248
|
<ol class='breadcrumbs'>
|
236
249
|
<% breadcrumb_trail do |crumb| %>
|
@@ -242,23 +255,30 @@ For bootstrap,
|
|
242
255
|
</nav>
|
243
256
|
```
|
244
257
|
|
258
|
+
and if you are using HAML do:
|
259
|
+
|
260
|
+
```haml
|
261
|
+
- # haml
|
262
|
+
%ol.breadcrumbs
|
263
|
+
- breadcrumb_trail do |crumb|
|
264
|
+
%li.breadcrumb-item{class: crumb.current? ? 'active' : '' }
|
265
|
+
= link_to_unless crumb.current?, crumb.name, crumb.url, (crumb.current? ? {'aria-current' => 'page'} : {})
|
266
|
+
```
|
245
267
|
|
246
|
-
Usually best practice is to put such snippet inside its own partial.
|
268
|
+
Usually best practice is to put such snippet inside its own `_breadcrumbs.html.erb` partial.
|
247
269
|
|
248
270
|
## 3. Configuration
|
249
271
|
|
250
272
|
There is a small set of custom opinionated defaults. The following options are valid parameters:
|
251
273
|
|
252
274
|
```ruby
|
253
|
-
:
|
254
|
-
:crumb_length # breadcrumb length in integer, default length is 30 characters
|
255
|
-
:match # set match type, default :inclusive
|
275
|
+
:match # set match type, default :inclusive (see [:match](#213-match) for more details)
|
256
276
|
```
|
257
277
|
|
258
278
|
You can override them in your views by passing them to the view `breadcrumb` helper
|
259
279
|
|
260
280
|
```erb
|
261
|
-
<% breadcrumb_trail
|
281
|
+
<% breadcrumb_trail(match: :exclusive) do |name, url, styles| %>
|
262
282
|
..
|
263
283
|
<% end %>
|
264
284
|
```
|
@@ -267,7 +287,7 @@ or by configuring an option in `config/initializers/loaf.rb`:
|
|
267
287
|
|
268
288
|
```ruby
|
269
289
|
Loaf.configure do |config|
|
270
|
-
config.
|
290
|
+
config.match = :exclusive
|
271
291
|
end
|
272
292
|
```
|
273
293
|
|
data/lib/loaf/configuration.rb
CHANGED
@@ -4,8 +4,6 @@ module Loaf
|
|
4
4
|
class Configuration
|
5
5
|
VALID_ATTRIBUTES = [
|
6
6
|
:locales_path,
|
7
|
-
:crumb_length,
|
8
|
-
:capitalize,
|
9
7
|
:match
|
10
8
|
].freeze
|
11
9
|
|
@@ -13,18 +11,8 @@ module Loaf
|
|
13
11
|
|
14
12
|
DEFAULT_LOCALES_PATH = '/'
|
15
13
|
|
16
|
-
DEFAULT_STYLE_CLASSES = 'selected'
|
17
|
-
|
18
|
-
DEFAULT_CRUMB_LENGTH = 30
|
19
|
-
|
20
|
-
DEFAULT_LAST_CRUMB_LINKED = false
|
21
|
-
|
22
|
-
DEFAULT_CAPITALIZE = false
|
23
|
-
|
24
14
|
DEFAULT_MATCH = :inclusive
|
25
15
|
|
26
|
-
DEFAULT_ROOT = true
|
27
|
-
|
28
16
|
# Setup this configuration
|
29
17
|
#
|
30
18
|
# @api public
|
data/lib/loaf/translation.rb
CHANGED
@@ -2,12 +2,15 @@
|
|
2
2
|
|
3
3
|
module Loaf
|
4
4
|
module Translation
|
5
|
-
extend self
|
6
|
-
|
7
5
|
# Returns translation lookup
|
6
|
+
#
|
7
|
+
# @return [String]
|
8
|
+
#
|
9
|
+
# @api private
|
8
10
|
def translation_scope
|
9
|
-
|
11
|
+
'loaf.breadcrumbs'
|
10
12
|
end
|
13
|
+
module_function :translation_scope
|
11
14
|
|
12
15
|
# Translate breadcrumb title
|
13
16
|
#
|
@@ -18,12 +21,17 @@ module Loaf
|
|
18
21
|
# @option options [String] :default
|
19
22
|
# The default translation
|
20
23
|
#
|
24
|
+
# @return [String]
|
25
|
+
#
|
21
26
|
# @api public
|
22
27
|
def find_title(title, options = {})
|
28
|
+
return title if title.nil? || title.empty?
|
29
|
+
|
23
30
|
options[:scope] ||= translation_scope
|
24
31
|
options[:default] = Array(options[:default])
|
25
32
|
options[:default] << title if options[:default].empty?
|
26
|
-
I18n.t(
|
33
|
+
I18n.t(title.to_s, options)
|
27
34
|
end
|
35
|
+
module_function :find_title
|
28
36
|
end # Translation
|
29
37
|
end # Loaf
|
data/lib/loaf/version.rb
CHANGED
data/lib/loaf/view_extensions.rb
CHANGED
@@ -2,13 +2,12 @@
|
|
2
2
|
|
3
3
|
require_relative 'breadcrumb'
|
4
4
|
require_relative 'crumb'
|
5
|
-
require_relative 'crumb_formatter'
|
6
5
|
require_relative 'options_validator'
|
6
|
+
require_relative 'translation'
|
7
7
|
|
8
8
|
module Loaf
|
9
9
|
# A mixin to define view extensions
|
10
10
|
module ViewExtensions
|
11
|
-
include Loaf::CrumbFormatter
|
12
11
|
include Loaf::OptionsValidator
|
13
12
|
|
14
13
|
def initialize(*)
|
@@ -51,7 +50,7 @@ module Loaf
|
|
51
50
|
valid?(options)
|
52
51
|
options = Loaf.configuration.to_hash.merge(options)
|
53
52
|
_breadcrumbs.each do |crumb|
|
54
|
-
name =
|
53
|
+
name = title_for(crumb.name)
|
55
54
|
path = url_for(_expand_url(crumb.url))
|
56
55
|
current = current_crumb?(path, crumb.match)
|
57
56
|
|
@@ -107,6 +106,15 @@ module Loaf
|
|
107
106
|
|
108
107
|
private
|
109
108
|
|
109
|
+
# Find title translation for a crumb name
|
110
|
+
#
|
111
|
+
# @return [String]
|
112
|
+
#
|
113
|
+
# @api private
|
114
|
+
def title_for(name)
|
115
|
+
Translation.find_title(name)
|
116
|
+
end
|
117
|
+
|
110
118
|
# Expand url in the current context of the view
|
111
119
|
#
|
112
120
|
# @api private
|
@@ -1,29 +1,23 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
RSpec.describe Loaf::Configuration do
|
4
4
|
it "allows to set and read attributes" do
|
5
5
|
config = Loaf::Configuration.new
|
6
|
-
|
7
|
-
config.crumb_length = 4
|
8
|
-
expect(config.crumb_length).to eq(4)
|
9
|
-
|
10
6
|
config.match = :exact
|
11
7
|
expect(config.match).to eq(:exact)
|
12
8
|
end
|
13
9
|
|
14
10
|
it "accepts attributes at initialization" do
|
15
|
-
options = {
|
11
|
+
options = { locales_path: '/lib', match: :exact }
|
16
12
|
config = Loaf::Configuration.new(options)
|
17
13
|
|
18
|
-
expect(config.
|
14
|
+
expect(config.locales_path).to eq('/lib')
|
19
15
|
expect(config.match).to eq(:exact)
|
20
16
|
end
|
21
17
|
|
22
18
|
it "exports configuration as hash" do
|
23
19
|
config = Loaf::Configuration.new
|
24
20
|
expect(config.to_hash).to eq({
|
25
|
-
capitalize: false,
|
26
|
-
crumb_length: 30,
|
27
21
|
locales_path: '/',
|
28
22
|
match: :inclusive
|
29
23
|
})
|
@@ -4,7 +4,7 @@ RSpec.describe Loaf::OptionsValidator, '.valid?' do
|
|
4
4
|
let(:klass) { Class.extend Loaf::OptionsValidator }
|
5
5
|
|
6
6
|
it 'validates succesfully known option' do
|
7
|
-
expect(klass.valid?(
|
7
|
+
expect(klass.valid?(match: :exact)).to eq(true)
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'validates unknown option with an error' do
|
@@ -6,6 +6,14 @@ RSpec.describe Loaf::Translation do
|
|
6
6
|
|
7
7
|
after { I18n.backend.reload! }
|
8
8
|
|
9
|
+
it "doesn't translate empty title" do
|
10
|
+
expect(described_class.find_title('')).to eql('')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "skips translation if doesn't find a matching scope" do
|
14
|
+
expect(described_class.find_title('unknown')).to eql('unknown')
|
15
|
+
end
|
16
|
+
|
9
17
|
it 'translates breadcrumb title' do
|
10
18
|
I18n.backend.store_translations 'en', loaf: { breadcrumbs: { home: 'Home'}}
|
11
19
|
expect(described_class.find_title('home')).to eql('Home')
|
@@ -196,8 +196,21 @@ RSpec.describe Loaf::ViewExtensions, '#breadcrumb_trail' do
|
|
196
196
|
}.to raise_error(Loaf::InvalidOptions)
|
197
197
|
end
|
198
198
|
|
199
|
+
it "permits arbitrary length crumb names" do
|
200
|
+
view = DummyView.new
|
201
|
+
view.breadcrumb('<span class="fa fa-home"></span>', :home_path)
|
202
|
+
view.set_path('/posts')
|
203
|
+
|
204
|
+
yielded = []
|
205
|
+
block = -> (crumb) { yielded << crumb.to_a }
|
206
|
+
view.breadcrumb_trail(&block)
|
207
|
+
|
208
|
+
expect(yielded).to eq([
|
209
|
+
['<span class="fa fa-home"></span>', '/', false]
|
210
|
+
])
|
211
|
+
end
|
212
|
+
|
199
213
|
it 'uses global configuration for crumb formatting' do
|
200
|
-
allow(Loaf.configuration).to receive(:crumb_length).and_return(10)
|
201
214
|
view = DummyView.new
|
202
215
|
view.breadcrumb('home-sweet-home', :home_path)
|
203
216
|
view.breadcrumb('posts-for-everybody', :posts_path)
|
@@ -208,13 +221,12 @@ RSpec.describe Loaf::ViewExtensions, '#breadcrumb_trail' do
|
|
208
221
|
view.breadcrumb_trail(&block)
|
209
222
|
|
210
223
|
expect(yielded).to eq([
|
211
|
-
['home-
|
212
|
-
['posts-
|
224
|
+
['home-sweet-home', '/', false],
|
225
|
+
['posts-for-everybody', '/posts', true]
|
213
226
|
])
|
214
227
|
end
|
215
228
|
|
216
229
|
it "allows to overwrite global configuration" do
|
217
|
-
allow(Loaf.configuration).to receive(:crumb_length).and_return(10)
|
218
230
|
view = DummyView.new
|
219
231
|
view.breadcrumb('home-sweet-home', :home_path)
|
220
232
|
view.breadcrumb('posts-for-everybody', :posts_path)
|
@@ -222,11 +234,11 @@ RSpec.describe Loaf::ViewExtensions, '#breadcrumb_trail' do
|
|
222
234
|
|
223
235
|
yielded = []
|
224
236
|
block = -> (crumb) { yielded << crumb.to_a }
|
225
|
-
view.breadcrumb_trail(
|
237
|
+
view.breadcrumb_trail(match: :exact, &block)
|
226
238
|
|
227
239
|
expect(yielded).to eq([
|
228
240
|
['home-sweet-home', '/', false],
|
229
|
-
['posts-for-
|
241
|
+
['posts-for-everybody', '/posts', true]
|
230
242
|
])
|
231
243
|
end
|
232
244
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loaf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -93,7 +93,6 @@ files:
|
|
93
93
|
- lib/loaf/configuration.rb
|
94
94
|
- lib/loaf/controller_extensions.rb
|
95
95
|
- lib/loaf/crumb.rb
|
96
|
-
- lib/loaf/crumb_formatter.rb
|
97
96
|
- lib/loaf/errors.rb
|
98
97
|
- lib/loaf/options_validator.rb
|
99
98
|
- lib/loaf/railtie.rb
|
@@ -147,7 +146,6 @@ files:
|
|
147
146
|
- spec/support/load_routes.rb
|
148
147
|
- spec/unit/configuration_spec.rb
|
149
148
|
- spec/unit/controller_extensions_spec.rb
|
150
|
-
- spec/unit/crumb_formatter_spec.rb
|
151
149
|
- spec/unit/crumb_spec.rb
|
152
150
|
- spec/unit/generators/install_generator_spec.rb
|
153
151
|
- spec/unit/options_validator_spec.rb
|
@@ -180,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
178
|
version: '0'
|
181
179
|
requirements: []
|
182
180
|
rubyforge_project:
|
183
|
-
rubygems_version: 2.
|
181
|
+
rubygems_version: 2.7.3
|
184
182
|
signing_key:
|
185
183
|
specification_version: 4
|
186
184
|
summary: Loaf manages and displays breadcrumb trails in your Rails application.
|
@@ -231,7 +229,6 @@ test_files:
|
|
231
229
|
- spec/support/load_routes.rb
|
232
230
|
- spec/unit/configuration_spec.rb
|
233
231
|
- spec/unit/controller_extensions_spec.rb
|
234
|
-
- spec/unit/crumb_formatter_spec.rb
|
235
232
|
- spec/unit/crumb_spec.rb
|
236
233
|
- spec/unit/generators/install_generator_spec.rb
|
237
234
|
- spec/unit/options_validator_spec.rb
|
data/lib/loaf/crumb_formatter.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'translation'
|
4
|
-
|
5
|
-
module Loaf
|
6
|
-
# A mixin for formatting crumb name
|
7
|
-
module CrumbFormatter
|
8
|
-
# @param [String] name
|
9
|
-
# the name to format
|
10
|
-
#
|
11
|
-
# @api public
|
12
|
-
def format_name(name, options = {})
|
13
|
-
return name if name.nil? || name.empty?
|
14
|
-
|
15
|
-
formatted = name.to_s.dup
|
16
|
-
formatted = Loaf::Translation.find_title(formatted)
|
17
|
-
formatted = formatted.capitalize if options[:capitalize]
|
18
|
-
if options[:crumb_length]
|
19
|
-
formatted = truncate(formatted, length: options[:crumb_length])
|
20
|
-
end
|
21
|
-
formatted
|
22
|
-
end
|
23
|
-
end # CrumbFormatter
|
24
|
-
end # Loaf
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
RSpec.describe Loaf::CrumbFormatter, '.format_name' do
|
4
|
-
let(:formatter) {
|
5
|
-
Class.new do
|
6
|
-
extend Loaf::CrumbFormatter
|
7
|
-
|
8
|
-
def self.truncate(name, options)
|
9
|
-
name
|
10
|
-
end
|
11
|
-
end
|
12
|
-
}
|
13
|
-
|
14
|
-
it 'returns name error if breadcrumb name is nil' do
|
15
|
-
expect(formatter.format_name('')).to eql('')
|
16
|
-
end
|
17
|
-
|
18
|
-
it "doesn't capitalize by default" do
|
19
|
-
name = 'some random name'
|
20
|
-
formatted = formatter.format_name(name)
|
21
|
-
expect(formatted).to eql(name)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'capitalizes crumb name with capitalize option' do
|
25
|
-
name = 'some random name'
|
26
|
-
formatted = formatter.format_name(name, capitalize: true)
|
27
|
-
expect(formatted).to eql('Some random name')
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'shortens crumb to provided length' do
|
31
|
-
name = 'very long name that is more that 30 characters long'
|
32
|
-
allow(formatter).to receive(:truncate).with(name, length: 30).
|
33
|
-
and_return(name[0..30])
|
34
|
-
expect(formatter.format_name(name, crumb_length: 30)).to eql(name[0..30])
|
35
|
-
end
|
36
|
-
end
|