Skip to content
Browse files

- Fixed before and after scopes (again) in Snippet and write them the

  rails 3 way,

- Added methods to Snippet to find if it is before and after in a
  part. Usedn them to hidde add buttons in snippets tab in edit page
  interface.
  • Loading branch information...
1 parent 01c679c commit 6345a946b4c0fad5b6277ea76ddab5b6a24b8341 Rodrigo Garcia Suarez committed Aug 26, 2011
Showing with 14 additions and 22 deletions.
  1. +12 −20 app/models/snippet.rb
  2. +2 −2 app/views/admin/pages/tabs/_snippets_field.html.erb
View
32 app/models/snippet.rb
@@ -11,29 +11,13 @@ class Snippet < ActiveRecord::Base
named_scope :for_page, lambda{ |page|
raise RuntimeError.new("Couldn't find Snippet for a nil Page") if page.blank?
- {
- :joins => {:page_parts, :page},
- :conditions => {:pages => {:id => page.id}}
- }
- }
-
- named_scope :before, {
- :joins => :snippet_page_parts,
- :conditions => {:snippets_page_parts => {:before_body => true}}
- }
+ joins(:page_parts => :page).where(:pages => {:id => page.id})
- named_scope :after, {
- :joins => :snippet_page_parts,
- :conditions => {:snippets_page_parts => {:before_body => false}}
}
- def self.inactive(page)
- @page = page
- snippets = scoped
- snippets = snippets.where('id NOT IN (?)', @page.snippets) unless @page.snippets.empty?
- snippets
- end
-
+ scope :before, where(:snippets_page_parts => {:before_body => true})
+ scope :after, where(:snippets_page_parts => {:before_body => false})
+
# rejects any page that has not been translated to the current locale.
scope :translated, lambda {
pages = Arel::Table.new(Snippet.table_name)
@@ -47,4 +31,12 @@ def pages
Page.for_snippet(self)
end
+ def before?(part)
+ part.snippets.before.include? self
+ end
+
+ def after?(part)
+ part.snippets.after.include? self
+ end
+
end
View
4 app/views/admin/pages/tabs/_snippets_field.html.erb
@@ -24,8 +24,8 @@
<li class='clearfix record <%= cycle('on', 'on-hover') %>' >
<span class="title"><%= snippet.title %></span>
<span class="actions">
- <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>
- <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>
+ <%= link_to t('.add_after_body'), {:controller => 'snippets_page_parts', :action => 'add', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id}, :class => 'add_icon add-snippet' unless snippet.after?(part)%>
+ <%= link_to t('.add_before_body'), {:controller => 'snippets_page_parts', :action => 'add', :id => @page.id, :snippet_id => snippet.id, :part_id => part.id, :before_body => 'true'}, :class => 'add_icon add-snippet' unless snippet.before?(part) %>
</span>
</li>
<% end %>

0 comments on commit 6345a94

Please sign in to comment.
Something went wrong with that request. Please try again.