# ActsAsCms
require 'erb'
module ActionView
class Base
alias_method :old_render, :render
def render(options = {}, old_local_assigns = {}, &block) #:nodoc:
if options.is_a?(Hash)
if options[:as_cms]
render_as_cms options[:as_cms]
else
old_render(options, old_local_assigns = {}, &block)
end
else
old_render(options, old_local_assigns = {}, &block)
end
end
private
def render_as_cms(cms_content_name)
controller_name = self.params[:controller]
action_name = self.params[:action]
cms_controller = CmsController.find_by_name(controller_name) ||
CmsController.create(:name => controller_name)
cms_action = cms_controller.cms_actions.find_by_name(action_name) ||
CmsAction.create(:name => action_name,
:cms_controller_id => cms_controller.id)
cms_content = cms_action.cms_contents.find_by_name(cms_content_name) ||
CmsContent.create(:name => cms_content_name,
:cms_controller_id => cms_controller.id,
:cms_action_id => cms_action.id)
to_render = "
"
to_render += cms_content.content.gsub("\n", "
")
to_render += "#{link_to 'Edit',
:action => 'cms_edit',
:id => cms_content.id}"
to_render += "
"
render :inline => to_render
end
end
end
module Acts
module CMS
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods
def acts_as_cms(options = {})
# create crud for CMS
define_default_cms_methods(options)
# create the CMS defined actions (from the database)
#define_cms_actions(options)
end
private
# add the CRUD for CMS Administration
def define_default_cms_methods(options)
@@template_path = "#{RAILS_ROOT}/vendor/plugins/acts_as_cms/templates"
define_method("cms_edit") do
@cms_content = CmsContent.find(params[:id])
if request.post? and params[:cms_content]
@cms_content.update_attributes(params[:cms_content])
redirect_to :action => @cms_content.cms_action.name
return
end
render :file => "#{@@template_path}/cms_edit.rhtml"
return
end
end
def define_cms_actions(options)
# find the controller
cms_controller = CmsController.find_by_name(self.controller_name)
# if we do not have this controller, we can add it
if cms_controller.nil?
cms_controller = CmsController.create(:name => self.controller_name)
end
# now see if we have any actions that have been defined
cms_actions = cms_controller.cms_actions
# find the cms_action
cms_action = cms_actions.select{|ca| ca.name == self.action_name}
if cms_action.nil?
cms_action = CmsAction.create(:name => self.action_name)
end
# get the cms_contents
cms_contents = cms_action.cms_contents
# for each action we define it and have it write its content
cms_actions.each do |cms_action|
define_method(cms_action.name) do
content = ERB.new(cms_action.content).result
#TODO generate an actual view ...
# what if text had an actual partial
render :text => content
end
end
end
end
end
end