Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created January 5, 2009 23:04
Show Gist options
  • Select an option

  • Save lukesutton/43569 to your computer and use it in GitHub Desktop.

Select an option

Save lukesutton/43569 to your computer and use it in GitHub Desktop.
Gluttonberg::PageDescription.add do
page :home do
home true
label "Home Page"
view "home"
layout "default"
section :main do
label "Main"
type :rich_text
config :option => 1
end
end
page :redirect do
label "Redirect"
description "An example of a redirect"
redirect_to :url, Merb::Router.url(:custom_route)
# redirect_to :page
# redirect_to do
# user = User.first
# Merb::Router.resource(user)
# end
end
end
module Gluttonberg
class PageDescription
@@_descriptions = {}
@@_home_page = nil
attr_accessor :options
def initialize(name)
@options = {:name => name, :home => false, :behaviour => :default}
@sections = []
@@_descriptions[name] = self
end
%w(label view layout limit description).each do |opt|
class_eval %{
def #{opt}(opt_value)
@options[:#{opt}] = opt_value
end
}
end
def self.add(&blk)
class_eval(&blk)
end
def self.page(name, &blk)
new(name).instance_eval(&blk)
end
def home(bool)
if bool
@@_home_page = self
@options[:limit] = 1
elsif @@_home_page == self
@@_home_page = nil
@options.delete(:limit)
end
end
def section(name, &blk)
new_section = Section.new(name)
new_section.instance_eval(&blk)
@sections << new_section
end
def redirect_to(type_or_blk, opt = nil)
if type_or_blk.is_a? Proc
@redirect_type = :block
@redirect_block = type_or_blk
else
@redirect_type = type_or_blk
@redirect_option = opt if opt
end
end
def redirect?
!@redirect_type.nil?
end
def redirect_url(page, params)
path = case @redirect_type
when :block then @redirect_block.call
when :url then @redirect_option
when :page then redirect_to_page(page, params)
when :component then Merb::Router.url(@redirect_option)
end
Router.localized_url(path, params)
end
private
def redirect_to_page(page, params)
localization = PageLocalization.first(
:fields => [:path],
:page_id => page.redirect_target_id,
:locale => params[:locale],
:dialect => params[:dialect]
)
raise DataMapper::ObjectNotFoundError unless localization
localization.path
end
class Section
attr_accessor :options, :custom
def initialize(name)
@options = {:name => name, :limit => 1}
end
%w(type limit).each do |opt|
class_eval %{
def #{opt}(opt_value)
@options[:#{opt}] = opt_value
end
}
end
def config(opts)
@config ||= {}
@config.merge!(opts)
end
end # Section
end # PageDescription
end # Gluttonberg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment