Class: Comatose::AdminController

Inherits:
ApplicationController show all
Defined in:
app/controllers/comatose/admin_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationController

#comatose_current_user, #find_page, #set_locale

Class Method Details

.configure_template_rootObject



332
333
334
335
336
337
338
339
340
# File 'app/controllers/comatose/admin_controller.rb', line 332

def configure_template_root
  if self.runtime_mode == :unknown
    if FileTest.exist? File.join(Rails.root, 'public', 'javascripts', 'admin.js')
      self.runtime_mode = :application
    else
      self.runtime_mode = :plugin
    end
  end
end

.expire_cms_page(page) ⇒ Object

Expire the page from all the mount points…



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/controllers/comatose/admin_controller.rb', line 304

def expire_cms_page(page)
  Comatose.mount_points.each do |path_info|
    Page.active_mount_info = path_info
    expire_page(page.uri)
    # If the page is the index page for the root, expire it too
    if path_info[:root] == page.uri
      expire_page("#{path_info[:root]}/index")
    end
    begin # I'm not sure this matters too much -- but it keeps things clean
      dir_path = File.join(Rails.root, 'public', page.uri[1..-1])
      Dir.delete( dir_path ) if FileTest.directory?( dir_path ) and !page.parent.nil?
    rescue
      # It probably isn't empty -- just as well we leave it be
      #STDERR.puts " - Couldn't delete dir #{dir_path} -> #{$!}"
    end
  end
end

.expire_cms_pages_from_bottom(page) ⇒ Object

Walks all the way down, and back up the tree – the allows the expire_cms_page to delete empty directories better



295
296
297
298
299
300
301
# File 'app/controllers/comatose/admin_controller.rb', line 295

def expire_cms_pages_from_bottom(page)
  pages = page.is_a?(Array) ? page : [page]
  pages.each do |page|
    page.children.each {|c| expire_cms_pages_from_bottom( c ) } if !page.children.empty?
    expire_cms_page( page )
  end
end

.get_page_layout(params) ⇒ Object

Returns a path to plugin layout, if it’s unspecified, otherwise a path to an application layout…



324
325
326
327
328
329
330
# File 'app/controllers/comatose/admin_controller.rb', line 324

def get_page_layout(params)
  if params[:layout] == 'comatose_content'
    File.join(plugin_layout_path, params[:layout])
  else
    params[:layout]
  end
end

.runtime_modeObject



342
343
344
# File 'app/controllers/comatose/admin_controller.rb', line 342

def runtime_mode
  @@runtime_mode ||= :unknown
end

.runtime_mode=(mode) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/controllers/comatose/admin_controller.rb', line 346

def runtime_mode=(mode)
  admin_view_path = File.expand_path(File.join( File.dirname(__FILE__), '..', 'views'))
  if self.respond_to?(:template_root)
    case mode
    when :plugin
      self.original_template_root = self.template_root
      self.template_root = admin_view_path
    when :application
      self.template_root = self.original_template_root if self.original_template_root
    end
  else
    ActionController::Base.append_view_path(admin_view_path) unless ActionController::Base.view_paths.include?(admin_view_path)
  end
  @@runtime_mode = mode
end

Instance Method Details

#createObject



48
49
50
51
52
53
54
55
56
# File 'app/controllers/comatose/admin_controller.rb', line 48

def create
  @root_pages = [fetch_root_page].flatten
  @page = Page.new params[:page]
  @page.author = fetch_author_name
  if @page.save
    flash[:notice] = "Created page '#{@page.title}'"
    redirect_to comatose_pages_path
  end
end

#deleteObject

Deletes the specified page



96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/comatose/admin_controller.rb', line 96

def delete
  @page = Page.find params[:id]
  if request.post?
    expire_cms_pages_from_bottom @page
    expire_cms_fragments_from_bottom @page
    @page.destroy
    flash[:notice] = "Deleted page '#{@page.title}'"
    redirect_to :controller=>self.controller_name, :action=>'index'
  end
end

#editObject

Edit a specific page



19
20
21
22
23
# File 'app/controllers/comatose/admin_controller.rb', line 19

def edit
  # Clear the page cache for this page... ?
  @page = Page.find params[:id]
  @root_pages = [fetch_root_page].flatten
end

#expire_page_cacheObject

Expires the entire page cache



127
128
129
130
131
132
# File 'app/controllers/comatose/admin_controller.rb', line 127

def expire_page_cache
  expire_cms_pages_from_bottom( fetch_root_page )
  expire_cms_fragments_from_bottom( fetch_root_page )
  flash[:notice] = "Page cache has been flushed"
  redirect_to :controller=>self.controller_name, :action=>'index'
end

#exportObject



152
153
154
155
156
157
158
159
# File 'app/controllers/comatose/admin_controller.rb', line 152

def export
  if Comatose.config.allow_import_export
    send_data(page_to_hash(Page.root).to_yaml, :disposition => 'attachment', :type => 'text/yaml', :filename => "comatose-pages.yml")
  else
    flash[:notice] = "Export is not allowed"
    redirect_to :controller=>self.controller_name, :action=>'index'
  end
end

#generate_page_cacheObject

Walks the page tree and generates HTML files in your /public folder… It will skip pages that have a ‘nocache’ keyword TODO: Make page cache generation work when in :plugin mode



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/comatose/admin_controller.rb', line 137

def generate_page_cache
  if runtime_mode == :plugin
    @errors = ["Page cache cannot be generated in plugin mode"]
  else
    @errors = generate_all_pages_html(params)
  end
  if @errors.length == 0
    flash[:notice] = "Pages Cached Successfully"
  else
    flash[:notice] = "Pages Cache Error(s): #{@errors.join(', ')}"
    flash[:cache_errors] = @errors
  end
  redirect_to :controller=>self.controller_name, :action=>'index'
end

#importObject



161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/comatose/admin_controller.rb', line 161

def import
  if Comatose.config.allow_import_export
    data = YAML::load(params[:import_file])
    hash_to_page_tree(data, Page.root)
    flash[:notice] = "Pages Imported Successfully"
  else
    flash[:notice] = "Import isn't allowed"
  end
  redirect_to :controller=>self.controller_name, :action=>'index'
end

#indexObject

Shows the page tree



13
14
15
16
# File 'app/controllers/comatose/admin_controller.rb', line 13

def index
  @root_pages = [fetch_root_page].flatten
  Comatose.logger.debug "current_user: #{current_user}"
end

#newObject

Create a new page (posts back)



42
43
44
45
# File 'app/controllers/comatose/admin_controller.rb', line 42

def new
  @root_pages = [fetch_root_page].flatten
  @page = Page.new :title=>'New Page', :parent_id => params[:parent]
end

#previewObject

Returns a preview of the page content…



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/comatose/admin_controller.rb', line 108

def preview
  begin
    content     = "No Content"
    page        = Page.new(params[:page])
    page.author = fetch_author_name
    if params.has_key? :version
      content = page.to_html({ 'params' => params.stringify_keys, 'version' => params[:version] })
    else
      content = page.to_html({ 'params' => params.stringify_keys })
    end
  rescue SyntaxError
    content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '&lt;')}</pre></p>"
  rescue
    content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '&lt;')}</pre></p>"
  end
  render :text  => content, :layout => false
end

#reorderObject

Saves position of child pages



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/comatose/admin_controller.rb', line 59

def reorder
  # If it's AJAX, do our thing and move on...
  if request.xhr?
    params["page_list_#{params[:id]}"].each_with_index { |id,idx| Page.update(id, :position => idx) }
    expire_cms_page Page.find(params[:id])
    render :text=>'Updated sort order', :layout=>false
  else
    @page = Page.find params[:id]
    if params.has_key? :cmd
      @target = Page.find params[:page]
      case params[:cmd]
        when 'up' then @target.move_higher
        when 'down' then @target.move_lower
      end
      redirect_to :action=>'reorder', :id=>@page
    end
  end
end

#set_versionObject

Reverts a page to a specific version…



86
87
88
89
90
91
92
93
# File 'app/controllers/comatose/admin_controller.rb', line 86

def set_version
  if request.post?
    @page = Page.find params[:id]
    @version_num = params[:version]
    @page.revert_to!(@version_num)
  end
  redirect_to :controller=>self.controller_name, :action=>'index'
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/comatose/admin_controller.rb', line 26

def update
  @page = Page.find params[:id]
  @root_pages = [fetch_root_page].flatten

  @page.update_attributes(params[:page].merge(:author => fetch_author_name))
  Comatose.logger.debug "Saved #{@page}"
  expire_cms_page     @page
  expire_cms_fragment @page
  flash[:notice] = "Saved changes to '#{@page.title}'"
  redirect_to comatose_pages_path and return false
rescue => e
  Comatose.logger.info e.message
  Comatose.logger.debug e.backtrace
end

#versionsObject

Allows comparing between two versions of a page’s content



79
80
81
82
83
# File 'app/controllers/comatose/admin_controller.rb', line 79

def versions
  @page = Page.find params[:id]
  @version_num = (params[:version] || @page.versions.length).to_i
  @version = @page.find_version(@version_num)
end