Skip to content

Commit

Permalink
Import from old repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
seancribbs committed Jun 20, 2008
0 parents commit 9021a56
Show file tree
Hide file tree
Showing 17 changed files with 578 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
= Share Layouts

Created by: Sean Cribbs (seancribbs AT gmail DOT com), September 20, 2007

Allows Rails controllers/actions to use Radiant layouts as their "layout".
content_for blocks are mapped to page parts, with the exception of :title and
:breadcrumbs, which map to their specific default tags. The default content,
or @content_for_layout, is mapped to the 'body' part.

== What to do in your controllers

radiant_layout 'Layout name'

-or-

radiant_layout { |controller| # some code to determine layout name }

radiant_layout takes the same options as the built-in layout. To specifically
override the Radiant layout and use a standard Rails one use
:layout => "mine", or :layout => false for no layout, as options to render.

To choose a different Radiant layout, set the @radiant_layout instance
variable to the name of a Radiant layout in your controller or view.

== Acknowledgments

Thanks to John Long for clarifying and simplifying the process for me!
Thanks to xtoddx for improving the tests and support for tags that use the
request and response.
Thanks to Digital Pulp, Inc. for funding the initial development of this
extension as part of the Redken.com project.
25 changes: 25 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the share_layouts extension.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the share_layouts extension.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ShareLayoutsExtension'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

# Load any custom rakefiles for extension
Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
38 changes: 38 additions & 0 deletions app/models/rails_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class RailsPage < Page
display_name "Application"
attr_accessor :breadcrumbs

def find_by_url(url, live=true, clean=true)
url = clean_url(url) if clean
self if url.starts_with?(self.url)
end

def request_uri=(value)
@url = URI.parse(value).path
self.slug = @url.split("/").last
end

def url
@url || super
end

def breadcrumb
title
end

def build_parts_from_hash!(content)
parts.clear
content.each do |k,v|
parts.build(:name => k.to_s, :content => v)
end
end

alias_method "tag:old_breadcrumbs", "tag:breadcrumbs"
tag 'breadcrumbs' do |tag|
if tag.locals.page.is_a?(RailsPage) && tag.locals.page.breadcrumbs
tag.locals.page.breadcrumbs
else
render_tag('old_breadcrumbs', tag)
end
end
end
1 change: 1 addition & 0 deletions app/views/layouts/radiant.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= radiant_layout %>
2 changes: 2 additions & 0 deletions lib/share_layouts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ShareLayouts
end
38 changes: 38 additions & 0 deletions lib/share_layouts/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module ShareLayouts::Helper

def radiant_layout(name = @radiant_layout)
returning String.new do |output|
page = find_page
assign_attributes!(page, name)
page.build_parts_from_hash!(extract_captures)
output << page.render
end
end

def assign_attributes!(page, name = @radiant_layout)
page.layout = Layout.find_by_name(name)
page.title = @title || @content_for_title || page.title || ''
page.breadcrumbs = @breadcrumbs || @content_for_breadcrumbs || page.breadcrumbs || ''
page.request_uri = request.request_uri
page.request = request
page.response = response
end

def extract_captures
variables = instance_variables.grep(/@content_for_/)
variables.inject({}) do |h, var|
var =~ /^@content_for_(.*)$/
key = $1.intern
key = :body if key == :layout
unless key == :title || key == :breadcrumbs
h[key] = instance_variable_get(var)
end
h
end
end

def find_page
page = Page.find_by_url(request.request_uri) rescue nil
page.is_a?(RailsPage) ? page : RailsPage.new(:class_name => "RailsPage")
end
end
19 changes: 19 additions & 0 deletions lib/share_layouts/radiant_layouts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ShareLayouts::RadiantLayouts
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def radiant_layout(name=nil, options={}, &block)
raise ArgumentError, "A layout name or block is required!" unless name || block
write_inheritable_attribute 'radiant_layout', name || block
before_filter :set_radiant_layout
layout 'radiant', options
end
end

def set_radiant_layout
@radiant_layout = self.class.read_inheritable_attribute 'radiant_layout'
@radiant_layout = @radiant_layout.call(self) if @radiant_layout.is_a? Proc
end
end
31 changes: 31 additions & 0 deletions lib/share_layouts/rails_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class ShareLayouts::RailsPage < Page
attr_accessor :breadcrumbs

def request_uri=(value)
@url = URI.parse(value).path
self.slug = @url.split("/").last
end

def url
@url
end

def breadcrumb
title
end

def build_parts_from_hash!(content)
content.each do |k,v|
parts << PagePart.new(:name => k.to_s, :content => v)
end
end

alias_method "tag:old_breadcrumbs", "tag:breadcrumbs"
tag 'breadcrumbs' do |tag|
if tag.locals.page.is_a? ShareLayouts::RailsPage
tag.locals.page.breadcrumbs
else
render_tag('old_breadcrumbs', tag)
end
end
end
17 changes: 17 additions & 0 deletions lib/tasks/share_layouts_extension_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace :radiant do
namespace :extensions do
namespace :share_layouts do

desc "Runs the migration of the Share Layouts extension"
task :migrate => :environment do
require 'radiant/extension_migrator'
if ENV["VERSION"]
ShareLayoutsExtension.migrator.migrate(ENV["VERSION"].to_i)
else
ShareLayoutsExtension.migrator.migrate
end
end

end
end
end
18 changes: 18 additions & 0 deletions share_layouts_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Uncomment this if you reference any of your controllers in activate
require_dependency 'application'

class ShareLayoutsExtension < Radiant::Extension
version "0.3"
description "Allows Radiant layouts to be used as layouts for standard Rails actions."
url "http://wiki.radiantcms.org/Thirdparty_Extensions"

def activate
RailsPage
ActionController::Base.send :include, ShareLayouts::RadiantLayouts
ActionView::Base.send :include, ShareLayouts::Helper
end

def deactivate
end

end
24 changes: 24 additions & 0 deletions test/fixtures/pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
homepage:
id: 1
title: Homepage
breadcrumb: Homepage
slug: /
status_id: 100
published_at: 2008-01-01 08:00:00
rails_page:
id: 2
title: App page
breadcrumb: App page
slug: app
class_name: RailsPage
status_id: 100
parent_id: 1
published_at: 2008-01-01 08:00:00
other:
id: 3
title: Other
breadcrumb: Other
slug: other
status_id: 100
parent_id: 1
published_at: 2008-01-01 08:00:00
18 changes: 18 additions & 0 deletions test/functional/share_layouts_extension_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'

class ShareLayoutsExtensionTest < Test::Unit::TestCase

def test_initialization
assert_equal File.join(File.expand_path(RAILS_ROOT), 'vendor', 'extensions', 'share_layouts'), ShareLayoutsExtension.root
assert_equal 'Share Layouts', ShareLayoutsExtension.extension_name
end

def test_should_add_controller_hooks
assert_respond_to ActionController::Base, :radiant_layout
assert_respond_to ActionController::Base.new, :set_radiant_layout
end

def test_should_add_helper
assert ApplicationController.master_helper_module.included_modules.include?(ShareLayouts::Helper)
end
end
Loading

0 comments on commit 9021a56

Please sign in to comment.