schlick-pickle 0.1.5.1 → 0.1.5.2
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.
- data/lib/pickle/page.rb +37 -0
- metadata +2 -1
data/lib/pickle/page.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Pickle
|
2
|
+
module Page
|
3
|
+
# given args of pickle model name, and an optional extra action, or segment, will attempt to find
|
4
|
+
# a matching named route
|
5
|
+
#
|
6
|
+
# find_path_for 'the user', :action => 'edit' # => /users/3/edit
|
7
|
+
# find_path_for 'the user', 'the comment' # => /users/3/comments/1
|
8
|
+
# find_path_for 'the user', :segment => 'comments' # => /users/3/comments
|
9
|
+
#
|
10
|
+
# If you don;t know if the 'extra' part of the path is an action or a segment, then just
|
11
|
+
# pass it as 'extra' and this method will run through the possibilities
|
12
|
+
#
|
13
|
+
# find_path_for 'the user', :extra => 'new comment' # => /users/3/comments/new
|
14
|
+
def find_path_for(*pickle_names)
|
15
|
+
options = pickle_names.extract_options!
|
16
|
+
models = pickle_names.map{|m| model(m)}
|
17
|
+
if options[:extra]
|
18
|
+
path, extra = nil, options[:extra].underscore.gsub(' ','_').split("_")
|
19
|
+
(1..extra.length-1).each do |idx|
|
20
|
+
break if (path = find_path_for_models_action_segment(models, extra[0..idx-1].join("_"), extra[idx..-1].join("_")))
|
21
|
+
end
|
22
|
+
path || find_path_for_models_action_segment(models, nil, options[:extra]) || find_path_for_models_action_segment(models, options[:extra], nil)
|
23
|
+
else
|
24
|
+
find_path_for_models_action_segment(models, options[:action], options[:segment])
|
25
|
+
end or raise "Could not figure out a path for #{pickle_names.inspect} #{options.inspect}"
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
def find_path_for_models_action_segment(models, action, segment)
|
30
|
+
action.nil? || action = action.underscore.gsub(' ','_')
|
31
|
+
segment.nil? || segment = segment.underscore.gsub(' ','_')
|
32
|
+
model_names = models.map{|m| m.class.name.underscore}.join("_")
|
33
|
+
parts = [action, model_names, segment].compact
|
34
|
+
send("#{parts.join('_')}_path", *models) rescue nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schlick-pickle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.5.
|
4
|
+
version: 0.1.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian White
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- lib/pickle/adapter.rb
|
26
26
|
- lib/pickle/config.rb
|
27
27
|
- lib/pickle/injector.rb
|
28
|
+
- lib/pickle/page.rb
|
28
29
|
- lib/pickle/parser/matchers.rb
|
29
30
|
- lib/pickle/parser/with_session.rb
|
30
31
|
- lib/pickle/parser.rb
|