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.


25
26
27
28
29
# File 'app/models/page_builder.rb', line 25

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


16
17
18
# File 'app/models/page_builder.rb', line 16

def locale
  @locale
end

#parentObject (readonly)

Returns the value of attribute parent


16
17
18
# File 'app/models/page_builder.rb', line 16

def parent
  @parent
end

#userObject (readonly)

Returns the value of attribute user


16
17
18
# File 'app/models/page_builder.rb', line 16

def user
  @user
end

Class Method Details

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


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

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


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

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


43
44
45
# File 'app/models/page_builder.rb', line 43

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