Class: Gluttonberg::PageDescription

Inherits:
Object
  • Object
show all
Defined in:
lib/gluttonberg/page_description.rb

Overview

This defines a DSL for for creating page descriptions. Page descriptions are used to declare the page archetypes in an installation.

  • Name & description

  • Sections

    • Html

    • Plain text

    • Image

  • Redirections

  • Rewrites to controllers

It also provides access to any page descriptions that have been declared.

Defined Under Namespace

Classes: Section

Constant Summary collapse

@@_descriptions =
{}
@@_categorised_descriptions =
{}
@@_description_names =
{}
@@_home_page =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PageDescription

Returns a new instance of PageDescription.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gluttonberg/page_description.rb', line 23

def initialize(name)
  @position = 0
  @options = {
    :name       => name,
    :home       => false,
    :domain       => nil, 
    :behaviour  => :default,
    :layout     => "public",
    :view       => "default"
  }
  @sections = {}
  @@_descriptions[name] = self
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



21
22
23
# File 'lib/gluttonberg/page_description.rb', line 21

def options
  @options
end

Class Method Details

.[](name) ⇒ Object

Returns the definition for a specific page description.



73
74
75
76
77
# File 'lib/gluttonberg/page_description.rb', line 73

def self.[](name)
  # puts @@_descriptions
  # puts @@_descriptions[name.to_s.downcase.to_sym].options
  @@_descriptions[name.to_s.downcase.to_sym]
end

.add(&blk) ⇒ Object



62
63
64
# File 'lib/gluttonberg/page_description.rb', line 62

def self.add(&blk)
  class_eval(&blk)
end

.allObject

Returns the full list of page descriptions as a hash, keyed to each description’s name.



81
82
83
# File 'lib/gluttonberg/page_description.rb', line 81

def self.all
  @@_descriptions
end

.behaviour(name) ⇒ Object

Returns all the descriptions with the matching behaviour in an array.



86
87
88
89
90
91
# File 'lib/gluttonberg/page_description.rb', line 86

def self.behaviour(name)
  @@_categorised_descriptions[name] ||= @@_descriptions.inject([]) do |memo, desc|
    memo << desc[1] if desc[1][:behaviour] == name
    memo
  end
end

.clear!Object

This is a destructive method which removes all page definitions. Mainly used for testing and debugging.



47
48
49
50
51
52
# File 'lib/gluttonberg/page_description.rb', line 47

def self.clear!
  @@_descriptions.clear
  @@_categorised_descriptions.clear
  @@_description_names.clear
  @@_home_page = nil
end

.names_for(name) ⇒ Object

Collects all the names of the descriptions which have the specified behaviour.



95
96
97
# File 'lib/gluttonberg/page_description.rb', line 95

def self.names_for(name)
  @@_description_names[name] ||= self.behaviour(name).collect {|d| d[:name]}
end

.page(name, &blk) ⇒ Object

Define a page. This can be called directly, but is generally used inside of an #add block.



68
69
70
# File 'lib/gluttonberg/page_description.rb', line 68

def self.page(name, &blk)
  new(name).instance_eval(&blk)
end

.setupObject

This just loads the page_descriptions.rb file from the config dir.

The specified file should contain the various page descriptions.



57
58
59
60
# File 'lib/gluttonberg/page_description.rb', line 57

def self.setup
  path = File.join(Rails.root, "config", "page_descriptions.rb")
  require path if File.exists?(path)
end

Instance Method Details

#[](opt) ⇒ Object

Returns the value the specified option — label, description etc.



100
101
102
# File 'lib/gluttonberg/page_description.rb', line 100

def [](opt)
  @options[opt]
end

#contains_section?(sec_name, type_name) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
# File 'lib/gluttonberg/page_description.rb', line 109

def contains_section?(sec_name , type_name)
  @sections.each do |name, section|
    return true if sec_name.to_s == name.to_s && section[:type].to_s == type_name.to_s
  end
  false
end

#domain(domain_name) ⇒ Object

Set a description as the home page.



129
130
131
# File 'lib/gluttonberg/page_description.rb', line 129

def domain(domain_name)
  @options[:domain] = domain_name
end

#home(bool) ⇒ Object

Set a description as the home page.



117
118
119
120
121
122
123
124
125
126
# File 'lib/gluttonberg/page_description.rb', line 117

def home(bool)
  @options[:home] = bool
  if bool
    @@_home_page = self
    @options[:limit] = 1
  elsif @@_home_page == self
    @@_home_page = nil
    @options.delete(:limit)
  end
end

#home?Boolean

Checks to see if this is home. Duh.

Returns:

  • (Boolean)


199
200
201
# File 'lib/gluttonberg/page_description.rb', line 199

def home?
  @options[:home]
end

#home_for_domain?(domain_name) ⇒ Boolean

Checks to see if this is home for a domain. Duh.

Returns:

  • (Boolean)


204
205
206
207
208
209
210
# File 'lib/gluttonberg/page_description.rb', line 204

def home_for_domain?(domain_name)
  if Rails.configuration.multisite == false
    home?
  else
    @options[:home] && Rails.configuration.multisite[@options[:domain]] == domain_name
  end  
end

#nameObject



146
147
148
# File 'lib/gluttonberg/page_description.rb', line 146

def name
   @options[:name]
end

#redirect?Boolean

Checks to see if the description has been defined as a redirect.

Returns:

  • (Boolean)


194
195
196
# File 'lib/gluttonberg/page_description.rb', line 194

def redirect?
  !@redirect_type.nil?
end

#redirect_to(type = nil, opt = nil, &blk) ⇒ Object

Declare this description as a redirect. The redirect type can be:

:remote - A full url to another domain :block - A block that will be evaluated and it’s return value will be

used to handle the redirect

:path - The path to redirect to, hey, simple! :page - Allows the user to specify which other page they want to

redirect to.


182
183
184
185
186
187
188
189
190
191
# File 'lib/gluttonberg/page_description.rb', line 182

def redirect_to(type = nil, opt = nil, &blk)
  if block_given?
    @redirect_block = blk
    @redirect_type  = :block
  else
    @redirect_option  = opt if opt
    @redirect_type    = type
  end
  @options[:behaviour]  = :redirect
end

#redirect_url(page, params) ⇒ Object

Returns the path that this description wants to redirect to. It accepts the current page — from which is extracts the redirect options — and the params for the current request.



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/gluttonberg/page_description.rb', line 215

def redirect_url(page, params)
  case @redirect_type
    when :remote
      @redirect_option
    when :block
      @redirect_block.call(page, params)
    when :path
      Router.localized_url(redirect_value(page, params), params)
    when :page
      path_to_page(page, params)
  end
end

#redirection_required?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/gluttonberg/page_description.rb', line 150

def redirection_required?
  @options[:behaviour] == :rewrite
end

#rewrite_required?Boolean

Allows us to check if this page needs to have a path rewritten to point to a controller.

Returns:

  • (Boolean)


165
166
167
# File 'lib/gluttonberg/page_description.rb', line 165

def rewrite_required?
  @options[:behaviour] == :rewrite
end

#rewrite_routeObject

Returns the named route to be used when rewriting the request.



170
171
172
# File 'lib/gluttonberg/page_description.rb', line 170

def rewrite_route
  @rewrite_route
end

#rewrite_to(route) ⇒ Object

Configures the page to act as a rewrite to named route. This doesn’t work like a rewrite in the traditional sense, since it is intended to be used to redirect requests to a controller. Becuase of this it can’t rewrite to a path, it needs to use a named route.



158
159
160
161
# File 'lib/gluttonberg/page_description.rb', line 158

def rewrite_to(route)
  @rewrite_route = route
  @options[:behaviour] = :rewrite
end

#section(name, &blk) ⇒ Object

Sugar for defining a section.



134
135
136
137
138
139
140
# File 'lib/gluttonberg/page_description.rb', line 134

def section(name, &blk)
  new_section = Section.new(name , @position)
  #new_section.position = @position
  new_section.instance_eval(&blk)
  @sections[name] = new_section
  @position += 1
end

#sectionsObject

Returns the collection of sections defined for a page description.



105
106
107
# File 'lib/gluttonberg/page_description.rb', line 105

def sections
  @sections
end

#top_level_page?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/gluttonberg/page_description.rb', line 142

def top_level_page?
   @options[:name] == :top_level_page
end