Module: Pageflow::Seeds

Defined in:
lib/pageflow/seeds.rb

Overview

Provides a DSL for seeding the database with Pageflow models. Include this module in your ‘db/seeds.rb` file.

Examples:


# db/seeds.rb
include Pageflow::Seeds

default_password 'supersecret'

(name: 'example') do ||
  user(account: ,
       email: '[email protected]',
       role: 'admin',
       first_name: 'John',
       last_name: 'Doe')
end

Constant Summary collapse

DEFAULT_USER_PASSWORD =
'!Pass123'.freeze

Instance Method Summary collapse

Instance Method Details

#account(attributes) {|account| ... } ⇒ Account

Create an Account with a default Pageflow::Site if no account by that name exists.

Options Hash (attributes):

  • :name (String)

    required

Yields:

  • (account)

    a block to be called before the account is saved



29
30
31
32
33
34
35
36
37
38
# File 'lib/pageflow/seeds.rb', line 29

def (attributes)
  .find_or_create_by!(attributes.slice(:name)) do ||
    .attributes = attributes.reverse_merge(name: 'Pageflow')

    build_default_site_for()

    ()
    yield() if block_given?
  end
end

#build_default_site_for(account, attributes = {}) {|site| ... } ⇒ Site

Build a default Pageflow::Site for an Account. To be used inside a block passed to #account.

Examples:


(name: 'example') do ||
  build_default_site_for() do |site|
    site.theme_name = 'mdr'
  end
end

Yields:

  • (site)

    a block which is passed the newly built site



55
56
57
58
59
60
61
62
63
64
# File 'lib/pageflow/seeds.rb', line 55

def build_default_site_for(, attributes = {}, &)
  default_attributes = {
    imprint_link_label: 'Impressum',
    imprint_link_url: 'http://example.com/impressum.html',
    copyright_link_label: '© Pageflow 2014',
    copyright_link_url: 'http://www.example.com/copyright.html'
  }

  .build_default_site(default_attributes.merge(attributes), &)
end

#default_user_password(password = nil) ⇒ Object

Set the default password to use for created users. Call before using #user.



92
93
94
95
96
97
98
# File 'lib/pageflow/seeds.rb', line 92

def default_user_password(password = nil)
  if password
    @default_user_password = password
  else
    @default_user_password || DEFAULT_USER_PASSWORD
  end
end

#membership(attributes) ⇒ Membership

Create a Membership for the given user and entity. Create a Membership for the corresponding account first if a Membership on an entry is to be created and no Membership on the entry’s account exists yet.

:entry and :account are lower-priority aliases for :entity

Options Hash (attributes):

  • :user (User)

    required

  • :entity (Entity)

    required



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pageflow/seeds.rb', line 144

def membership(attributes)
  if (attributes[:entry].present? && attributes[:entity].present?) ||
     (attributes[:account].present? && attributes[:entity].present?)
    say_attribute_precedence(':entity', ':entry and :account')
  end
   attributes unless attributes[:entity].present?

  if attributes[:entity].is_a?(Entry) || attributes[:entry].present?
    entry = attributes[:entity] || attributes[:entry]
    unless attributes[:user].accounts.include?(entry.)
      Membership.find_or_create_by!(entity: entry.,
                                    user: attributes[:user],
                                    role: :member) do |membership|
        say_creating_membership(membership)
      end
    end
  end

  Membership.find_or_create_by!(attributes) do |membership|
    say_creating_membership(membership)
  end
end

#sample_entry(attributes) {|entry| ... } ⇒ Entry

Create a sample Entry with some chapter, pages, and optional text if no entry with that title exists in the given account.

Options Hash (attributes):

  • :account (Account)

    required

  • :title (String)

    required

  • :text (String)

    optional

Yields:

  • (entry)

    a block to be called before the entry is saved



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pageflow/seeds.rb', line 109

def sample_entry(attributes)
  entry = Entry.where(attributes.slice(:account, :title)).first
  page_text = attributes.delete(:text) { |_| '' }

  if entry.nil?
    entry = Entry.create!(attributes) do |created_entry|
      created_entry.site = attributes.fetch(:account).default_site

      say_creating_entry(created_entry)
      yield(created_entry) if block_given?
    end

    storyline = entry.draft.storylines.first
    chapter = storyline.chapters.create!(title: 'Chapter 1', position: 0)
    chapter.pages.create!(template: 'background_image',
                          configuration: {text: page_text})
    chapter.pages.create!(template: 'background_image',
                          configuration: {text: page_text})

    chapter = storyline.chapters.create!(title: 'Chapter 2', position: 1)
    chapter.pages.create!(template: 'video', configuration: {text: page_text})
  end

  entry
end

#user(attributes) {|user| ... } ⇒ User

Create a User if none with the given email exists yet.

Options Hash (attributes):

  • :email (String)

    required

Yields:

  • (user)

    a block to be called before the user is saved



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

def user(attributes)
  default_attributes = {
    password: default_user_password,
    first_name: 'Elliot',
    last_name: 'Example'
  }

  User.find_or_create_by!(attributes.slice(:email)) do |user|
    user.attributes = default_attributes.merge(attributes)
    user.password_confirmation = user.password

    say_creating_user(user)
    yield(user) if block_given?
  end
end