gretel-trails 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e37b779cdb1997c44fbffe04b0e99cdcaeffa9ac
4
- data.tar.gz: d90ed701b34331eae74e4d60ea6f3c465b3841f6
3
+ metadata.gz: 5ecb5003348bd54d39ff554ef56de1f38c82f0bf
4
+ data.tar.gz: 4c2cde48545343930bb454cda4d2e2f63cb330e9
5
5
  SHA512:
6
- metadata.gz: c5351e3714d5dbaf54f423248c1919876e129b02fab2f2260097b63962f43def0598b485a11ca18596bc0c526387efce9813dbc1f8f07d233de0f6518eb9a0d7
7
- data.tar.gz: 0f29f72ea2058d12e67a32408fffe33e40d2040f614968d88c12a0f38ec3eecf86b1c3585860b633e783c0687dff2f03e299d0e590d898b8dd096afd2cfad620
6
+ metadata.gz: 70a4faff241e9417d5791048af7a7972a33f3c3f2e101e5756ca8426f6e5deade6796aec721962c0c15bf6ce3f16b384a44244f278fca391da44548a0221f9c6
7
+ data.tar.gz: 38cf86aeef2f3e9cb402c4697dfac4815d825215b778460a52c396a641b94dc60f2820418767104e51cb043d59421a8ce1aceea25d74b851e610b397002bf433
data/README.md CHANGED
@@ -29,7 +29,9 @@ The default store is the URL store that encodes trails directly in the URL. Note
29
29
  In order to use the URL store, you must set a secret that's used to prevent cross-site scripting attacks. In an initializer, e.g. *config/initializers/gretel.rb*:
30
30
 
31
31
  ```
32
- Gretel::Trails::UrlStore.secret = 'your_key_here' # Must be changed to something else to be secure
32
+ Gretel::Trails.configure do |config|
33
+ config.store.secret = 'your_key_here' # Must be changed to something else to be secure
34
+ end
33
35
  ```
34
36
 
35
37
  You can generate a secret using `SecureRandom.hex(64)` or `rake secret`.
@@ -51,7 +53,9 @@ The product view will now have the breadcrumb trail from the first page (reviews
51
53
  The default trail param is `params[:trail]`. You can change it in an initializer:
52
54
 
53
55
  ```ruby
54
- Gretel::Trails.trail_param = :other_param
56
+ Gretel::Trails.configure do |config|
57
+ config.trail_param = :other_param
58
+ end
55
59
  ```
56
60
 
57
61
  ## Hiding trails in URLs
@@ -61,7 +65,9 @@ Gretel::Trails has a `:hidden` strategy that can be used to hide trails in URLs
61
65
  To hide trails, you set the strategy in an initializer, e.g. *config/initializers/gretel.rb*:
62
66
 
63
67
  ```ruby
64
- Gretel::Trails.strategy = :hidden
68
+ Gretel::Trails.configure do |config|
69
+ config.strategy = :hidden
70
+ end
65
71
  ```
66
72
 
67
73
  Add a data attribute with the trail to your `<body>` tag, in *application.html.erb*:
@@ -117,24 +123,30 @@ If you want to do custom breadcrumb links with these changes applied, you can us
117
123
 
118
124
  The link will now have a URL without the trail param and `data-trail` containing the trail.
119
125
 
120
- ## Customization
126
+ ### Customization
121
127
 
122
- ### JS selector
128
+ #### JS selector
123
129
 
124
130
  If you want to customize the JS selector (the default is `.js-append-trail`), you can do so in an initializer:
125
131
 
126
132
  ```ruby
127
- Gretel::Trails::HiddenStrategy.js_selector = ".my-other-selector"
133
+ Gretel::Trails.configure do |config|
134
+ config.strategy = :hidden
135
+ config.hidden.js_selector = ".my-other-selector"
136
+ end
128
137
  ```
129
138
 
130
139
  It supports all [CSS selectors](http://api.jquery.com/category/selectors/) that you can use in jQuery.
131
140
 
132
- ### Data attribute
141
+ #### Data attribute
133
142
 
134
143
  The default trail data attribute for `<body>` and links is `data-trail` but you can change this in an initializer:
135
144
 
136
145
  ```ruby
137
- Gretel::Trails::HiddenStrategy.data_attribute = "other-data-attribute"
146
+ Gretel::Trails.configure do |config|
147
+ config.strategy = :hidden
148
+ config.hidden.data_attribute = "other-data-attribute"
149
+ end
138
150
  ```
139
151
 
140
152
  `data-` is added automatically, so if for example you want the attribute to be `data-my-attr`, you just set it to `my-attr`.
@@ -150,8 +162,10 @@ The default store is the URL store which is great for simple use, but if you hav
150
162
  To use the URL store, set it in an initializer, e.g. *config/initializers/gretel.rb*:
151
163
 
152
164
  ```ruby
153
- Gretel::Trails.store = :url # Not really needed as this is the default
154
- Gretel::Trails::UrlStore.secret = 'your_key_here' # Must be changed to something else to be secure
165
+ Gretel::Trails.configure do |config|
166
+ config.store = :url # Not really needed as this is the default
167
+ config.store.secret = 'your_key_here' # Must be changed to something else to be secure
168
+ end
155
169
  ```
156
170
 
157
171
  The secret is used to prevent cross-site scripting attacks. You can generate a secure one using `SecureRandom.hex(64)` or `rake secret`.
@@ -163,7 +177,9 @@ The database store stores trails in the database so the trail keys have a maximu
163
177
  To use the database store, set it an initializer, e.g. *config/initializers/gretel.rb*:
164
178
 
165
179
  ```ruby
166
- Gretel::Trails.store = :db
180
+ Gretel::Trails.configure do |config|
181
+ config.store = :db
182
+ end
167
183
  ```
168
184
 
169
185
  You also need to create a migration for the database table that holds the trails:
@@ -187,7 +203,10 @@ If you need a gem for managing recurring tasks, [Whenever](https://github.com/ja
187
203
  The default expiration period is 1 day. To set a custom expiration period, in an initializer:
188
204
 
189
205
  ```ruby
190
- Gretel::Trails::ActiveRecordStore.expires_in = 2.days
206
+ Gretel::Trails.configure do |config|
207
+ config.store = :db
208
+ config.store.expires_in = 2.days
209
+ end
191
210
  ```
192
211
 
193
212
  ### Redis store
@@ -197,8 +216,10 @@ If you want to store trails in [Redis](https://github.com/redis/redis), you can
197
216
  To use the Redis store, set it in an initializer, e.g. *config/initializers/gretel.rb*:
198
217
 
199
218
  ```ruby
200
- Gretel::Trails.store = :redis
201
- Gretel::Trails::RedisStore.connect_options = { host: "10.0.1.1", port: 6380 }
219
+ Gretel::Trails.configure do |config|
220
+ config.store = :redis
221
+ config.store.connect_options = { host: "10.0.1.1", port: 6380 }
222
+ end
202
223
  ```
203
224
 
204
225
  Trails are now stored in Redis and expired automatically after 1 day (by default).
@@ -206,7 +227,10 @@ Trails are now stored in Redis and expired automatically after 1 day (by default
206
227
  To set a custom expiration period, in an initializer:
207
228
 
208
229
  ```ruby
209
- Gretel::Trails::RedisStore.expires_in = 2.days
230
+ Gretel::Trails.configure do |config|
231
+ config.store = :redis
232
+ config.store.expires_in = 2.days
233
+ end
210
234
  ```
211
235
 
212
236
  ## Requirements
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^test/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "gretel", ">= 3.0.0.beta5"
20
+ spec.add_dependency "gretel", ">= 3.0.0.beta6"
21
21
  spec.add_dependency "rails", ">= 3.2.0"
22
22
  spec.add_development_dependency "rails", "~> 3.2.13"
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
data/lib/gretel/trails.rb CHANGED
@@ -7,13 +7,15 @@ require "gretel/trails/engine"
7
7
 
8
8
  module Gretel
9
9
  module Trails
10
- STORES = {
10
+ DEFAULT_STORES = {
11
11
  url: UrlStore,
12
12
  db: ActiveRecordStore,
13
13
  redis: RedisStore
14
14
  }
15
15
 
16
16
  class << self
17
+ include Gretel::Resettable
18
+
17
19
  # Activated strategies
18
20
  def strategies
19
21
  @strategies ||= []
@@ -40,14 +42,26 @@ module Gretel
40
42
  # Can be a subclass of +Gretel::Trails::Store+, or a symbol: +:url+, +:db+, or +:redis+.
41
43
  def store=(value)
42
44
  if value.is_a?(Symbol)
43
- klass = STORES[value]
44
- raise ArgumentError, "Unknown Gretel::Trails.store #{value.inspect}. Use any of #{STORES.inspect}." unless klass
45
+ klass = available_stores[value]
46
+ raise ArgumentError, "Unknown Gretel::Trails.store #{value.inspect}. Use any of #{available_stores.inspect}." unless klass
45
47
  self.store = klass
46
48
  else
47
49
  @store = value
48
50
  end
49
51
  end
50
52
 
53
+ # Registers a store for use with `Gretel::Trails.store = :key`.
54
+ #
55
+ # Gretel::Trails.register_store :url, UrlStore
56
+ def register_store(key, klass)
57
+ available_stores[key] = klass
58
+ end
59
+
60
+ # Hash of registered stores.
61
+ def available_stores
62
+ @available_stores ||= DEFAULT_STORES
63
+ end
64
+
51
65
  # Uses the store to encode an array of links to a unique key that can be used in URLs.
52
66
  def encode(links)
53
67
  store.encode(links)
@@ -93,12 +107,6 @@ module Gretel
93
107
  def configure
94
108
  yield self
95
109
  end
96
-
97
- # Resets all changes made to +Gretel::Trail+. Used for testing.
98
- def reset!
99
- instance_variables.each { |var| remove_instance_variable var }
100
- STORES.each_value(&:reset!)
101
- end
102
110
  end
103
111
  end
104
112
  end
@@ -12,6 +12,20 @@ Gretel::Renderer.class_eval do
12
12
 
13
13
  # Returns encoded trail for the breadcrumb.
14
14
  def trail
15
- @trail ||= Gretel::Trails.encode(links)
15
+ @trail ||= begin
16
+ transformed_links = links.dup
17
+ if transform_current_path && transformed_links.any? && request
18
+ transformed_links.last.url = request.fullpath
19
+ end
20
+ Gretel::Trails.encode(transformed_links)
21
+ end
22
+ end
23
+
24
+ # Whether to set the current link path to +request.fullpath+.
25
+ def transform_current_path
26
+ return @transform_current_path if defined?(@transform_current_path)
27
+ @transform_current_path = true
16
28
  end
29
+
30
+ attr_writer :transform_current_path
17
31
  end
@@ -3,4 +3,12 @@ Gretel::ViewHelpers.class_eval do
3
3
  def breadcrumb_trail
4
4
  gretel_renderer.trail
5
5
  end
6
+
7
+ # Yields a block having the breadcrumb trail set to the breadcrumb given by +key+.
8
+ def with_breadcrumb_trail(key, *args, &block)
9
+ with_breadcrumb(key, *args) do
10
+ gretel_renderer.transform_current_path = false
11
+ yield
12
+ end
13
+ end
6
14
  end
@@ -2,6 +2,8 @@ module Gretel
2
2
  module Trails
3
3
  class Store
4
4
  class << self
5
+ include Gretel::Resettable
6
+
5
7
  # Save an encoded array to the store. It must return the trail key that
6
8
  # can later be used to retrieve the array from the store.
7
9
  def save(array)
@@ -43,11 +45,6 @@ module Gretel
43
45
  []
44
46
  end
45
47
  end
46
-
47
- # Resets all changes made to the store. Used for testing.
48
- def reset!
49
- instance_variables.each { |var| remove_instance_variable var }
50
- end
51
48
  end
52
49
  end
53
50
  end
@@ -5,6 +5,8 @@ module Gretel
5
5
  DEFAULT_DATA_ATTRIBUTE = "trail"
6
6
 
7
7
  class << self
8
+ include Gretel::Resettable
9
+
8
10
  # jQuery selector for links that should have the trail appended
9
11
  # to them on click. Default: +.js-append-trail+
10
12
  def js_selector
@@ -27,6 +29,21 @@ module Gretel
27
29
  end
28
30
  end
29
31
 
32
+ Gretel::Trails.class_eval do
33
+ class << self
34
+ def hidden
35
+ Gretel::Trails::HiddenStrategy
36
+ end
37
+
38
+ def reset_with_hidden!
39
+ reset_without_hidden!
40
+ hidden.reset!
41
+ end
42
+
43
+ alias_method_chain :reset!, :hidden
44
+ end
45
+ end
46
+
30
47
  Gretel::Renderer.class_eval do
31
48
  # Moves the trail from the querystring into a data attribute.
32
49
  def breadcrumb_link_to_with_hidden_trail(name, url, options = {})
@@ -1,5 +1,5 @@
1
1
  module Gretel
2
2
  module Trails
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,7 @@
1
+ class HomeController < ApplicationController
2
+ def index
3
+ end
4
+
5
+ def about
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ <% breadcrumb :about %>
2
+
3
+ <h1>About</h1>
4
+
5
+ <%= link_to "Recent products before different trail", recent_products_path, data: { trail: breadcrumb_trail } %>
6
+
7
+ <% with_breadcrumb_trail :product, Product.find_by_slug("one") do %>
8
+ <%= link_to "Recent products with different trail", recent_products_path, data: { trail: breadcrumb_trail } %>
9
+ <% end %>
10
+
11
+ <%= link_to "Recent products after different trail", recent_products_path, data: { trail: breadcrumb_trail } %>
@@ -0,0 +1 @@
1
+ <h1>Welcome!</h1>
@@ -1,6 +1,22 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class GretelTrailsTest < ActionDispatch::IntegrationTest
4
+ setup do
5
+ Gretel.reset!
6
+ Gretel::Trails.configure do |config|
7
+ config.store = :db
8
+ config.strategy = :hidden
9
+ end
10
+ end
11
+
12
+ test "configuration block" do
13
+ Gretel::Trails.configure do |config|
14
+ config.hidden.js_selector = ".test"
15
+ end
16
+
17
+ assert_equal ".test", Gretel::Trails::HiddenStrategy.js_selector
18
+ end
19
+
4
20
  test "regular breadcrumbs" do
5
21
  visit "/categories/one"
6
22
 
@@ -23,24 +39,24 @@ class GretelTrailsTest < ActionDispatch::IntegrationTest
23
39
  end
24
40
 
25
41
  test "invisibly applying trail" do
26
- visit "/products/recent"
42
+ visit "/products/recent?page=2"
27
43
 
28
44
  within "#products" do
29
45
  click_link "Product One"
30
46
  end
31
47
  assert_equal "/products/one", current_fullpath
32
- assert_equal ["/", "/products/recent"], all(".breadcrumbs a").map { |a| a[:href] }
48
+ assert_equal ["/", "/products/recent?page=2"], all(".breadcrumbs a").map { |a| a[:href] }
33
49
 
34
50
  click_link "See reviews"
35
51
  assert_equal "/products/one/reviews", current_fullpath
36
- assert_equal ["/", "/products/recent", "/products/one"], all(".breadcrumbs a").map { |a| a[:href] }
52
+ assert_equal ["/", "/products/recent?page=2", "/products/one"], all(".breadcrumbs a").map { |a| a[:href] }
37
53
 
38
54
  all(".breadcrumbs a").last.click
39
55
  assert_equal "/products/one", current_fullpath
40
- assert_equal ["/", "/products/recent"], all(".breadcrumbs a").map { |a| a[:href] }
56
+ assert_equal ["/", "/products/recent?page=2"], all(".breadcrumbs a").map { |a| a[:href] }
41
57
 
42
58
  all(".breadcrumbs a").last.click
43
- assert_equal "/products/recent", current_fullpath
59
+ assert_equal "/products/recent?page=2", current_fullpath
44
60
  end
45
61
 
46
62
  test "breadcrumb_link_to" do
data/test/trails_test.rb CHANGED
@@ -11,10 +11,13 @@ class TrailsTest < ActiveSupport::TestCase
11
11
 
12
12
  test "configuration block" do
13
13
  Gretel::Trails.configure do |config|
14
+ config.store = :url
15
+ config.store.secret = "1234"
14
16
  config.trail_param = :set_from_config
15
17
  end
16
18
 
17
19
  assert_equal :set_from_config, Gretel::Trails.trail_param
20
+ assert_equal "1234", Gretel::Trails::UrlStore.secret
18
21
  end
19
22
 
20
23
  test "setting invalid store" do
@@ -22,4 +25,13 @@ class TrailsTest < ActiveSupport::TestCase
22
25
  Gretel::Trails.store = :xx
23
26
  end
24
27
  end
28
+
29
+ test "register store" do
30
+ klass = Class.new(Gretel::Trails::Store)
31
+
32
+ Gretel::Trails.register_store :test_store, klass
33
+ Gretel::Trails.store = :test_store
34
+
35
+ assert_equal klass, Gretel::Trails.store
36
+ end
25
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gretel-trails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lasse Bunk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-21 00:00:00.000000000 Z
11
+ date: 2013-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gretel
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0.beta5
19
+ version: 3.0.0.beta6
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0.beta5
26
+ version: 3.0.0.beta6
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +192,7 @@ files:
192
192
  - test/dummy/app/assets/stylesheets/application.css
193
193
  - test/dummy/app/controllers/application_controller.rb
194
194
  - test/dummy/app/controllers/categories_controller.rb
195
+ - test/dummy/app/controllers/home_controller.rb
195
196
  - test/dummy/app/controllers/products_controller.rb
196
197
  - test/dummy/app/controllers/reviews_controller.rb
197
198
  - test/dummy/app/helpers/application_helper.rb
@@ -200,6 +201,8 @@ files:
200
201
  - test/dummy/app/models/category.rb
201
202
  - test/dummy/app/models/product.rb
202
203
  - test/dummy/app/views/categories/show.html.erb
204
+ - test/dummy/app/views/home/about.html.erb
205
+ - test/dummy/app/views/home/index.html.erb
203
206
  - test/dummy/app/views/layouts/application.html.erb
204
207
  - test/dummy/app/views/products/recent.html.erb
205
208
  - test/dummy/app/views/products/show.html.erb
@@ -214,7 +217,6 @@ files:
214
217
  - test/dummy/config/environments/production.rb
215
218
  - test/dummy/config/environments/test.rb
216
219
  - test/dummy/config/initializers/backtrace_silencers.rb
217
- - test/dummy/config/initializers/gretel.rb
218
220
  - test/dummy/config/initializers/inflections.rb
219
221
  - test/dummy/config/initializers/mime_types.rb
220
222
  - test/dummy/config/initializers/secret_token.rb
@@ -235,10 +237,10 @@ files:
235
237
  - test/dummy/script/rails
236
238
  - test/dummy/test/fixtures/categories.yml
237
239
  - test/dummy/test/fixtures/products.yml
238
- - test/gretel_trails_test.rb
239
240
  - test/stores/active_record_store_test.rb
240
241
  - test/stores/redis_store_test.rb
241
242
  - test/stores/url_store_test.rb
243
+ - test/strategies/hidden_strategy_test.rb
242
244
  - test/test_helper.rb
243
245
  - test/trails_test.rb
244
246
  - test/view_helpers_test.rb
@@ -262,7 +264,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
264
  version: '0'
263
265
  requirements: []
264
266
  rubyforge_project:
265
- rubygems_version: 2.0.3
267
+ rubygems_version: 2.1.10
266
268
  signing_key:
267
269
  specification_version: 4
268
270
  summary: Collection of strategies for handling Gretel trails.
@@ -273,6 +275,7 @@ test_files:
273
275
  - test/dummy/app/assets/stylesheets/application.css
274
276
  - test/dummy/app/controllers/application_controller.rb
275
277
  - test/dummy/app/controllers/categories_controller.rb
278
+ - test/dummy/app/controllers/home_controller.rb
276
279
  - test/dummy/app/controllers/products_controller.rb
277
280
  - test/dummy/app/controllers/reviews_controller.rb
278
281
  - test/dummy/app/helpers/application_helper.rb
@@ -281,6 +284,8 @@ test_files:
281
284
  - test/dummy/app/models/category.rb
282
285
  - test/dummy/app/models/product.rb
283
286
  - test/dummy/app/views/categories/show.html.erb
287
+ - test/dummy/app/views/home/about.html.erb
288
+ - test/dummy/app/views/home/index.html.erb
284
289
  - test/dummy/app/views/layouts/application.html.erb
285
290
  - test/dummy/app/views/products/recent.html.erb
286
291
  - test/dummy/app/views/products/show.html.erb
@@ -295,7 +300,6 @@ test_files:
295
300
  - test/dummy/config/environments/production.rb
296
301
  - test/dummy/config/environments/test.rb
297
302
  - test/dummy/config/initializers/backtrace_silencers.rb
298
- - test/dummy/config/initializers/gretel.rb
299
303
  - test/dummy/config/initializers/inflections.rb
300
304
  - test/dummy/config/initializers/mime_types.rb
301
305
  - test/dummy/config/initializers/secret_token.rb
@@ -316,10 +320,10 @@ test_files:
316
320
  - test/dummy/script/rails
317
321
  - test/dummy/test/fixtures/categories.yml
318
322
  - test/dummy/test/fixtures/products.yml
319
- - test/gretel_trails_test.rb
320
323
  - test/stores/active_record_store_test.rb
321
324
  - test/stores/redis_store_test.rb
322
325
  - test/stores/url_store_test.rb
326
+ - test/strategies/hidden_strategy_test.rb
323
327
  - test/test_helper.rb
324
328
  - test/trails_test.rb
325
329
  - test/view_helpers_test.rb
@@ -1,2 +0,0 @@
1
- Gretel::Trails.store = :db
2
- Gretel::Trails.strategy = :hidden