Class: Humpyard::PagesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/humpyard/pages_controller.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#createObject

Create a new page



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/humpyard/pages_controller.rb', line 39

def create
  @page = Humpyard::config.page_types[params[:type]].new params[:page]
  @page.title_for_url = @page.page.suggested_title_for_url
  
  authorize! :create, @page.page
  
  if @page.save
    @prev = Humpyard::Page.find_by_id(params[:prev_id])
    @next = Humpyard::Page.find_by_id(params[:next_id])
    
    #do_move(@page, @prev, @next)
  
    # insert_options = {
    #   :element => "hy-id-#{@page.id}",
    #   :url => @page.page.human_url,
    #   :parent => @page.parent ? "hy-page-dialog-item-#{@page.id}" : "hy-page-dialog-pages"
    # }
    
    # insert_options[:before] = "hy-page-dialog-item-#{@next.id}" if @next
    # insert_options[:after] = "hy-page-dialog-item-#{@prev.id}" if not @next and @prev

    # just reload the tree
  
    replace_options = {
      :element => "hy-page-treeview",
      :content => render_to_string(:partial => "tree.html", :locals => {:page => @page})
    }
  
    render :json => {
      :status => :ok,
      #:dialog => :close,
      # :insert => [insert_options],
      :replace => [replace_options],
      :flash => {
        :level => 'info',
        :content => I18n.t('humpyard_form.flashes.create_success', :model => Humpyard::Page.model_name.human)
      }
    }
  else
    render :json => {
      :status => :failed, 
      :errors => @page.errors,
      :flash => {
        :level => 'error',
        :content => I18n.t('humpyard_form.flashes.create_fail', :model => Humpyard::Page.model_name.human)
      }
    }
  end
end

#destroyObject

Destroy a page



185
186
187
188
189
190
191
# File 'app/controllers/humpyard/pages_controller.rb', line 185

def destroy
  @page = Humpyard::Page.find(params[:id])
  
  authorize! :destroy, @page  
  
  @page.destroy
end

#editObject

Dialog content for an existing page



90
91
92
93
94
95
96
# File 'app/controllers/humpyard/pages_controller.rb', line 90

def edit
  @page = Humpyard::Page.find(params[:id]).content_data
  
  authorize! :update, @page.page
  
  render :partial => 'edit'
end

#indexObject

Probably unneccassary - may be removed later



8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/humpyard/pages_controller.rb', line 8

def index
  authorize! :manage, Humpyard::Page  
  
  @root_page = Humpyard::Page.root
  @page = Humpyard::Page.find_by_id(params[:actual_page_id])
 
  unless @page.nil?
    @page = @page.content_data
  end
  render :partial => 'index'
end

#moveObject

Move a page



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/humpyard/pages_controller.rb', line 146

def move
  @page = Humpyard::Page.find(params[:id])
  
  if @page 
    unless can? :update, @page
      render :json => {
        :status => :failed
      }, :status => 403
      return
    end
    
    parent = Humpyard::Page.find_by_id(params[:parent_id])
    # by default, make it possible to move page to root, uncomment to do otherwise:
    #unless parent
    #  parent = Humpyard::Page.root_page
    #end
    @page.update_attribute :parent, parent
    @prev = Humpyard::Page.find_by_id(params[:prev_id])
    @next = Humpyard::Page.find_by_id(params[:next_id])
    
    do_move(@page, @prev, @next)
    
    replace_options = {
      :element => "hy-page-treeview",
      :content => render_to_string(:partial => "tree.html", :locals => {:page => @page})
    }
    
    render :json => {
      :status => :ok,
      :replace => [replace_options]
    }
  else
    render :json => {
      :status => :failed
    }, :status => 404        
  end
end

#newObject

Dialog content for a new page



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/humpyard/pages_controller.rb', line 21

def new
  @page = Humpyard::config.page_types[params[:type]].new
  
  unless can? :create, @page.page
    render :json => {
      :status => :failed
    }, :status => 403
    return
  end
  
  @page_type = params[:type]
  @prev = Humpyard::Page.find_by_id(params[:prev_id])
  @next = Humpyard::Page.find_by_id(params[:next_id])
  
  render :partial => 'edit'
end

#showObject

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.

Raises:

  • (::ActionController::RoutingError)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'app/controllers/humpyard/pages_controller.rb', line 203

def show
  # No page found at the beginning
  @page = nil

  if params[:locale] and Humpyard.config.locales.include? params[:locale].to_sym
    I18n.locale = params[:locale]
  elsif session[:humpyard_locale]
    I18n.locale = session[:humpyard_locale]
  else
    I18n.locale = Humpyard.config.locales.first
  end
  
  # Find page by name
  if not params[:webpath].blank?
    dyn_page_path = false
    parent_page = nil
    params[:webpath].split('/').each do |path_part|
      # Ignore multiple slashes in URLs
      unless(path_part.blank?)
        if(dyn_page_path) 
          dyn_page_path << path_part
        else     
          # Find page by name and parent; parent=nil means page on root level
          @page = Page.with_translated_attribute(:title_for_url, CGI::escape(path_part), I18n.locale).first
          # Raise 404 if no page was found for the URL or subpart
          raise ::ActionController::RoutingError, "No route matches \"#{request.path}\" (X4201) [#{path_part}]" if @page.nil?
          raise ::ActionController::RoutingError, "No route matches \"#{request.path}\" (X4202)" if not (@page.parent == parent_page or @page.parent == Humpyard::Page.root_page)
          
          parent_page = @page unless @page.is_root_page?
          dyn_page_path = [] if @page.content_data.is_humpyard_dynamic_page? 
        end
      end
    end

    if @page.content_data.is_humpyard_dynamic_page? and dyn_page_path.size > 0
      @sub_page = @page.parse_path(dyn_page_path)
      
      # Raise 404 if no page was found for the sub_page part
      raise ::ActionController::RoutingError, "No route matches \"#{request.path}\" (D4201)" if @sub_page.blank?

      @page_partial = "/humpyard/pages/#{@page.content_data_type.split('::').last.underscore.pluralize}/#{@sub_page[:partial]}" if @sub_page[:partial]
      @local_vars = {:page => @page}.merge(@sub_page[:locals]) if @sub_page[:locals] and @sub_page[:locals].class == Hash
      
      # render partial only if request was an AJAX-call
      if request.xhr?
        respond_to do |format|
          format.html {
            render :partial => @page_partial, :locals => @local_vars
          }
        end
        return
      end
    end
    
  # Find page by id
  elsif not params[:id].blank?
    # Render page by id if not webpath was given but an id
    @page = Page.find(params[:id])
  # Find root page
  else
    # Render index page if neither id or webpath was given
    @page = Page.root_page :force_reload => true
    unless @page
      @page = Page.new
      render '/humpyard/pages/welcome'
      return false
    end
  end
  
  # Raise 404 if no page was found
  raise ::ActionController::RoutingError, "No route matches \"#{request.path}\"" if @page.nil? or @page.content_data.is_humpyard_virtual_page?
  
  response.headers['X-Humpyard-Page'] = "#{@page.id}"
  response.headers['X-Humpyard-Modified'] = "#{@page.last_modified}"
  response.headers['X-Humpyard-ServerTime'] = "#{Time.now.utc}"
 
  @page_partial ||= "/humpyard/pages/#{@page.content_data_type.split('::').last.underscore.pluralize}/show"
  @local_vars ||= {:page => @page}

  self.class.layout(@page.template_name)

  if Rails.application.config.action_controller.perform_caching and not @page.always_refresh
    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?
  end
end

#sitemapObject

Render the sitemap.xml for robots



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/controllers/humpyard/pages_controller.rb', line 290

def sitemap
  require 'builder'
  
  xml = ::Builder::XmlMarkup.new :indent => 2
  xml.instruct!
  xml.tag! :urlset, {
    'xmlns'=>"http://www.sitemaps.org/schemas/sitemap/0.9",
    'xmlns:xsi'=>"http://www.w3.org/2001/XMLSchema-instance",
    'xsi:schemaLocation'=>"http://www.sitemaps.org/schemas/sitemap/0.9\nhttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
  } do

    base_url = "#{request.protocol}#{request.host}#{request.port==80 ? '' : ":#{request.port}"}"

    if root_page = Page.root_page
      Humpyard.config.locales.each do |locale|
        add_to_sitemap xml, base_url, locale, [{
            :index => true,
            :url => root_page.human_url(:locale => locale),
            :lastmod => root_page.last_modified,
            :children => []
          }] + root_page.child_pages(:single_root => true).map{|p| p.content_data.site_map(locale)}
      end
    end
  end
  render :xml => xml.target!
end

#updateObject

Update an existing page



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/humpyard/pages_controller.rb', line 99

def update
  @page = Humpyard::Page.find(params[:id]).content_data
  
  if @page 
    unless can? :update, @page.page
      render :json => {
        :status => :failed
      }, :status => 403
      return
    end
    
    if @page.update_attributes params[:page]
      @page.title_for_url = @page.page.suggested_title_for_url
      @page.save
      render :json => {
        :status => :ok,
#            :dialog => :close,
        :replace => [
          { 
            :element => "hy-page-treeview-text-#{@page.id}",
            :content => @page.title
          }
        ],
        :flash => {
          :level => 'info',
          :content => I18n.t('humpyard_form.flashes.update_success', :model => Humpyard::Page.model_name.human)
        }
      }
    else
      render :json => {
        :status => :failed, 
        :errors => @page.errors,
        :flash => {
          :level => 'error',
          :content => I18n.t('humpyard_form.flashes.update_fail', :model => Humpyard::Page.model_name.human)
        }
      }
    end
  else
    render :json => {
      :status => :failed
    }, :status => 404
  end
  
end