Skip to content

Commit 01c679c

Browse files
author
Rodrigo Garcia Suarez
committed
- Added to SnippetsPagePartsController functionality so snippets could
be added after or before PagePartBody. - Added validation to SnippetPagePart so the same snippet could be only one time after the part body and only one time before it. - Change snippet tab in page edit interface so user cand and and delete snippets after or before part body. Update englush locales file. - Fixed after and before scopes in Snippet.
1 parent 4bbebfd commit 01c679c

File tree

6 files changed

+45
-31
lines changed

6 files changed

+45
-31
lines changed

app/controllers/admin/snippets_page_parts_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ def add
55
@page = Page.find(params[:id])
66
@part = PagePart.find(params[:part_id])
77
@snippet = Snippet.find(params[:snippet_id])
8-
9-
sp = SnippetPagePart.new(:page_part => @part, :snippet => @snippet)
8+
before_body = params[:before_body] == 'true' ? true : false
9+
10+
sp = SnippetPagePart.new(:page_part => @part, :snippet => @snippet, :before_body => before_body)
1011

1112
if sp.save
1213
flash[:notice] = "Snippet #{@snippet.title} was successfully added."
@@ -19,8 +20,9 @@ def remove
1920
@page = Page.find(params[:id])
2021
@part = PagePart.find(params[:part_id])
2122
@snippet = Snippet.find(params[:snippet_id])
23+
before_body = params[:before_body] == 'true' ? true : false
2224

23-
sp = SnippetPagePart.where(:page_part_id => @page, :snippet_id => @snippet)
25+
sp = SnippetPagePart.where(:page_part_id => @part, :snippet_id => @snippet, :before_body => before_body)
2426

2527
removed = sp.first.destroy() unless sp.empty?
2628

app/models/snippet.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class Snippet < ActiveRecord::Base
1818
}
1919

2020
named_scope :before, {
21-
:joins => :page_parts,
21+
:joins => :snippet_page_parts,
2222
:conditions => {:snippets_page_parts => {:before_body => true}}
2323
}
2424

2525
named_scope :after, {
26-
:joins => :page_parts,
26+
:joins => :snippet_page_parts,
2727
:conditions => {:snippets_page_parts => {:before_body => false}}
2828
}
2929

app/models/snippet_page_part.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class SnippetPagePart < ActiveRecord::Base
44

55
belongs_to :snippet, :foreign_key => :snippet_id
66
belongs_to :page_part, :foreign_key => :page_part_id
7+
8+
validates_uniqueness_of :snippet_id, :scope => [:page_part_id, :before_body]
79

810
before_save do |snippet_page_part|
911
snippet_page_part.position = (SnippetPagePart.where('page_part_id = ?', snippet_page_part.page_part_id).maximum(:position) || -1) + 1

app/views/admin/pages/tabs/_snippets_field.html.erb

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
<div class="part-snippets">
22
<div class="active-snippets">
33
<h1><%= part.title %></h1>
4-
<% if part.snippets.any? %>
5-
<h2><%= t('.active') %></h2>
4+
<% if part.snippets.before.any? %>
5+
<h2><%= t('.before_body') %></h2>
66
<ul>
7-
<% part.snippets.each do |snippet| %>
8-
<li class='clearfix record <%= cycle('on', 'on-hover') %>' >
9-
<span class='title'>
10-
<%= snippet.title %>
11-
<% if ::Refinery::I18n.frontend_locales.many? and
12-
(locales = snippet.translations.collect{|t| t.locale}).present? %>
13-
<span class='preview'>
14-
<% locales.each do |locale| %>
15-
<%= refinery_icon_tag "flags/#{locale}.png", :size => '16x11' %>
16-
<% end %>
17-
</span>
18-
<% end %>
19-
</span>
20-
<span class='actions'>
21-
<%= link_to refinery_icon_tag('application_edit.png'), edit_admin_snippet_path(snippet),
22-
:title => t('.edit') %>
23-
<%= link_to refinery_icon_tag('delete.png'), url_for({:controller => 'snippets_page_parts', :action => 'remove', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id}),
24-
:title => t('.remove'),
25-
:class => 'remove-snippet' %>
26-
</span>
27-
</li>
28-
<% end %>
7+
<%= render :partial => '/admin/pages/tabs/snippets_list_item', :collection => part.snippets.before, :as => :snippet, :locals => {:part => part, :before_body => true} %>
8+
</ul>
9+
<% end %>
10+
<% if part.snippets.after.any? %>
11+
<h2><%= t('.after_body') %></h2>
12+
<ul>
13+
<%= render :partial => '/admin/pages/tabs/snippets_list_item', :collection => part.snippets.after, :as => :snippet, :locals => {:part => part, :before_body => false} %>
2914
</ul>
3015
<% end %>
3116
</div>
@@ -39,7 +24,8 @@
3924
<li class='clearfix record <%= cycle('on', 'on-hover') %>' >
4025
<span class="title"><%= snippet.title %></span>
4126
<span class="actions">
42-
<a class="add_icon add-snippet" href="<%= url_for({:controller => 'snippets_page_parts', :action => 'add', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id}) %>">Add</a>
27+
<a class="add_icon add-snippet" href="<%= url_for({:controller => 'snippets_page_parts', :action => 'add', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id}) %>"><%= t('.add_after_body') %></a>
28+
<a class="add_icon add-snippet" href="<%= url_for({:controller => 'snippets_page_parts', :action => 'add', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id, :before_body => 'true'}) %>"><%= t('.add_before_body') %></a>
4329
</span>
4430
</li>
4531
<% end %>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<li class='clearfix record <%= cycle('on', 'on-hover') %>' >
2+
<span class='title'>
3+
<%= snippet.title %>
4+
<% if ::Refinery::I18n.frontend_locales.many? and
5+
(locales = snippet.translations.collect{|t| t.locale}).present? %>
6+
<span class='preview'>
7+
<% locales.each do |locale| %>
8+
<%= refinery_icon_tag "flags/#{locale}.png", :size => '16x11' %>
9+
<% end %>
10+
</span>
11+
<% end %>
12+
</span>
13+
<span class='actions'>
14+
<%= link_to refinery_icon_tag('application_edit.png'), edit_admin_snippet_path(snippet),
15+
:title => t('.edit') %>
16+
<%= link_to refinery_icon_tag('delete.png'), url_for({:controller => 'snippets_page_parts', :action => 'remove', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id, :before_body => before_body}),
17+
:title => t('.remove'),
18+
:class => 'remove-snippet' %>
19+
</span>
20+
</li>

config/locales/en.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ en:
2323
snippets_field:
2424
active: Active
2525
inactive: Inactive
26+
add_after_body: Add after body
27+
add_before_body: Add before body
28+
after_body: After body
29+
before_body: Before body
30+
snippets_list_item:
2631
remove: Remove
2732
edit: Edit
28-
add: Add
2933
snippets:
3034
show:
3135
other: Other Snippets

0 commit comments

Comments
 (0)