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
36
37
38
# File 'lib/gluttonberg/page_description.rb', line 23

def initialize(name)
  @position = 0
  @options = {
    :name       => name,
    :home       => false,
    :domain       => :default,
    :behaviour  => :default,
    :layout     => "public",
    :view       => "default",
    :page_options => {},
    :group => nil,
    :contributor_access => false
  }
  @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.



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

def self.[](name)
  self.all[name.to_s.downcase.to_sym]
end

.add(&blk) ⇒ Object



64
65
66
# File 'lib/gluttonberg/page_description.rb', line 64

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.



50
51
52
53
54
55
# File 'lib/gluttonberg/page_description.rb', line 50

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

.find_home_page_description_for_domain?(domain_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.find_home_page_description_for_domain?(domain_name)
  page_desc = PageDescription.all.find{|key , val|  val.home_for_domain?(domain_name) }
  page_desc = page_desc.last unless page_desc.blank?
  page_desc
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.



70
71
72
# File 'lib/gluttonberg/page_description.rb', line 70

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.



59
60
61
62
# File 'lib/gluttonberg/page_description.rb', line 59

def self.setup
  path = File.join(Rails.root, "config", "page_descriptions.rb")
  eval(File.read(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

#contributor_access(access) ⇒ Object



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

def contributor_access(access)
  @options[:contributor_access] = access
end

#contributor_access?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/gluttonberg/page_description.rb', line 175

def contributor_access?
  @options[:contributor_access]
end

#domain(domain_name) ⇒ Object

Set a description as the home page.



135
136
137
# File 'lib/gluttonberg/page_description.rb', line 135

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

#group(grp) ⇒ Object



167
168
169
# File 'lib/gluttonberg/page_description.rb', line 167

def group(grp)
  @options[:group] = grp
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)


209
210
211
# File 'lib/gluttonberg/page_description.rb', line 209

def home?
  @options[:home]
end

#home_for_domain?(domain_name) ⇒ Boolean

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

Returns:

  • (Boolean)


214
215
216
217
218
219
220
# File 'lib/gluttonberg/page_description.rb', line 214

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

#nameObject



155
156
157
# File 'lib/gluttonberg/page_description.rb', line 155

def name
   @options[:name]
end

#page_options(opts = {}) ⇒ Object



163
164
165
# File 'lib/gluttonberg/page_description.rb', line 163

def page_options(opts = {})
  @options[:page_options] = opts
end

#redirect_to(path_or_url) ⇒ Object

Declare this description as a redirect. The redirect type can be: :path - The path to redirect to, hey, simple! :page - Allows the user to specify which other page they want to

redirect to.


203
204
205
206
# File 'lib/gluttonberg/page_description.rb', line 203

def redirect_to(path_or_url)
  @redirect_path_or_url  = path_or_url if path_or_url
  @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.



225
226
227
# File 'lib/gluttonberg/page_description.rb', line 225

def redirect_url(page, params={})
  @redirect_path_or_url
end

#redirection_required?Boolean

Returns:

  • (Boolean)


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

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

#remove_section(name) ⇒ Object



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

def remove_section(name)
  @sections.delete(name)
end

#rewrite_required?Boolean

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

Returns:

  • (Boolean)


190
191
192
# File 'lib/gluttonberg/page_description.rb', line 190

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

#rewrite_routeObject

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



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

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.



183
184
185
186
# File 'lib/gluttonberg/page_description.rb', line 183

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

#section(name, &blk) ⇒ Object

Sugar for defining a section.



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

def section(name, &blk)
  new_section = Section.new(name , @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)


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

def top_level_page?
   name == :top_level_page
end