Class: PageBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/page_builder.rb

Overview

# PageBuilder

PageBuilder is a simple DSL for building pages, for use in ie. seeds.rb

Example:

PageBuilder.build(User.first) do
  page "Home", template: "home"
  page "Products" do
    page "Product 1"
    page "Product 2"
  end
  page "Contact", unique_name: "contact"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, locale: nil, parent: nil) ⇒ PageBuilder

Returns a new instance of PageBuilder.



27
28
29
30
31
# File 'app/models/page_builder.rb', line 27

def initialize(user, locale: nil, parent: nil)
  @user = user
  @locale = locale || I18n.default_locale
  @parent = parent
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



18
19
20
# File 'app/models/page_builder.rb', line 18

def locale
  @locale
end

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'app/models/page_builder.rb', line 18

def parent
  @parent
end

#userObject (readonly)

Returns the value of attribute user.



18
19
20
# File 'app/models/page_builder.rb', line 18

def user
  @user
end

Class Method Details

.build(user, locale: nil, parent: nil, &block) ⇒ Object



21
22
23
24
# File 'app/models/page_builder.rb', line 21

def build(user, locale: nil, parent: nil, &block)
  new(user, locale: locale, parent: parent)
    .run(&block)
end

Instance Method Details

#page(name, options = {}, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/page_builder.rb', line 33

def page(name, options = {}, &block)
  page = Page.create(
    { name: name }.merge(default_options).merge(options)
  )
  if block_given?
    self.class
        .new(user, locale: locale, parent: page)
        .run(&block)
  end
  page
end

#run(&block) ⇒ Object



45
46
47
# File 'app/models/page_builder.rb', line 45

def run(&block)
  instance_eval(&block)
end