Class: ComatoseAdminController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/comatose_admin_controller.rb

Overview

The controller for serving cms content…

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_template_rootObject



326
327
328
329
330
331
332
333
334
# File 'lib/comatose_admin_controller.rb', line 326

def configure_template_root
  if self.runtime_mode == :unknown
    if FileTest.exist? File.join(RAILS_ROOT, 'public', 'javascripts', 'comatose_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…



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/comatose_admin_controller.rb', line 298

def expire_cms_page(page)
  Comatose.mount_points.each do |path_info|
    ComatosePage.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



289
290
291
292
293
294
295
# File 'lib/comatose_admin_controller.rb', line 289

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…



318
319
320
321
322
323
324
# File 'lib/comatose_admin_controller.rb', line 318

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

.runtime_modeObject



336
337
338
# File 'lib/comatose_admin_controller.rb', line 336

def runtime_mode
  @@runtime_mode ||= :unknown
end

.runtime_mode=(mode) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/comatose_admin_controller.rb', line 340

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

#deleteObject

Deletes the specified page



107
108
109
110
111
112
113
114
115
116
# File 'lib/comatose_admin_controller.rb', line 107

def delete
  @page = ComatosePage.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 specfic page (posts back)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/comatose_admin_controller.rb', line 18

def edit
  # Clear the page cache for this page... ?
  @page = ComatosePage.find params[:id]
  @root_pages = [fetch_root_page].flatten
  if request.post?
    @page.update_attributes(params[:page])
    @page.updated_on = Time.now
    @page.author = fetch_author_name
    if @page.save
      begin
        instance_eval &Comatose.config.controller_edit_after_page_save
      rescue Exception => e
        p e
      end

      expire_cms_page @page
      expire_cms_fragment @page
      flash[:notice] = "Saved changes to '#{@page.title}'"
      redirect_to :controller=>self.controller_name, :action=>'index'
    end
  else
    begin
      instance_eval &Comatose.config.controller_edit_show
    rescue Exception => e
      p e
    end
  end

end

#expire_page_cacheObject

Expires the entire page cache



137
138
139
140
141
142
# File 'lib/comatose_admin_controller.rb', line 137

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



162
163
164
165
166
167
168
169
# File 'lib/comatose_admin_controller.rb', line 162

def export
  if Comatose.config.allow_import_export
    send_data(page_to_hash(ComatosePage.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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/comatose_admin_controller.rb', line 147

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



171
172
173
174
175
176
177
178
179
180
# File 'lib/comatose_admin_controller.rb', line 171

def import
  if Comatose.config.allow_import_export
    data = YAML::load(params[:import_file])
    hash_to_page_tree(data, ComatosePage.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
# File 'lib/comatose_admin_controller.rb', line 13

def index
  @root_pages = [fetch_root_page].flatten
end

#newObject

Create a new page (posts back)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/comatose_admin_controller.rb', line 49

def new
  @root_pages = [fetch_root_page].flatten
  if request.post?
    @page = ComatosePage.new params[:page]
    @page.author = fetch_author_name
    if @page.save
      begin
        instance_eval &Comatose.config.controller_new_after_page_save
      rescue Exception => e
        p e
      end

      flash[:notice] = "Created page '#{@page.title}'"
      redirect_to :controller=>self.controller_name, :action=>'index'
    end
  else
    @page = ComatosePage.new :title=>'New Page', :parent_id=>(params[:parent] || nil)
  end
end

#previewObject

Returns a preview of the page content…



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/comatose_admin_controller.rb', line 119

def preview
  begin
    page = ComatosePage.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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/comatose_admin_controller.rb', line 70

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| ComatosePage.update(id, :position => idx) }
    expire_cms_page ComatosePage.find(params[:id])
    render :text=>'Updated sort order', :layout=>false
  else
    @page = ComatosePage.find params[:id]
    if params.has_key? :cmd
      @target = ComatosePage.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…



97
98
99
100
101
102
103
104
# File 'lib/comatose_admin_controller.rb', line 97

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

#versionsObject

Allows comparing between two versions of a page’s content



90
91
92
93
94
# File 'lib/comatose_admin_controller.rb', line 90

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