Search code examples
ruby-on-rails-3refinerycms

In Refinery CMS, how can I export data from one instance of my application to another?


What I'm really trying to do is set up a seed file with basic user and page data that can be imported each time I deploy my app. Because the Refinery models aren't found in the conventional Rails model directory, I was unable to use the SeedDump gem.

What's the simplest way to export data from an existing Refinery CMS application?


Solution

  • I was able to generate a basic seed file from my existing development application by hand. In the example below, I build a seed file that:

    1. Creates an admin superuser
    2. Updates the home page to use custom layout and view templates
    3. Replaces the default about page with a new one using a custom template

    Here's how I did it:

    First, I dipped into the rails console to look up the relevant records using the Refinery models:

    rails console
    
    :001 > Refinery::Page.find_by_slug('about')
    => #<Refinery::Page id: 4, ... >
    :002 > Refinery::Page.find_by_slug('home')
    :003 > Refinery::PagePart.all
    

    Then, using the records looked up in the console as references, I copy-pasted the necessary fields into my seed file. Here's my seed file:

    # This file should contain all the record creation needed to seed the database with its default values.
    # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
    #
    # Examples:
    #
    #   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
    #   Mayor.create(name: 'Emanuel', city: cities.first)
    
    # Added by Refinery CMS Pages extension
    Refinery::Pages::Engine.load_seed
    
    #
    # Custom Changes
    #
    # Create User
    Refinery::User.create!(
      username: "admin",
      password: "admin",
      password_confirmation: "admin",
      email: "admin@mysite.com"
    )
    admin_user = Refinery::User.find_by_username("admin")
    
    # Add necessary roles
    # https://groups.google.com/d/msg/refinery-cms/akI74wnviFs/j613apqJdvgJ
    admin_user.add_role :refinery
    admin_user.add_role :superuser
    
    # Update Home Page Template
    home_page = Refinery::Page.find_by_slug('home')
    home_page.layout_template = "home"
    home_page.view_template = "home"
    home_page.save!
    
    # Replace the About Page
    # Delete existing page
    old_about_page = Refinery::Page.find_by_slug('about')
    old_about_page.destroy
    
    # Add new page
    Refinery::Page.create!(
      title: "About Us",
      custom_slug: "about",
      layout_template: "article",
      view_template: "article"
    )
    about_page = Refinery::Page.find_by_slug('about')
    
    # Then add image
    img_path = Rails.root.join('app/assets/images/cms_contact_us.jpg')
    Refinery::Image.create(image: File.new(img_path))
    contact_us_image = Refinery::Image.last
    
    # Finally add page-parts
    Refinery::PagePart.create!([
      { refinery_page_id: about_page.id,
        title: "Headline",
        body: "<p>About Us</p>"
      },
      { refinery_page_id: about_page.id,
        title: "Epigraph",
        body: "<p>Impossible is nothing.</p>"
      },
      { refinery_page_id: about_page.id,
        title: "Body",
        body: "<h2>About Us</h2>\r\n<h3>Our Mission</h3>\r\n<p>...</p>"
      },
      { refinery_page_id: about_page.id,
        title: "Image",
        body: "<p><img rel=\"225x255\" alt=\"Contact Us\" title=\"Contact Us\" src=\"%s\" height=\"140\" width=\"600\" /></p>" % contact_us_image.url
      }
    ])
    

    Finally, I ran rake db:setup with the new seed file.

    Now when I need to set up a new instance of my application, I can just clone from my repository and run rake db:setup.