Skip to content

Commit

Permalink
Implemented new relationship between Snippets and PageParts. It is
Browse files Browse the repository at this point in the history
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
Rodrigo Garcia Suarez committed Aug 18, 2011
1 parent a58e971 commit 2bac294
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 16 deletions.
24 changes: 16 additions & 8 deletions app/models/snippet.rb
@@ -1,19 +1,27 @@
class Snippet < ActiveRecord::Base class Snippet < ActiveRecord::Base

acts_as_indexed :fields => [:title, :body] acts_as_indexed :fields => [:title, :body]


validates :title, :presence => true, :uniqueness => true validates :title, :presence => true, :uniqueness => true


translates :body translates :body


has_many :snippet_page, :dependent => :destroy has_many :snippet_page_parts, :dependent => :destroy
has_many :pages, :through => :snippet_page 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) def self.inactive(page)
@page = page @page = page
snippets = scoped snippets = scoped
snippets = snippets.where('id NOT IN (?)', @page.snippets) unless @page.snippets.empty? snippets = snippets.where('id NOT IN (?)', @page.snippets) unless @page.snippets.empty?
snippets snippets
end end


# rejects any page that has not been translated to the current locale. # rejects any page that has not been translated to the current locale.
Expand All @@ -22,6 +30,6 @@ def self.inactive(page)
translations = Arel::Table.new(Snippet.translations_table_name) translations = Arel::Table.new(Snippet.translations_table_name)


includes(:translations).where( 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 end
12 changes: 12 additions & 0 deletions app/models/snippet_page_part.rb
@@ -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
19 changes: 19 additions & 0 deletions db/migrate/4_create_snippets_page_parts.rb
@@ -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
30 changes: 30 additions & 0 deletions lib/extensions/page_extensions.rb
@@ -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
34 changes: 26 additions & 8 deletions lib/refinerycms-snippets.rb
Expand Up @@ -3,19 +3,37 @@
module Refinery module Refinery
module Snippets module Snippets
class Engine < Rails::Engine class Engine < Rails::Engine

config.before_initialize do
require 'extensions/page_extensions'
end

initializer "static assets" do |app| initializer "static assets" do |app|
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public" app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public"
end end

config.to_prepare do 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 Page.module_eval do
has_many :snippet_page, :dependent => :destroy # TODO: eliminar
has_many :snippets, :through => :snippet_page, :order => 'position ASC' # 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 end
Page.send :included, Extensions::Page
end end


config.after_initialize do config.after_initialize do
::Refinery::Pages::Tab.register do |tab| ::Refinery::Pages::Tab.register do |tab|
tab.name = "snippets" tab.name = "snippets"
tab.partial = "/admin/pages/tabs/snippets" tab.partial = "/admin/pages/tabs/snippets"
end end
Expand All @@ -24,10 +42,10 @@ class Engine < Rails::Engine
plugin.url = {:controller => '/admin/snippets'} plugin.url = {:controller => '/admin/snippets'}
plugin.menu_match = /^\/?(admin|refinery)\/snippets/ plugin.menu_match = /^\/?(admin|refinery)\/snippets/
plugin.activity = [{ plugin.activity = [{
:class => Snippet :class => Snippet
}, { }, {
:class => SnippetPage :class => SnippetPage
}] }]
end end
end end
end end
Expand Down

0 comments on commit 2bac294

Please sign in to comment.