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



307
308
309
310
311
312
313
314
315
# File 'lib/comatose_admin_controller.rb', line 307

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…



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/comatose_admin_controller.rb', line 279

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



270
271
272
273
274
275
276
# File 'lib/comatose_admin_controller.rb', line 270

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…



299
300
301
302
303
304
305
# File 'lib/comatose_admin_controller.rb', line 299

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

.runtime_modeObject



317
318
319
# File 'lib/comatose_admin_controller.rb', line 317

def runtime_mode
  @@runtime_mode ||= :unknown
end

.runtime_mode=(mode) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/comatose_admin_controller.rb', line 321

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/comatose_admin_controller.rb', line 88

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
# 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
      expire_cms_page @page
      expire_cms_fragment @page
      flash[:notice] = "Saved changes to '#{@page.title}'"
      redirect_to :controller=>self.controller_name, :action=>'index'
    end
  end
end

#expire_page_cacheObject

Expires the entire page cache



118
119
120
121
122
123
# File 'lib/comatose_admin_controller.rb', line 118

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



143
144
145
146
147
148
149
150
# File 'lib/comatose_admin_controller.rb', line 143

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/comatose_admin_controller.rb', line 128

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



152
153
154
155
156
157
158
159
160
161
# File 'lib/comatose_admin_controller.rb', line 152

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)



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/comatose_admin_controller.rb', line 36

def new
  @root_pages = [fetch_root_page].flatten
  if request.post?
    @page = ComatosePage.new params[:page]
    @page.author = fetch_author_name
    if @page.save
      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…



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/comatose_admin_controller.rb', line 100

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



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

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…



78
79
80
81
82
83
84
85
# File 'lib/comatose_admin_controller.rb', line 78

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



71
72
73
74
75
# File 'lib/comatose_admin_controller.rb', line 71

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