Skip to content

Commit

Permalink
Added import and export of pages in admin controller
Browse files Browse the repository at this point in the history
  • Loading branch information
staugaard committed Oct 10, 2008
1 parent 1683f91 commit 3ea8e5e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
- version: 2.0.3 (alpha)
- staugaard: Added import and export of pages in admin controller

- version: 2.0.2 (alpha)
- staugaard: Added "unloadable" to ComatoseController (problems with ActsAsAuthenticated-integration similar to http://tinyurl.com/6caz5r)

- version: 2.0.1 (alpha)
- Merged changes from andreas (http://github.com/andreas)...
- andreas: Fixed comatose:admin:customize raketask (seems plugin_path was wrong, and made use of mkdir_p to avoid "File exists"-errors.)
Expand Down
4 changes: 2 additions & 2 deletions comatose.gemspec
@@ -1,8 +1,8 @@
# Generated on Tue May 20 20:13:12 -0500 2008
Gem::Specification.new do |s|
s.name = "comatose"
s.version = "2.0.2"
s.date = "2008-09-15" # 2008-05-20
s.version = "2.0.3"
s.date = "2008-10-10" # 2008-05-20
s.summary = "Micro CMS designed for being embedded into existing Rails applications"
s.email = "matt@elucidata.net"
s.rubyforge_project = 'comatose'
Expand Down
37 changes: 37 additions & 0 deletions lib/comatose_admin_controller.rb
Expand Up @@ -140,6 +140,16 @@ def generate_page_cache
redirect_to :controller=>self.controller_name, :action=>'index'
end

def export
send_data(page_to_hash(ComatosePage.root).to_yaml, :disposition => 'inline', :type => 'text/yaml', :filename => "comatose-pages.yml")
end

def import
data = YAML::load(params[:import_file])
hash_to_page_tree(data, ComatosePage.root)
flash[:notice] = "Pages Imported Successfully"
redirect_to :controller=>self.controller_name, :action=>'index'
end

protected

Expand Down Expand Up @@ -345,5 +355,32 @@ def runtime_mode=(mode)
end
end

private

def page_to_hash(page)
data = page.attributes.clone
# Pull out the specific, or unnecessary fields
%w(id parent_id updated_on author position version created_on full_path).each {|key| data.delete(key)}
if !page.children.empty?
data['children'] = []
page.children.each do |child|
data['children'] << page_to_hash(child)
end
end
data
end

def hash_to_page_tree(hsh, page)
child_ary = hsh.delete 'children'
page.update_attributes(hsh)
page.save
child_ary.each do |child_hsh|
if child_pg = page.children.find_by_slug( child_hsh['slug'] )
hash_to_page_tree( child_hsh, child_pg )
else
hash_to_page_tree( child_hsh, page.children.create )
end
end if child_ary
end

end
7 changes: 7 additions & 0 deletions views/comatose_admin/index.html.erb
@@ -1,4 +1,11 @@
<div class="action">
<% form_tag(url_for(:action => 'import'), :multipart => true, :style => 'display: none;', :id => 'import_form') do %>
<%= file_field_tag 'import_file' %>
<%= submit_tag 'Import' %>
<% end %>
<%= link_to 'Import', '#', :id => 'import_link2', :onclick => "$('import_form').setStyle({display: 'inline'}); this.hide();" %>
<%= link_to 'Export', :controller=>controller.controller_name, :action=>'export' %>
<%= link_to 'Clear Page Cache', :controller=>controller.controller_name, :action=>'expire_page_cache' %>
</div>

Expand Down

0 comments on commit 3ea8e5e

Please sign in to comment.