Class: Trestle::Admin::Builder

Inherits:
Builder
  • Object
show all
Defined in:
lib/trestle/admin/builder.rb

Direct Known Subclasses

Resource::Builder

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Builder

#build, build, target

Constructor Details

#initialize(admin) ⇒ Builder

Returns a new instance of Builder.


14
15
16
# File 'lib/trestle/admin/builder.rb', line 14

def initialize(admin)
  @admin, @controller = admin, admin.const_get(:AdminController)
end

Class Method Details

.create(name, options = {}, &block) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/trestle/admin/builder.rb', line 18

def self.create(name, options={}, &block)
  # Create admin subclass
  admin = Class.new(admin_class)
  admin.options = options

  # Define a constant based on the admin name
  scope = options[:scope] || Object
  scope.const_set("#{name.to_s.camelize}Admin", admin)

  # Define admin controller class
  # This is done using class_eval rather than Class.new so that the full
  # class name and parent chain is set when Rails' inherited hooks are called.
  admin.class_eval("class AdminController < #{controller.call.name}; end")

  # Set a reference on the controller class to the admin class
  controller = admin.const_get(:AdminController)
  controller.instance_variable_set("@admin", admin)

  admin.build(&block)
  admin.validate!

  admin
end

Instance Method Details

#admin(&block) ⇒ Object


67
68
69
70
# File 'lib/trestle/admin/builder.rb', line 67

def admin(&block)
  @admin.instance_eval(&block) if block_given?
  @admin
end

81
82
83
84
85
86
87
88
89
# File 'lib/trestle/admin/builder.rb', line 81

def breadcrumb(label=nil, path=nil, &block)
  if block_given?
    @admin.breadcrumb = block
  elsif label
    @admin.breadcrumb = -> { Breadcrumb.new(label, path) }
  else
    @admin.breadcrumb = -> { false }
  end
end

#controller(&block) ⇒ Object


72
73
74
75
# File 'lib/trestle/admin/builder.rb', line 72

def controller(&block)
  @controller.class_eval(&block) if block_given?
  @controller
end

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


55
56
57
58
59
60
61
# File 'lib/trestle/admin/builder.rb', line 55

def form(options={}, &block)
  if block_given?
    admin.form = Form.new(options, &block)
  else
    admin.form = Form::Automatic.new(admin, options)
  end
end

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


63
64
65
# File 'lib/trestle/admin/builder.rb', line 63

def hook(name, options={}, &block)
  admin.hooks.append(name, options, &block)
end

42
43
44
45
46
47
48
# File 'lib/trestle/admin/builder.rb', line 42

def menu(*args, &block)
  if block_given?
    admin.menu = Navigation::Block.new(admin, &block)
  else
    menu { item(*args) }
  end
end

#routes(&block) ⇒ Object


77
78
79
# File 'lib/trestle/admin/builder.rb', line 77

def routes(&block)
  @admin.additional_routes << block
end

#table(name_or_options = {}, options = {}, &block) ⇒ Object


50
51
52
53
# File 'lib/trestle/admin/builder.rb', line 50

def table(name_or_options={}, options={}, &block)
  name, options = normalize_table_options(name_or_options, options)
  admin.tables[name] = Table::Builder.build(options, &block)
end