rails_nav 1.3.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rails_nav.rb +89 -19
- data/rails_nav.gemspec +1 -1
- metadata +2 -2
data/lib/rails_nav.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
##
|
7
7
|
#
|
8
8
|
def Nav.version()
|
9
|
-
'
|
9
|
+
'2.0.0'
|
10
10
|
end
|
11
11
|
|
12
12
|
def Nav.dependencies
|
@@ -54,7 +54,7 @@
|
|
54
54
|
|
55
55
|
# for use when the controller instance is available *now*
|
56
56
|
#
|
57
|
-
# Nav.
|
57
|
+
# Nav.build do |nav|
|
58
58
|
#
|
59
59
|
# nav.link 'Home', root_path
|
60
60
|
#
|
@@ -80,6 +80,8 @@
|
|
80
80
|
attr_accessor(:block)
|
81
81
|
attr_accessor(:controller)
|
82
82
|
attr_accessor(:strategy)
|
83
|
+
attr_accessor(:weights)
|
84
|
+
attr_accessor(:active)
|
83
85
|
|
84
86
|
def initialize(name = :main, &block)
|
85
87
|
@name = name.to_s
|
@@ -87,6 +89,7 @@
|
|
87
89
|
@already_computed_active = false
|
88
90
|
@controller = nil
|
89
91
|
@strategy = :call
|
92
|
+
@active = nil
|
90
93
|
end
|
91
94
|
|
92
95
|
def evaluate(block, *args)
|
@@ -113,24 +116,45 @@
|
|
113
116
|
link.controller = @controller
|
114
117
|
active = link.compute_active!
|
115
118
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
119
|
+
weight =
|
120
|
+
begin
|
121
|
+
case active
|
122
|
+
when nil, false
|
123
|
+
-1
|
124
|
+
when true
|
125
|
+
0
|
126
|
+
else
|
127
|
+
Integer(active)
|
128
|
+
end
|
129
|
+
rescue
|
130
|
+
-1
|
124
131
|
end
|
132
|
+
|
133
|
+
link.weight = weight
|
134
|
+
weights[index] = weight
|
125
135
|
end
|
126
136
|
|
137
|
+
self.weights = weights
|
138
|
+
|
127
139
|
each_with_index do |link, index|
|
128
140
|
link.active = false
|
129
141
|
end
|
130
142
|
|
131
|
-
|
143
|
+
no_clear_winner =
|
144
|
+
weights.uniq.size < 2
|
145
|
+
|
146
|
+
active_link =
|
147
|
+
if no_clear_winner
|
148
|
+
detect{|link| link.default}
|
149
|
+
else
|
150
|
+
most_active_link_index = weights.index(weights.max)
|
151
|
+
self[most_active_link_index]
|
152
|
+
end
|
132
153
|
|
133
|
-
active_link
|
154
|
+
if active_link
|
155
|
+
active_link.active = true
|
156
|
+
self.active = active_link
|
157
|
+
end
|
134
158
|
|
135
159
|
@already_computed_active = true
|
136
160
|
end
|
@@ -203,28 +227,35 @@
|
|
203
227
|
#
|
204
228
|
class Link
|
205
229
|
attr_accessor(:nav)
|
230
|
+
attr_accessor(:args)
|
231
|
+
attr_accessor(:options)
|
206
232
|
attr_accessor(:controller)
|
207
233
|
attr_accessor(:content)
|
208
234
|
attr_accessor(:options)
|
209
235
|
attr_accessor(:pattern)
|
210
236
|
attr_accessor(:active)
|
211
237
|
attr_accessor(:compute_active)
|
238
|
+
attr_accessor(:default)
|
239
|
+
attr_accessor(:weight)
|
240
|
+
attr_accessor(:slug)
|
212
241
|
|
213
242
|
def initialize(nav, *args, &block)
|
214
243
|
@nav = nav
|
215
244
|
|
216
245
|
options =
|
217
|
-
if args.
|
246
|
+
if args.last.is_a?(Hash)
|
218
247
|
args.extract_options!.to_options!
|
219
248
|
else
|
220
249
|
{}
|
221
250
|
end
|
222
251
|
|
223
|
-
@content = options[:content]
|
224
|
-
@
|
225
|
-
@pattern = options[:pattern]
|
226
|
-
@compute_active = options[:active]
|
252
|
+
@content = options[:content] || args.shift || 'Slash'
|
253
|
+
@url = options[:url] || args.shift || {}
|
254
|
+
@pattern = options[:pattern] || args.shift || Link.default_active_pattern_for(@content)
|
255
|
+
@compute_active = options[:active] || block || Link.default_active_block_for(@pattern)
|
256
|
+
@default = options[:default]
|
227
257
|
|
258
|
+
@slug = Slug.for(@content, :join => '-')
|
228
259
|
@already_computed_active = nil
|
229
260
|
@active = nil
|
230
261
|
end
|
@@ -244,6 +275,10 @@
|
|
244
275
|
!!@active
|
245
276
|
end
|
246
277
|
|
278
|
+
def default?
|
279
|
+
!!@default
|
280
|
+
end
|
281
|
+
|
247
282
|
%w( controller request fullpath path_info ).each do |method|
|
248
283
|
class_eval <<-__, __FILE__, __LINE__
|
249
284
|
def #{ method }(*args, &block)
|
@@ -253,7 +288,7 @@
|
|
253
288
|
end
|
254
289
|
|
255
290
|
def url
|
256
|
-
controller.send(:url_for,
|
291
|
+
controller.send(:url_for, *@url)
|
257
292
|
end
|
258
293
|
alias_method(:href, :url)
|
259
294
|
|
@@ -261,8 +296,13 @@
|
|
261
296
|
content.to_s
|
262
297
|
end
|
263
298
|
|
299
|
+
def inspect
|
300
|
+
super
|
301
|
+
end
|
302
|
+
|
264
303
|
def Link.default_active_pattern_for(content)
|
265
|
-
|
304
|
+
path_info = Slug.for(content, :join => '_')
|
305
|
+
%r/\b#{ path_info }\b/i
|
266
306
|
end
|
267
307
|
|
268
308
|
def Link.default_active_block_for(pattern)
|
@@ -275,6 +315,36 @@
|
|
275
315
|
end
|
276
316
|
end
|
277
317
|
end
|
318
|
+
|
319
|
+
class Slug < ::String
|
320
|
+
Join = '-'
|
321
|
+
|
322
|
+
def Slug.for(*args)
|
323
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
324
|
+
join = (options[:join]||options['join']||Join).to_s
|
325
|
+
string = args.flatten.compact.join(join)
|
326
|
+
string = unidecode(string).titleize
|
327
|
+
words = string.to_s.scan(%r/\w+/)
|
328
|
+
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
|
329
|
+
words.delete_if{|word| word.nil? or word.strip.empty?}
|
330
|
+
new(words.join(join).downcase)
|
331
|
+
end
|
332
|
+
|
333
|
+
begin
|
334
|
+
require 'stringx'
|
335
|
+
rescue LoadError
|
336
|
+
end
|
337
|
+
|
338
|
+
unless defined?(Stringex::Unidecoder)
|
339
|
+
def Slug.unidecode(string)
|
340
|
+
string
|
341
|
+
end
|
342
|
+
else
|
343
|
+
def Slug.unidecode(string)
|
344
|
+
Stringex::Unidecoder.decode(string)
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
278
348
|
end
|
279
349
|
|
280
350
|
# factored out mixin for controllers/views
|
data/rails_nav.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_nav
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails_current
|