Class: RivetCms::Seeds::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rivet_cms/seeds.rb

Overview

Applies a template's declarations to the database for one organization.

Instance Method Summary collapse

Constructor Details

#initialize(organization) ⇒ Builder

Returns a new instance of Builder.



97
98
99
100
101
102
103
104
105
# File 'lib/rivet_cms/seeds.rb', line 97

def initialize(organization)
  @organization = organization
  @categories = []
  @components = []
  @content_types = []
  @category_index = {}
  @component_index = {}
  @content_type_index = {}
end

Instance Method Details

#category(name, slug: nil, system: false, position: 0) ⇒ Object



107
108
109
# File 'lib/rivet_cms/seeds.rb', line 107

def category(name, slug: nil, system: false, position: 0)
  @categories << { name: name, slug: slug || name.parameterize, system: system, position: position }
end

#commitObject

Shells first (so references and component fields can resolve regardless of declaration order), then fields.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rivet_cms/seeds.rb', line 125

def commit
  RivetCms::ApplicationRecord.transaction do
    @categories.each { |attrs| upsert_category(attrs) }
    component_records = @components.map { |attrs| upsert_component(attrs) }
    content_type_records = @content_types.map { |attrs| upsert_content_type(attrs) }

    @components.each_with_index { |attrs, i| build_fields(component_records[i], attrs[:fields]) }
    @content_types.each_with_index { |attrs, i| build_fields(content_type_records[i], attrs[:fields]) if content_type_records[i] }
  end
  self
end

#component(name, category:, slug: nil, description: nil, &block) ⇒ Object



111
112
113
114
115
# File 'lib/rivet_cms/seeds.rb', line 111

def component(name, category:, slug: nil, description: nil, &block)
  fields = FieldSet.new
  fields.instance_eval(&block) if block
  @components << { name: name, slug: slug || name.parameterize, category: category, description: description, fields: fields.definitions }
end

#content_type(name, slug:, description: nil, single: false, &block) ⇒ Object



117
118
119
120
121
# File 'lib/rivet_cms/seeds.rb', line 117

def content_type(name, slug:, description: nil, single: false, &block)
  fields = FieldSet.new
  fields.instance_eval(&block) if block
  @content_types << { name: name, slug: slug, description: description, single: single, fields: fields.definitions }
end