class Humpyard::PagesController

Humpyard::PagesController is the controller for editing and viewing pages

Public Instance Methods

create() click to toggle source

Create a new page

   # File app/controllers/humpyard/pages_controller.rb
39 def create
40   @page = Humpyard::config.page_types[params[:type]].new params[:page]
41   @page.title_for_url = @page.page.suggested_title_for_url
42   
43   authorize! :create, @page.page
44   
45   if @page.save
46     @prev = Humpyard::Page.find_by_id(params[:prev_id])
47     @next = Humpyard::Page.find_by_id(params[:next_id])
48     
49     #do_move(@page, @prev, @next)
50   
51     # insert_options = {
52     #   :element => "hy-id-#{@page.id}",
53     #   :url => @page.page.human_url,
54     #   :parent => @page.parent ? "hy-page-dialog-item-#{@page.id}" : "hy-page-dialog-pages"
55     # }
56     
57     # insert_options[:before] = "hy-page-dialog-item-#{@next.id}" if @next
58     # insert_options[:after] = "hy-page-dialog-item-#{@prev.id}" if not @next and @prev
59 
60     # just reload the tree
61   
62     replace_options = {
63       :element => "hy-page-treeview",
64       :content => render_to_string(:partial => "tree.html", :locals => {:page => @page})
65     }
66   
67     render :json => {
68       :status => :ok,
69       #:dialog => :close,
70       # :insert => [insert_options],
71       :replace => [replace_options],
72       :flash => {
73         :level => 'info',
74         :content => I18n.t('humpyard_form.flashes.create_success', :model => Humpyard::Page.model_name.human)
75       }
76     }
77   else
78     render :json => {
79       :status => :failed, 
80       :errors => @page.errors,
81       :flash => {
82         :level => 'error',
83         :content => I18n.t('humpyard_form.flashes.create_fail', :model => Humpyard::Page.model_name.human)
84       }
85     }
86   end
87 end
destroy() click to toggle source

Destroy a page

    # File app/controllers/humpyard/pages_controller.rb
185 def destroy
186   @page = Humpyard::Page.find(params[:id])
187   
188   authorize! :destroy, @page  
189   
190   @page.destroy
191 end
edit() click to toggle source

Dialog content for an existing page

   # File app/controllers/humpyard/pages_controller.rb
90 def edit
91   @page = Humpyard::Page.find(params[:id]).content_data
92   
93   authorize! :update, @page.page
94   
95   render :partial => 'edit'
96 end
index() click to toggle source

Probably unneccassary - may be removed later

   # File app/controllers/humpyard/pages_controller.rb
 8 def index
 9   authorize! :manage, Humpyard::Page  
10   
11   @root_page = Humpyard::Page.root
12   @page = Humpyard::Page.find_by_id(params[:actual_page_id])
13  
14   unless @page.nil?
15     @page = @page.content_data
16   end
17   render :partial => 'index'
18 end
move() click to toggle source

Move a page

    # File app/controllers/humpyard/pages_controller.rb
146 def move
147   @page = Humpyard::Page.find(params[:id])
148   
149   if @page 
150     unless can? :update, @page
151       render :json => {
152         :status => :failed
153       }, :status => 403
154       return
155     end
156     
157     parent = Humpyard::Page.find_by_id(params[:parent_id])
158     # by default, make it possible to move page to root, uncomment to do otherwise:
159     #unless parent
160     #  parent = Humpyard::Page.root_page
161     #end
162     @page.update_attribute :parent, parent
163     @prev = Humpyard::Page.find_by_id(params[:prev_id])
164     @next = Humpyard::Page.find_by_id(params[:next_id])
165     
166     do_move(@page, @prev, @next)
167     
168     replace_options = {
169       :element => "hy-page-treeview",
170       :content => render_to_string(:partial => "tree.html", :locals => {:page => @page})
171     }
172     
173     render :json => {
174       :status => :ok,
175       :replace => [replace_options]
176     }
177   else
178     render :json => {
179       :status => :failed
180     }, :status => 404        
181   end
182 end
new() click to toggle source

Dialog content for a new page

   # File app/controllers/humpyard/pages_controller.rb
21 def new
22   @page = Humpyard::config.page_types[params[:type]].new
23   
24   unless can? :create, @page.page
25     render :json => {
26       :status => :failed
27     }, :status => 403
28     return
29   end
30   
31   @page_type = params[:type]
32   @prev = Humpyard::Page.find_by_id(params[:prev_id])
33   @next = Humpyard::Page.find_by_id(params[:next_id])
34   
35   render :partial => 'edit'
36 end
show() click to toggle source

Render a given page

When a “webpath” parameter is given it will render the page with that name. This is what happens when you call a page with it's pretty URL.

When no “name” is given and an “id” parameter is given it will render the page with the given id.

When no “name” nor “id” parameter is given it will render the root page.

    # File app/controllers/humpyard/pages_controller.rb
203 def show
204   # No page found at the beginning
205   @page = nil
206 
207   if params[:locale] and Humpyard.config.locales.include? params[:locale].to_sym
208     I18n.locale = params[:locale]
209   elsif session[:humpyard_locale]
210     I18n.locale = session[:humpyard_locale]
211   else
212     I18n.locale = Humpyard.config.locales.first
213   end
214   
215   # Find page by name
216   if not params[:webpath].blank?
217     dyn_page_path = false
218     parent_page = nil
219     params[:webpath].split('/').each do |path_part|
220       # Ignore multiple slashes in URLs
221       unless(path_part.blank?)
222         if(dyn_page_path) 
223           dyn_page_path << path_part
224         else     
225           # Find page by name and parent; parent=nil means page on root level
226           @page = Page.with_translated_attribute(:title_for_url, CGI::escape(path_part), I18n.locale).first
227           # Raise 404 if no page was found for the URL or subpart
228           raise ::ActionController::RoutingError, "No route matches \"#{request.path}\" (X4201) [#{path_part}]" if @page.nil?
229           raise ::ActionController::RoutingError, "No route matches \"#{request.path}\" (X4202)" if not (@page.parent == parent_page or @page.parent == Humpyard::Page.root_page)
230           
231           parent_page = @page unless @page.is_root_page?
232           dyn_page_path = [] if @page.content_data.is_humpyard_dynamic_page? 
233         end
234       end
235     end
236 
237     if @page.content_data.is_humpyard_dynamic_page? and dyn_page_path.size > 0
238       @sub_page = @page.parse_path(dyn_page_path)
239       
240       # Raise 404 if no page was found for the sub_page part
241       raise ::ActionController::RoutingError, "No route matches \"#{request.path}\" (D4201)" if @sub_page.blank?
242 
243       @page_partial = "/humpyard/pages/#{@page.content_data_type.split('::').last.underscore.pluralize}/#{@sub_page[:partial]}" if @sub_page[:partial]
244       @local_vars = {:page => @page}.merge(@sub_page[:locals]) if @sub_page[:locals] and @sub_page[:locals].class == Hash
245       
246       # render partial only if request was an AJAX-call
247       if request.xhr?
248         respond_to do |format|
249           format.html {
250             render :partial => @page_partial, :locals => @local_vars
251           }
252         end
253         return
254       end
255     end
256     
257   # Find page by id
258   elsif not params[:id].blank?
259     # Render page by id if not webpath was given but an id
260     @page = Page.find(params[:id])
261   # Find root page
262   else
263     # Render index page if neither id or webpath was given
264     @page = Page.root_page :force_reload => true
265     unless @page
266       @page = Page.new
267       render '/humpyard/pages/welcome'
268       return false
269     end
270   end
271   
272   # Raise 404 if no page was found
273   raise ::ActionController::RoutingError, "No route matches \"#{request.path}\"" if @page.nil? or @page.content_data.is_humpyard_virtual_page?
274   
275   response.headers['X-Humpyard-Page'] = "#{@page.id}"
276   response.headers['X-Humpyard-Modified'] = "#{@page.last_modified}"
277   response.headers['X-Humpyard-ServerTime'] = "#{Time.now.utc}"
278  
279   @page_partial ||= "/humpyard/pages/#{@page.content_data_type.split('::').last.underscore.pluralize}/show"
280   @local_vars ||= {:page => @page}
281 
282   self.class.layout(@page.template_name)
283 
284   if Rails.application.config.action_controller.perform_caching and not @page.always_refresh
285     fresh_when :etag => "#{humpyard_user.nil? ? '' : humpyard_user}p#{@page.id}m#{@page.last_modified}", :last_modified => @page.last_modified(:include_pages => true), :public => @humpyard_user.nil?
286   end
287 end
sitemap() click to toggle source

Render the sitemap.xml for robots

    # File app/controllers/humpyard/pages_controller.rb
290 def sitemap
291   require 'builder'
292   
293   xml = ::Builder::XmlMarkup.new :indent => 2
294   xml.instruct!
295   xml.tag! :urlset, {
296     'xmlns'=>"http://www.sitemaps.org/schemas/sitemap/0.9",
297     'xmlns:xsi'=>"http://www.w3.org/2001/XMLSchema-instance",
298     'xsi:schemaLocation'=>"http://www.sitemaps.org/schemas/sitemap/0.9\nhttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
299   } do
300 
301     base_url = "#{request.protocol}#{request.host}#{request.port==80 ? '' : ":#{request.port}"}"
302 
303     if root_page = Page.root_page
304       Humpyard.config.locales.each do |locale|
305         add_to_sitemap xml, base_url, locale, [{
306             :index => true,
307             :url => root_page.human_url(:locale => locale),
308             :lastmod => root_page.last_modified,
309             :children => []
310           }] + root_page.child_pages(:single_root => true).map{|p| p.content_data.site_map(locale)}
311       end
312     end
313   end
314   render :xml => xml.target!
315 end
update() click to toggle source

Update an existing page

    # File app/controllers/humpyard/pages_controller.rb
 99     def update
100       @page = Humpyard::Page.find(params[:id]).content_data
101       
102       if @page 
103         unless can? :update, @page.page
104           render :json => {
105             :status => :failed
106           }, :status => 403
107           return
108         end
109         
110         if @page.update_attributes params[:page]
111           @page.title_for_url = @page.page.suggested_title_for_url
112           @page.save
113           render :json => {
114             :status => :ok,
115 #            :dialog => :close,
116             :replace => [
117               { 
118                 :element => "hy-page-treeview-text-#{@page.id}",
119                 :content => @page.title
120               }
121             ],
122             :flash => {
123               :level => 'info',
124               :content => I18n.t('humpyard_form.flashes.update_success', :model => Humpyard::Page.model_name.human)
125             }
126           }
127         else
128           render :json => {
129             :status => :failed, 
130             :errors => @page.errors,
131             :flash => {
132               :level => 'error',
133               :content => I18n.t('humpyard_form.flashes.update_fail', :model => Humpyard::Page.model_name.human)
134             }
135           }
136         end
137       else
138         render :json => {
139           :status => :failed
140         }, :status => 404
141       end
142       
143     end