Module: Nitro::Scaffold

Extended by:
ClassMethods
Defined in:
lib/nitro/scaffold.rb,
lib/nitro/scaffold/model.rb,
lib/part/admin/og/customize.rb,
lib/nitro/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.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Methods included from ClassMethods

enchant, enchant_class

Class Method Details

.controller(*models) ⇒ Object

Automatically creates a scaffold controller to handle the given models.



8
9
10
11
12
# File 'lib/nitro/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. ++



138
139
140
141
142
143
144
145
146
# File 'lib/nitro/scaffold/model.rb', line 138

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. ++



122
123
124
125
126
127
128
129
130
# File 'lib/nitro/scaffold/model.rb', line 122

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/nitro/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. ++



41
42
43
44
45
46
47
48
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
# File 'lib/nitro/scaffold/model.rb', line 41

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) urls.
  
  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

.included(base) ⇒ Object



127
128
129
# File 'lib/nitro/scaffold.rb', line 127

def self.included base
  base.extend ClassMethods  
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. ++



28
29
30
31
32
# File 'lib/nitro/scaffold/model.rb', line 28

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