Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment