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'

Instance Method Summary collapse

Instance Method Details

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

Create an Account with a default Theming if no account by that name exists.

Parameters:

  • attributes (Hash)

    attributes to override defaults

Options Hash (attributes):

  • :name (String)

    required

Yields:

  • (account)

    a block to be called before the account is saved

Returns:

  • (Account)

    newly created account



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

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

    build_default_theming_for()

    ()
    yield() if block_given?
  end
end

#build_default_theming_for(account, attributes = {}) {|theming| ... } ⇒ Theming

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

Examples:


(name: 'example') do ||
  build_default_theming_for() do |theming|
    theming.theme_name = 'mdr'
  end
end

Parameters:

  • account (Account)

    the account to build a default themeing for

  • attributes (Hash) (defaults to: {})

    further attributes to override defaults

Yields:

  • (theming)

    a block which is passed the newly built theming

Returns:

  • (Theming)

    newly built theming



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

def build_default_theming_for(, attributes = {}, &block)
  default_attributes = {
    theme_name: Pageflow.config.themes.names.first,

    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_theming(default_attributes.merge(attributes), &block)
end

#default_user_password(password = nil) ⇒ Object

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

Parameters:

  • password (String) (defaults to: nil)


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

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 entry.

Parameters:

  • attributes (Hash)

    attributes to override defaults

Options Hash (attributes):

  • :user (User)

    required

  • :entry (Entry)

    required

Returns:



138
139
140
141
142
# File 'lib/pageflow/seeds.rb', line 138

def membership(attributes)
  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 and pages if no entry with that title exists in the given account.

Parameters:

  • attributes (Hash)

    attributes to override defaults

Options Hash (attributes):

  • :account (Account)

    required

  • :title (title)

    required

Yields:

  • (entry)

    a block to be called before the entry is saved

Returns:

  • (Entry)

    newly created entry



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

def sample_entry(attributes)
  entry = Entry.where(attributes.slice(:account, :title)).first

  if entry.nil?
    entry = Entry.create!(attributes) do |entry|
      entry.theming = attributes.fetch(:account).default_theming

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

    chapter = entry.draft.chapters.create!(title: 'Kapitel 1')
    chapter.pages.create!(template: 'background_image')
    chapter.pages.create!(template: 'background_image')

    chapter = entry.draft.chapters.create!(title: 'Kapitel 2')
    chapter.pages.create!(template: 'video')
  end

  entry
end

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

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

Parameters:

  • attributes (Hash)

    attributes to override defaults

Options Hash (attributes):

  • :email (String)

    required

Yields:

  • (user)

    a block to be called before the user is saved

Returns:

  • (User)

    newly created user



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

def user(attributes, &block)
  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