Permalink
Browse files

Implemented new relationship between Snippets and PageParts. It is

going to replace the one between Snippets and Pages.

Added some scopes to find all the Pages for an Snippet and all the
Snippets for a Page.
  • Loading branch information...
1 parent a58e971 commit 2bac294844762d99a2dd27d4e33188cb5863f81b @rochgs rochgs committed Aug 18, 2011
View
@@ -1,19 +1,27 @@
class Snippet < ActiveRecord::Base
-
+
acts_as_indexed :fields => [:title, :body]
validates :title, :presence => true, :uniqueness => true
translates :body
- has_many :snippet_page, :dependent => :destroy
- has_many :pages, :through => :snippet_page
+ has_many :snippet_page_parts, :dependent => :destroy
+ has_many :page_parts, :through => :snippet_page_parts
+
+ 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}}
+ }
+ }
def self.inactive(page)
- @page = page
- snippets = scoped
- snippets = snippets.where('id NOT IN (?)', @page.snippets) unless @page.snippets.empty?
- snippets
+ @page = page
+ snippets = scoped
+ snippets = snippets.where('id NOT IN (?)', @page.snippets) unless @page.snippets.empty?
+ snippets
end
# rejects any page that has not been translated to the current locale.
@@ -22,6 +30,6 @@ def self.inactive(page)
translations = Arel::Table.new(Snippet.translations_table_name)
includes(:translations).where(
- translations[:locale].eq(Globalize.locale)).where(pages[:id].eq(translations[:snippet_id]))
+ translations[:locale].eq(Globalize.locale)).where(pages[:id].eq(translations[:snippet_id]))
}
end
@@ -0,0 +1,12 @@
+class SnippetPagePart < ActiveRecord::Base
+
+ set_table_name 'snippets_page_parts'
+
+ belongs_to :snippet, :foreign_key => :snippet_id
+ belongs_to :page_part, :foreign_key => :page_part_id
+
+ before_save do |snippet_page_part|
+ snippet_page_part.position = (SnippetPagePart.where('page_part_id = ?', snippet_page_part.page_part_id).maximum(:position) || -1) + 1
+ end
+
+end
@@ -0,0 +1,19 @@
+class CreateSnippetsPageParts < ActiveRecord::Migration
+
+ def self.up
+ create_table :snippets_page_parts do |t|
+ t.integer :snippet_id, :null => false, :references => [:snippets, :id]
+ t.integer :page_part_id, :null => false, :references => [:page_parts, :id]
+ t.integer :position, :null => false, :default => 0
+ t.boolean :before_body, :null => false, :default => false
+ end
+
+ add_index :snippets_page_parts, :snippet_id
+ add_index :snippets_page_parts, :page_part_id
+ end
+
+ def self.down
+ drop_table :snippet_page_parts
+ end
+
+end
@@ -0,0 +1,30 @@
+module Extensions
+
+ module Page
+
+ # Accessor method to get a page part from a page.
+ # Example:
+ #
+ # Page.first.content_for(:body)
+ #
+ # Will return the body page part of the first page wrap with its
+ # attached snippets.
+ def content_for(part_title)
+ part = self.parts.detect do |part|
+ part.title.present? and #protecting against the problem that occurs when have nil title
+ part.title == part_title.to_s or
+ part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_")
+ end
+
+ content = part.snippets.before.each{|snippet| snippet.try(:body)}
+ content = part.try(:body)
+ content = part.snippets.after.each{|snippet| snippet.try(:body)}
+ end
+
+ def self.included(base)
+ alias_method :content_without_snippets_for, :content_for
+ end
+
+ end
+
+end
@@ -3,19 +3,37 @@
module Refinery
module Snippets
class Engine < Rails::Engine
+
+ config.before_initialize do
+ require 'extensions/page_extensions'
+ end
+
initializer "static assets" do |app|
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public"
end
-
+
config.to_prepare do
+ PagePart.module_eval do
+ has_many :snippet_page_parts, :dependent => :destroy
+ has_many :snippets, :through => :snippet_page_parts, :order => 'position ASC'
+ end
Page.module_eval do
- has_many :snippet_page, :dependent => :destroy
- has_many :snippets, :through => :snippet_page, :order => 'position ASC'
+ # TODO: eliminar
+ # has_many :snippet_page, :dependent => :destroy
+ # has_many :snippets, :through => :snippet_page, :order => 'position ASC'
+ named_scope :for_snippet, lambda{ |snippet|
+ raise RuntimeError.new("Couldn't find Pages for a nil Snippet") if snippet.blank?
+ {
+ :joins => {:parts, :snippets},
+ :conditions => {:snippets_page_parts => {:snippet_id => snippet.id}}
+ }
+ }
end
+ Page.send :included, Extensions::Page
end
config.after_initialize do
- ::Refinery::Pages::Tab.register do |tab|
+ ::Refinery::Pages::Tab.register do |tab|
tab.name = "snippets"
tab.partial = "/admin/pages/tabs/snippets"
end
@@ -24,10 +42,10 @@ class Engine < Rails::Engine
plugin.url = {:controller => '/admin/snippets'}
plugin.menu_match = /^\/?(admin|refinery)\/snippets/
plugin.activity = [{
- :class => Snippet
- }, {
- :class => SnippetPage
- }]
+ :class => Snippet
+ }, {
+ :class => SnippetPage
+ }]
end
end
end

0 comments on commit 2bac294

Please sign in to comment.