Commit 6d28dbeb61b2ec56e765e87e14ccc1d75e7c97dd
Committed by
Rafael Reggiani Manzo
1 parent
141c0c41
Exists in
staging
and in
29 other branches
refactoring breadcrumbs
moved all `params` references to `trial` method, also `trial` method moved to a plugin helper
Showing
5 changed files
with
54 additions
and
62 deletions
Show diff stats
plugins/breadcrumbs/lib/breadcrumbs_plugin/content_breadcrumbs_block.rb
@@ -22,70 +22,8 @@ class BreadcrumbsPlugin::ContentBreadcrumbsBlock < Block | @@ -22,70 +22,8 @@ class BreadcrumbsPlugin::ContentBreadcrumbsBlock < Block | ||
22 | _('This block displays breadcrumb trail.') | 22 | _('This block displays breadcrumb trail.') |
23 | end | 23 | end |
24 | 24 | ||
25 | - def page_trail(page, params={}) | ||
26 | - links = [] | ||
27 | - if page | ||
28 | - links = page.ancestors.reverse.map { |p| { :name => p.title, :url => p.url } } | ||
29 | - links << { :name => page.title, :url => page.url } | ||
30 | - elsif params[:controller] == 'cms' | ||
31 | - id = params[:id] || params[:parent_id] | ||
32 | - links = page_trail(Article.find(id)) if id | ||
33 | - links << { :name => cms_action(params[:action]), :url => params } if show_cms_action | ||
34 | - elsif (params[:controller] == 'profile' || params[:controller] == 'events') | ||
35 | - links << { :name => _('Profile'), :url => {:controller=> 'profile', :action =>'index', :profile =>params[:profile]}} | ||
36 | - links << { :name => profile_action(params[:action]), :url => params } unless params[:action] == 'index' | ||
37 | - end | ||
38 | - links | ||
39 | - end | ||
40 | - | ||
41 | - def trail(page, profile=nil, params={}) | ||
42 | - links = page_trail(page, params) | ||
43 | - if profile && !links.empty? && show_profile | ||
44 | - [ {:name => profile.name, :url => profile.url} ] + links | ||
45 | - else | ||
46 | - links | ||
47 | - end | ||
48 | - end | ||
49 | - | ||
50 | - def content(args={}) | ||
51 | - block = self | ||
52 | - ret = (proc do | ||
53 | - trail = block.trail(@page, @profile, params) | ||
54 | - if !trail.empty? | ||
55 | - separator = content_tag('span', ' > ', :class => 'separator') | ||
56 | - | ||
57 | - breadcrumb = trail.map do |t| | ||
58 | - link_to(t[:name], t[:url], :class => 'item') | ||
59 | - end.join(separator) | ||
60 | - | ||
61 | - if block.show_section_name | ||
62 | - section_name = block.show_profile ? trail.second[:name] : trail.first[:name] | ||
63 | - breadcrumb << content_tag('div', section_name, :class => 'section-name') | ||
64 | - end | ||
65 | - | ||
66 | - breadcrumb.html_safe | ||
67 | - else | ||
68 | - '' | ||
69 | - end | ||
70 | - end) | ||
71 | - ret | ||
72 | - end | ||
73 | - | ||
74 | def cacheable? | 25 | def cacheable? |
75 | false | 26 | false |
76 | end | 27 | end |
77 | 28 | ||
78 | - protected | ||
79 | - | ||
80 | - CMS_ACTIONS = {:edit => c_('Edit'), :upload_files => _('Upload Files'), :new => c_('New')} | ||
81 | - PROFILE_ACTIONS = {:members => _('Members'), :events => _('Events')} | ||
82 | - | ||
83 | - def cms_action(action) | ||
84 | - CMS_ACTIONS[action.to_sym] || action | ||
85 | - end | ||
86 | - | ||
87 | - def profile_action(action) | ||
88 | - PROFILE_ACTIONS[action.to_sym] || action | ||
89 | - end | ||
90 | - | ||
91 | end | 29 | end |
@@ -0,0 +1,43 @@ | @@ -0,0 +1,43 @@ | ||
1 | +module BreadcrumbsPluginHelper | ||
2 | + | ||
3 | + def action(action) | ||
4 | + { :edit => c_('Edit'), | ||
5 | + :upload_files => _('Upload Files'), | ||
6 | + :new => c_('New'), | ||
7 | + :members => _('Members'), | ||
8 | + :events => _('Events') | ||
9 | + }[action.to_sym] || action | ||
10 | + end | ||
11 | + | ||
12 | + def page_trail(page) | ||
13 | + links = [] | ||
14 | + page.ancestors.reverse.each do |p| | ||
15 | + links << { :name => p.title, :url => p.url } | ||
16 | + end | ||
17 | + links << { :name => page.title, :url => page.url } | ||
18 | + links | ||
19 | + end | ||
20 | + | ||
21 | + def trail(block, page, profile=nil, params={}) | ||
22 | + links = [] | ||
23 | + if page | ||
24 | + links << page_trail(page) | ||
25 | + elsif params[:controller] == 'cms' && (id = params[:id] || params[:parent_id]) | ||
26 | + links << page_trail(Article.find(id)) | ||
27 | + if block.show_cms_action | ||
28 | + links << { :name => action(params[:action]), :url => params } | ||
29 | + end | ||
30 | + elsif (params[:controller] == 'profile' || params[:controller] == 'events') | ||
31 | + _params = {:controller=> 'profile', :action =>'index', :profile => params[:profile]} | ||
32 | + links << { :name => _('Profile'), :url => _params } | ||
33 | + unless params[:action] == 'index' | ||
34 | + links << { :name => action(params[:action]), :url => params } | ||
35 | + end | ||
36 | + end | ||
37 | + if !links.empty? && profile && block.show_profile | ||
38 | + links.unshift({:name => profile.name, :url => profile.url}) | ||
39 | + end | ||
40 | + links | ||
41 | + end | ||
42 | + | ||
43 | +end |
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +<%= link_to link[:name], link[:url], :class => 'item' %> |
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +<%= content_tag('span', ' > ', :class => 'separator') %> |
plugins/breadcrumbs/views/blocks/content_breadcrumbs.html.erb
0 → 100644
@@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
1 | +<% extend BreadcrumbsPluginHelper %> | ||
2 | + | ||
3 | +<% links = trail(block, @page, @profile, params).flatten %> | ||
4 | +<% unless links.empty? %> | ||
5 | + <%= render :partial => 'blocks/link', :collection => links, :as => 'link', :spacer_template => 'blocks/separator' %> | ||
6 | + <% if block.show_section_name %> | ||
7 | + <%= content_tag('div', (block.show_profile ? links.second[:name] : links.first[:name]), :class => 'section-name') %> | ||
8 | + <% end %> | ||
9 | +<% end %> |