Module: Raw::Scaffold

Defined in:
lib/raw/scaffold.rb,
lib/raw/scaffold/model.rb,
lib/raw/scaffold/controller.rb

Overview

Scaffolding is one facet of Nitro’s Rapid Application Develpoment (RAD) features. The scaffolder automatically generates common code for managed object and their controllers.

Class Method Summary collapse

Class Method Details

.all_modelsObject

Enchant all models.



16
17
18
19
20
# File 'lib/raw/scaffold/model.rb', line 16

def self.all_models
  if Scaffold.enchant_all_models
    self.model(*Og.manager.managed_classes)
  end
end

.controller(*models) ⇒ Object

Automatically creates a scaffold controller to handle the given models.



8
9
10
11
12
# File 'lib/raw/scaffold/controller.rb', line 8

def self.controller(*models)
  for m in models
    scaffold_controller(m)
  end
end

.define_class_method(klass, meth, body, force = false) ⇒ Object

– This helper defines an class method for the scaffolded class. The method is only defined if the klass does not already respond to it. ++



145
146
147
148
149
150
151
152
153
# File 'lib/raw/scaffold/model.rb', line 145

def self.define_class_method(klass, meth, body, force = false)
  if force or (!klass.respond_to? meth.to_s)
    klass.module_eval %{
      def self.#{meth}
        #{body}
      end
    }
  end
end

.define_instance_method(klass, meth, body, force = false) ⇒ Object

– This helper defines an instance method for the scaffolded class. The method is only defined if the klass does not already respond to it. ++



129
130
131
132
133
134
135
136
137
# File 'lib/raw/scaffold/model.rb', line 129

def self.define_instance_method(klass, meth, body, force = false)
  if force or (!klass.instance_methods.include? meth.to_s)
    klass.module_eval %{
      def #{meth}
        #{body}
      end
    }
  end
end

.enchant_controller(model) ⇒ Object



14
15
16
17
18
# File 'lib/raw/scaffold/controller.rb', line 14

def self.enchant_controller(model)
  eval %{
  
  }  
end

.enchant_model(klass) ⇒ Object

– Actually enchant the given class. Override this method to customize for your application. The string calculation code is deliberatly dynamic to work with Ruby’s OO features. ++



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/raw/scaffold/model.rb', line 49

def self.enchant_model(klass)
  # Find the controller that handles this model. Unless no
  # controller annotation is defined, try to find a controller
  # of the form: Model::Controller. Some examples:
  #
  # class Ticket
  #   ann :self, :controller => SpecialTicketController
  #   ..
  # end
  #
  # or try to find a Ticket::Controller class. 

  controller = klass.ann(:self, :controller) || klass.constant('Controller')

  # If the class defines a text_key use it to create more
  # readable (and SEO friendly) URIs.
  
  key = klass.ann(:self, :text_key) || 'oid' 

  # to_s
        
  if klass.instance_methods.include? 'title'
    define_instance_method klass, :to_s, %{ title }, force = true      
  elsif klass.instance_methods.include? 'name'
    define_instance_method klass, :to_s, %{ name }, force = true      
  end

  # to_href
  # ex: /articles/23

  define_instance_method klass, :to_href, %{
    "\#{#{controller}.mount_path}/read/\#{#{key}}".squeeze('/')
  }

  # to_link
  # ex: <a href="/articles/23">The article's title</a>
  
  define_instance_method klass, :to_link, %{
    %|<a href="\#{to_href}">\#{to_s}</a>|
  }

  # to_link
  # ex: <a href="/articles/23">The article's title</a>
  
  define_class_method klass, :controller, %{
    #{controller}
  }
                
  if defined? OgAdminController
    self.extend(OgAdminHelper)

    # to_edit_href
    # ex: admin/update/Article/23

    define_instance_method klass, :to_edit_href, %{
      "\#{OgAdminController.mount_path}/update/#{class_to_name(klass)}/\#{oid}"
    }

    # to_admin_href
    # ex: admin/list/Article

    define_instance_method klass, :to_admin_href, %{
      "\#{AdminController.mount_path}/list/#{class_to_name(klass)}"
    }
                  
    # to_admin_href
    # ex: admin/list/Article

    define_class_method klass, :to_admin_href, %{
      "\#{AdminController.mount_path}/list/#{class_to_name(klass)}"
    }
  end
end

.model(*classes) ⇒ Object

‘Enchant’ a model class (typically a managed class, entity). A collection of useful methods are magically added to the class and/or the class instances.

  • to_s

  • to_href

  • to_link

  • to_edit_href

  • to_admin_href

– to_xxx is used instead of xxx in an attempt to avoid colisions with user defined methods. ++



36
37
38
39
40
# File 'lib/raw/scaffold/model.rb', line 36

def self.model(*classes)
  for c in classes
    enchant_model c
  end
end