2

我真正想做的是设置一个种子文件,其中包含每次部署我的应用程序时可以导入的基本用户和页面数据。因为在传统的 Rails 模型目录中找不到 Refinery 模型,所以我无法使用SeedDump gem

从现有 Refinery CMS 应用程序导出数据的最简单方法是什么?

4

1 回答 1

3

我能够从我现有的开发应用程序中手动生成一个基本的种子文件。在下面的示例中,我构建了一个种子文件:

  1. 创建管理员超级用户
  2. 更新主页以使用自定义布局和视图模板
  3. 使用自定义模板将默认关于页面替换为新页面

我是这样做的:

首先,我使用 Refinery 模型进入 rails 控制台查找相关记录:

rails console

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

然后,使用在控制台中查找的记录作为参考,我将必要的字段复制粘贴到我的种子文件中。这是我的种子文件:

# 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
  }
])

最后,我运行rake db:setup了新的种子文件。

现在,当我需要设置我的应用程序的新实例时,我可以从我的存储库中克隆并运行rake db:setup.

于 2013-07-30T17:52:19.993 回答