Guest User

Untitled

a guest
Feb 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. module Gluttonberg
  2. class PageDescription
  3. @@_descriptions = {}
  4. @@_home_page = nil
  5.  
  6. attr_accessor :options
  7.  
  8. def initialize(name)
  9. @options = {:name => name, :home => false, :behaviour => :default}
  10. @sections = []
  11. @@_descriptions[name] = self
  12. end
  13.  
  14. %w(label view layout limit description).each do |opt|
  15. class_eval %{
  16. def #{opt}(opt_value)
  17. @options[:#{opt}] = opt_value
  18. end
  19. }
  20. end
  21.  
  22. def self.add(&blk)
  23. class_eval(&blk)
  24. end
  25.  
  26. def self.page(name, &blk)
  27. new(name).instance_eval(&blk)
  28. end
  29.  
  30. def home(bool)
  31. if bool
  32. @@_home_page = self
  33. @options[:limit] = 1
  34. elsif @@_home_page == self
  35. @@_home_page = nil
  36. @options.delete(:limit)
  37. end
  38. end
  39.  
  40. def section(name, &blk)
  41. new_section = Section.new(name)
  42. new_section.instance_eval(&blk)
  43. @sections << new_section
  44. end
  45.  
  46. def redirect_to(type_or_blk, opt = nil)
  47. if type_or_blk.is_a? Proc
  48. @redirect_type = :block
  49. @redirect_block = type_or_blk
  50. else
  51. @redirect_type = type_or_blk
  52. @redirect_option = opt if opt
  53. end
  54. end
  55.  
  56. def redirect?
  57. !@redirect_type.nil?
  58. end
  59.  
  60. def redirect_url(page, params)
  61. path = case @redirect_type
  62. when :block then @redirect_block.call
  63. when :url then @redirect_option
  64. when :page then redirect_to_page(page, params)
  65. when :component then Merb::Router.url(@redirect_option)
  66. end
  67. Router.localized_url(path, params)
  68. end
  69.  
  70. private
  71.  
  72. def redirect_to_page(page, params)
  73. localization = PageLocalization.first(
  74. :fields => [:path],
  75. :page_id => page.redirect_target_id,
  76. :locale => params[:locale],
  77. :dialect => params[:dialect]
  78. )
  79. raise DataMapper::ObjectNotFoundError unless localization
  80. localization.path
  81. end
  82.  
  83. class Section
  84. attr_accessor :options, :custom
  85.  
  86. def initialize(name)
  87. @options = {:name => name, :limit => 1}
  88. end
  89.  
  90. %w(type limit).each do |opt|
  91. class_eval %{
  92. def #{opt}(opt_value)
  93. @options[:#{opt}] = opt_value
  94. end
  95. }
  96. end
  97.  
  98. def config(opts)
  99. @config ||= {}
  100. @config.merge!(opts)
  101. end
  102. end # Section
  103. end # PageDescription
  104. end # Gluttonberg
Add Comment
Please, Sign In to add comment