lightrail 0.0.1 → 0.99.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.rspec +4 -0
- data/.travis.yml +11 -0
- data/CHANGES.md +8 -0
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/README.md +205 -0
- data/Rakefile +5 -0
- data/bin/lightrail +2 -1
- data/lib/lightrail.rb +2 -0
- data/lib/lightrail/action_controller/metal.rb +0 -2
- data/lib/lightrail/cli.rb +16 -0
- data/lib/lightrail/commands/application.rb +26 -0
- data/lib/lightrail/generators.rb +323 -0
- data/lib/lightrail/generators/app_base.rb +281 -0
- data/lib/lightrail/generators/app_generator.rb +299 -0
- data/lib/lightrail/generators/base.rb +378 -0
- data/lib/lightrail/generators/templates/Gemfile +25 -0
- data/lib/lightrail/generators/templates/README +259 -0
- data/lib/lightrail/generators/templates/Rakefile +7 -0
- data/lib/lightrail/generators/templates/app/assets/images/rails.png +0 -0
- data/lib/lightrail/generators/templates/app/assets/javascripts/application.js.tt +17 -0
- data/lib/lightrail/generators/templates/app/assets/stylesheets/application.css +13 -0
- data/lib/lightrail/generators/templates/app/controllers/application_controller.rb +3 -0
- data/lib/lightrail/generators/templates/app/helpers/application_helper.rb +2 -0
- data/lib/lightrail/generators/templates/app/mailers/.empty_directory +0 -0
- data/lib/lightrail/generators/templates/app/models/.empty_directory +0 -0
- data/lib/lightrail/generators/templates/app/views/layouts/application.html.erb.tt +14 -0
- data/lib/lightrail/generators/templates/config.ru +4 -0
- data/lib/lightrail/generators/templates/config/application.rb +67 -0
- data/lib/lightrail/generators/templates/config/boot.rb +6 -0
- data/lib/lightrail/generators/templates/config/databases/frontbase.yml +31 -0
- data/lib/lightrail/generators/templates/config/databases/ibm_db.yml +86 -0
- data/lib/lightrail/generators/templates/config/databases/jdbc.yml +62 -0
- data/lib/lightrail/generators/templates/config/databases/jdbcmysql.yml +33 -0
- data/lib/lightrail/generators/templates/config/databases/jdbcpostgresql.yml +43 -0
- data/lib/lightrail/generators/templates/config/databases/jdbcsqlite3.yml +20 -0
- data/lib/lightrail/generators/templates/config/databases/mysql.yml +51 -0
- data/lib/lightrail/generators/templates/config/databases/oracle.yml +39 -0
- data/lib/lightrail/generators/templates/config/databases/postgresql.yml +55 -0
- data/lib/lightrail/generators/templates/config/databases/sqlite3.yml +25 -0
- data/lib/lightrail/generators/templates/config/environment.rb +5 -0
- data/lib/lightrail/generators/templates/config/environments/development.rb.tt +38 -0
- data/lib/lightrail/generators/templates/config/environments/production.rb.tt +76 -0
- data/lib/lightrail/generators/templates/config/environments/test.rb.tt +36 -0
- data/lib/lightrail/generators/templates/config/initializers/backtrace_silencers.rb +7 -0
- data/lib/lightrail/generators/templates/config/initializers/inflections.rb +15 -0
- data/lib/lightrail/generators/templates/config/initializers/mime_types.rb +5 -0
- data/lib/lightrail/generators/templates/config/initializers/secret_token.rb.tt +7 -0
- data/lib/lightrail/generators/templates/config/initializers/session_store.rb.tt +8 -0
- data/lib/lightrail/generators/templates/config/initializers/wrap_parameters.rb.tt +16 -0
- data/lib/lightrail/generators/templates/config/locales/en.yml +5 -0
- data/lib/lightrail/generators/templates/config/routes.rb +58 -0
- data/lib/lightrail/generators/templates/db/seeds.rb.tt +7 -0
- data/lib/lightrail/generators/templates/gitignore +16 -0
- data/lib/lightrail/generators/templates/public/404.html +26 -0
- data/lib/lightrail/generators/templates/public/422.html +26 -0
- data/lib/lightrail/generators/templates/public/500.html +25 -0
- data/lib/lightrail/generators/templates/public/favicon.ico +0 -0
- data/lib/lightrail/generators/templates/public/index.html +241 -0
- data/lib/lightrail/generators/templates/public/robots.txt +5 -0
- data/lib/lightrail/generators/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/lightrail/generators/templates/script/rails +5 -0
- data/lib/lightrail/generators/templates/test/fixtures/.empty_directory +0 -0
- data/lib/lightrail/generators/templates/test/functional/.empty_directory +0 -0
- data/lib/lightrail/generators/templates/test/integration/.empty_directory +0 -0
- data/lib/lightrail/generators/templates/test/performance/browsing_test.rb +12 -0
- data/lib/lightrail/generators/templates/test/test_helper.rb +15 -0
- data/lib/lightrail/generators/templates/test/unit/.empty_directory +0 -0
- data/lib/lightrail/version.rb +1 -1
- data/lightrail.gemspec +23 -0
- data/logo.png +0 -0
- data/spec/lightrail/action_controller/metal_spec.rb +8 -0
- data/spec/spec_helper.rb +1 -0
- data/tasks/rspec.task +7 -0
- metadata +105 -13
- data/lib/lightrail/action_controller/param.rb +0 -12
- data/lib/lightrail/core_ext/regexp.rb +0 -7
- data/lib/lightrail/encryptor.rb +0 -62
@@ -0,0 +1,378 @@
|
|
1
|
+
begin
|
2
|
+
require 'thor/group'
|
3
|
+
rescue LoadError
|
4
|
+
puts "Thor is not available.\nIf you ran this command from a git checkout " \
|
5
|
+
"of Lightrail, please make sure thor is installed,\nand run this command " \
|
6
|
+
"as `ruby #{$0} #{(ARGV | ['--dev']).join(" ")}`"
|
7
|
+
exit
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rails/generators/actions'
|
11
|
+
require 'active_support/core_ext/object/inclusion'
|
12
|
+
|
13
|
+
module Lightrail
|
14
|
+
module Generators
|
15
|
+
class Error < Thor::Error
|
16
|
+
end
|
17
|
+
|
18
|
+
class Base < Thor::Group
|
19
|
+
include Thor::Actions
|
20
|
+
include Rails::Generators::Actions
|
21
|
+
|
22
|
+
add_runtime_options!
|
23
|
+
|
24
|
+
# Returns the source root for this generator using default_source_root as default.
|
25
|
+
def self.source_root(path=nil)
|
26
|
+
@_source_root = path if path
|
27
|
+
@_source_root ||= default_source_root
|
28
|
+
end
|
29
|
+
|
30
|
+
# Tries to get the description from a USAGE file one folder above the source
|
31
|
+
# root otherwise uses a default description.
|
32
|
+
def self.desc(description=nil)
|
33
|
+
return super if description
|
34
|
+
usage = source_root && File.expand_path("../USAGE", source_root)
|
35
|
+
|
36
|
+
@desc ||= if usage && File.exist?(usage)
|
37
|
+
ERB.new(File.read(usage)).result(binding)
|
38
|
+
else
|
39
|
+
"Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Convenience method to get the namespace from the class name. It's the
|
44
|
+
# same as Thor default except that the Generator at the end of the class
|
45
|
+
# is removed.
|
46
|
+
def self.namespace(name=nil)
|
47
|
+
return super if name
|
48
|
+
@namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':')
|
49
|
+
end
|
50
|
+
|
51
|
+
# Convenience method to hide this generator from the available ones when
|
52
|
+
# running rails generator command.
|
53
|
+
def self.hide!
|
54
|
+
Lightrail::Generators.hide_namespace self.namespace
|
55
|
+
end
|
56
|
+
|
57
|
+
# Invoke a generator based on the value supplied by the user to the
|
58
|
+
# given option named "name". A class option is created when this method
|
59
|
+
# is invoked and you can set a hash to customize it.
|
60
|
+
#
|
61
|
+
# ==== Examples
|
62
|
+
#
|
63
|
+
# module Lightrail::Generators
|
64
|
+
# class ControllerGenerator < Base
|
65
|
+
# hook_for :test_framework, :aliases => "-t"
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# The example above will create a test framework option and will invoke
|
70
|
+
# a generator based on the user supplied value.
|
71
|
+
#
|
72
|
+
# For example, if the user invoke the controller generator as:
|
73
|
+
#
|
74
|
+
# rails generate controller Account --test-framework=test_unit
|
75
|
+
#
|
76
|
+
# The controller generator will then try to invoke the following generators:
|
77
|
+
#
|
78
|
+
# "rails:test_unit", "test_unit:controller", "test_unit"
|
79
|
+
#
|
80
|
+
# Notice that "rails:generators:test_unit" could be loaded as well, what
|
81
|
+
# Rails looks for is the first and last parts of the namespace. This is what
|
82
|
+
# allows any test framework to hook into Rails as long as it provides any
|
83
|
+
# of the hooks above.
|
84
|
+
#
|
85
|
+
# ==== Options
|
86
|
+
#
|
87
|
+
# The first and last part used to find the generator to be invoked are
|
88
|
+
# guessed based on class invokes hook_for, as noticed in the example above.
|
89
|
+
# This can be customized with two options: :base and :as.
|
90
|
+
#
|
91
|
+
# Let's suppose you are creating a generator that needs to invoke the
|
92
|
+
# controller generator from test unit. Your first attempt is:
|
93
|
+
#
|
94
|
+
# class AwesomeGenerator < Lightrail::Generators::Base
|
95
|
+
# hook_for :test_framework
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# The lookup in this case for test_unit as input is:
|
99
|
+
#
|
100
|
+
# "test_unit:awesome", "test_unit"
|
101
|
+
#
|
102
|
+
# Which is not the desired the lookup. You can change it by providing the
|
103
|
+
# :as option:
|
104
|
+
#
|
105
|
+
# class AwesomeGenerator < Lightrail::Generators::Base
|
106
|
+
# hook_for :test_framework, :as => :controller
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# And now it will lookup at:
|
110
|
+
#
|
111
|
+
# "test_unit:controller", "test_unit"
|
112
|
+
#
|
113
|
+
# Similarly, if you want it to also lookup in the rails namespace, you just
|
114
|
+
# need to provide the :base value:
|
115
|
+
#
|
116
|
+
# class AwesomeGenerator < Lightrail::Generators::Base
|
117
|
+
# hook_for :test_framework, :in => :rails, :as => :controller
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# And the lookup is exactly the same as previously:
|
121
|
+
#
|
122
|
+
# "rails:test_unit", "test_unit:controller", "test_unit"
|
123
|
+
#
|
124
|
+
# ==== Switches
|
125
|
+
#
|
126
|
+
# All hooks come with switches for user interface. If you do not want
|
127
|
+
# to use any test framework, you can do:
|
128
|
+
#
|
129
|
+
# rails generate controller Account --skip-test-framework
|
130
|
+
#
|
131
|
+
# Or similarly:
|
132
|
+
#
|
133
|
+
# rails generate controller Account --no-test-framework
|
134
|
+
#
|
135
|
+
# ==== Boolean hooks
|
136
|
+
#
|
137
|
+
# In some cases, you may want to provide a boolean hook. For example, webrat
|
138
|
+
# developers might want to have webrat available on controller generator.
|
139
|
+
# This can be achieved as:
|
140
|
+
#
|
141
|
+
# Lightrail::Generators::ControllerGenerator.hook_for :webrat, :type => :boolean
|
142
|
+
#
|
143
|
+
# Then, if you want webrat to be invoked, just supply:
|
144
|
+
#
|
145
|
+
# rails generate controller Account --webrat
|
146
|
+
#
|
147
|
+
# The hooks lookup is similar as above:
|
148
|
+
#
|
149
|
+
# "rails:generators:webrat", "webrat:generators:controller", "webrat"
|
150
|
+
#
|
151
|
+
# ==== Custom invocations
|
152
|
+
#
|
153
|
+
# You can also supply a block to hook_for to customize how the hook is
|
154
|
+
# going to be invoked. The block receives two arguments, an instance
|
155
|
+
# of the current class and the class to be invoked.
|
156
|
+
#
|
157
|
+
# For example, in the resource generator, the controller should be invoked
|
158
|
+
# with a pluralized class name. But by default it is invoked with the same
|
159
|
+
# name as the resource generator, which is singular. To change this, we
|
160
|
+
# can give a block to customize how the controller can be invoked.
|
161
|
+
#
|
162
|
+
# hook_for :resource_controller do |instance, controller|
|
163
|
+
# instance.invoke controller, [ instance.name.pluralize ]
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
def self.hook_for(*names, &block)
|
167
|
+
options = names.extract_options!
|
168
|
+
in_base = options.delete(:in) || base_name
|
169
|
+
as_hook = options.delete(:as) || generator_name
|
170
|
+
|
171
|
+
names.each do |name|
|
172
|
+
defaults = if options[:type] == :boolean
|
173
|
+
{ }
|
174
|
+
elsif default_value_for_option(name, options).in?([true, false])
|
175
|
+
{ :banner => "" }
|
176
|
+
else
|
177
|
+
{ :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" }
|
178
|
+
end
|
179
|
+
|
180
|
+
unless class_options.key?(name)
|
181
|
+
class_option(name, defaults.merge!(options))
|
182
|
+
end
|
183
|
+
|
184
|
+
hooks[name] = [ in_base, as_hook ]
|
185
|
+
invoke_from_option(name, options, &block)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# Remove a previously added hook.
|
190
|
+
#
|
191
|
+
# ==== Examples
|
192
|
+
#
|
193
|
+
# remove_hook_for :orm
|
194
|
+
#
|
195
|
+
def self.remove_hook_for(*names)
|
196
|
+
remove_invocation(*names)
|
197
|
+
|
198
|
+
names.each do |name|
|
199
|
+
hooks.delete(name)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
# Make class option aware of Lightrail::Generators.options and Lightrail::Generators.aliases.
|
204
|
+
def self.class_option(name, options={}) #:nodoc:
|
205
|
+
options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
|
206
|
+
options[:aliases] = default_aliases_for_option(name, options)
|
207
|
+
options[:default] = default_value_for_option(name, options)
|
208
|
+
super(name, options)
|
209
|
+
end
|
210
|
+
|
211
|
+
# Returns the default source root for a given generator. This is used internally
|
212
|
+
# by rails to set its generators source root. If you want to customize your source
|
213
|
+
# root, you should use source_root.
|
214
|
+
def self.default_source_root
|
215
|
+
return unless base_name && generator_name
|
216
|
+
path = File.expand_path(File.join(base_name, generator_name, 'templates'), base_root)
|
217
|
+
path if File.exists?(path)
|
218
|
+
end
|
219
|
+
|
220
|
+
# Returns the base root for a common set of generators. This is used to dynamically
|
221
|
+
# guess the default source root.
|
222
|
+
def self.base_root
|
223
|
+
File.dirname(__FILE__)
|
224
|
+
end
|
225
|
+
|
226
|
+
# Cache source root and add lib/generators/base/generator/templates to
|
227
|
+
# source paths.
|
228
|
+
def self.inherited(base) #:nodoc:
|
229
|
+
super
|
230
|
+
|
231
|
+
# Invoke source_root so the default_source_root is set.
|
232
|
+
base.source_root
|
233
|
+
|
234
|
+
if base.name && base.name !~ /Base$/
|
235
|
+
Lightrail::Generators.subclasses << base
|
236
|
+
|
237
|
+
Lightrail::Generators.templates_path.each do |path|
|
238
|
+
if base.name.include?('::')
|
239
|
+
base.source_paths << File.join(path, base.base_name, base.generator_name)
|
240
|
+
else
|
241
|
+
base.source_paths << File.join(path, base.generator_name)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
protected
|
248
|
+
|
249
|
+
# Check whether the given class names are already taken by user
|
250
|
+
# application or Ruby on Rails.
|
251
|
+
#
|
252
|
+
def class_collisions(*class_names) #:nodoc:
|
253
|
+
return unless behavior == :invoke
|
254
|
+
|
255
|
+
class_names.flatten.each do |class_name|
|
256
|
+
class_name = class_name.to_s
|
257
|
+
next if class_name.strip.empty?
|
258
|
+
|
259
|
+
# Split the class from its module nesting
|
260
|
+
nesting = class_name.split('::')
|
261
|
+
last_name = nesting.pop
|
262
|
+
|
263
|
+
# Extract the last Module in the nesting
|
264
|
+
last = nesting.inject(Object) do |last_module, nest|
|
265
|
+
break unless last_module.const_defined?(nest, false)
|
266
|
+
last_module.const_get(nest)
|
267
|
+
end
|
268
|
+
|
269
|
+
if last && last.const_defined?(last_name.camelize, false)
|
270
|
+
raise Error, "The name '#{class_name}' is either already used in your application " <<
|
271
|
+
"or reserved by Ruby on Rails. Please choose an alternative and run " <<
|
272
|
+
"this generator again."
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
# Use Rails default banner.
|
278
|
+
#
|
279
|
+
def self.banner
|
280
|
+
"rails generate #{namespace.sub(/^rails:/,'')} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]".gsub(/\s+/, ' ')
|
281
|
+
end
|
282
|
+
|
283
|
+
# Sets the base_name taking into account the current class namespace.
|
284
|
+
#
|
285
|
+
def self.base_name
|
286
|
+
@base_name ||= begin
|
287
|
+
if base = name.to_s.split('::').first
|
288
|
+
base.underscore
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
# Removes the namespaces and get the generator name. For example,
|
294
|
+
# Lightrail::Generators::ModelGenerator will return "model" as generator name.
|
295
|
+
#
|
296
|
+
def self.generator_name
|
297
|
+
@generator_name ||= begin
|
298
|
+
if generator = name.to_s.split('::').last
|
299
|
+
generator.sub!(/Generator$/, '')
|
300
|
+
generator.underscore
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
# Return the default value for the option name given doing a lookup in
|
306
|
+
# Lightrail::Generators.options.
|
307
|
+
#
|
308
|
+
def self.default_value_for_option(name, options)
|
309
|
+
default_for_option(Lightrail::Generators.options, name, options, options[:default])
|
310
|
+
end
|
311
|
+
|
312
|
+
# Return default aliases for the option name given doing a lookup in
|
313
|
+
# Lightrail::Generators.aliases.
|
314
|
+
#
|
315
|
+
def self.default_aliases_for_option(name, options)
|
316
|
+
default_for_option(Lightrail::Generators.aliases, name, options, options[:aliases])
|
317
|
+
end
|
318
|
+
|
319
|
+
# Return default for the option name given doing a lookup in config.
|
320
|
+
#
|
321
|
+
def self.default_for_option(config, name, options, default)
|
322
|
+
if generator_name and c = config[generator_name.to_sym] and c.key?(name)
|
323
|
+
c[name]
|
324
|
+
elsif base_name and c = config[base_name.to_sym] and c.key?(name)
|
325
|
+
c[name]
|
326
|
+
elsif config[:rails].key?(name)
|
327
|
+
config[:rails][name]
|
328
|
+
else
|
329
|
+
default
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
# Keep hooks configuration that are used on prepare_for_invocation.
|
334
|
+
#
|
335
|
+
def self.hooks #:nodoc:
|
336
|
+
@hooks ||= from_superclass(:hooks, {})
|
337
|
+
end
|
338
|
+
|
339
|
+
# Prepare class invocation to search on Rails namespace if a previous
|
340
|
+
# added hook is being used.
|
341
|
+
#
|
342
|
+
def self.prepare_for_invocation(name, value) #:nodoc:
|
343
|
+
return super unless value.is_a?(String) || value.is_a?(Symbol)
|
344
|
+
|
345
|
+
if value && constants = self.hooks[name]
|
346
|
+
value = name if TrueClass === value
|
347
|
+
Lightrail::Generators.find_by_namespace(value, *constants)
|
348
|
+
elsif klass = Lightrail::Generators.find_by_namespace(value)
|
349
|
+
klass
|
350
|
+
else
|
351
|
+
super
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
# Small macro to add ruby as an option to the generator with proper
|
356
|
+
# default value plus an instance helper method called shebang.
|
357
|
+
#
|
358
|
+
def self.add_shebang_option!
|
359
|
+
class_option :ruby, :type => :string, :aliases => "-r", :default => Thor::Util.ruby_command,
|
360
|
+
:desc => "Path to the Ruby binary of your choice", :banner => "PATH"
|
361
|
+
|
362
|
+
no_tasks {
|
363
|
+
define_method :shebang do
|
364
|
+
@shebang ||= begin
|
365
|
+
command = if options[:ruby] == Thor::Util.ruby_command
|
366
|
+
"/usr/bin/env #{File.basename(Thor::Util.ruby_command)}"
|
367
|
+
else
|
368
|
+
options[:ruby]
|
369
|
+
end
|
370
|
+
"#!#{command}"
|
371
|
+
end
|
372
|
+
end
|
373
|
+
}
|
374
|
+
end
|
375
|
+
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'lightrail', '<%= Lightrail::VERSION %>'
|
4
|
+
gem 'activerecord'
|
5
|
+
gem 'activeresource'
|
6
|
+
gem 'actionmailer'
|
7
|
+
|
8
|
+
<%= "gem 'jruby-openssl'\n" if defined?(JRUBY_VERSION) -%>
|
9
|
+
<%= assets_gemfile_entry %>
|
10
|
+
<%= javascript_gemfile_entry %>
|
11
|
+
|
12
|
+
# To use ActiveModel has_secure_password
|
13
|
+
# gem 'bcrypt-ruby', '~> 3.0.0'
|
14
|
+
|
15
|
+
# To use Jbuilder templates for JSON
|
16
|
+
# gem 'jbuilder'
|
17
|
+
|
18
|
+
# Use unicorn as the web server
|
19
|
+
# gem 'unicorn'
|
20
|
+
|
21
|
+
# Deploy with Capistrano
|
22
|
+
# gem 'capistrano', :group => :development
|
23
|
+
|
24
|
+
# To use debugger
|
25
|
+
# gem 'ruby-debug19', :require => 'ruby-debug'
|
@@ -0,0 +1,259 @@
|
|
1
|
+
== Welcome to Rails
|
2
|
+
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
5
|
+
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb"
|
7
|
+
templates that are primarily responsible for inserting pre-built data in between
|
8
|
+
HTML tags. The model contains the "smart" domain objects (such as Account,
|
9
|
+
Product, Person, Post) that holds all the business logic and knows how to
|
10
|
+
persist themselves to a database. The controller handles the incoming requests
|
11
|
+
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
12
|
+
and directing data to the view.
|
13
|
+
|
14
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
15
|
+
layer entitled Active Record. This layer allows you to present the data from
|
16
|
+
database rows as objects and embellish these data objects with business logic
|
17
|
+
methods. You can read more about Active Record in
|
18
|
+
link:files/vendor/rails/activerecord/README.html.
|
19
|
+
|
20
|
+
The controller and view are handled by the Action Pack, which handles both
|
21
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
22
|
+
are bundled in a single package due to their heavy interdependence. This is
|
23
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
24
|
+
more separate. Each of these packages can be used independently outside of
|
25
|
+
Rails. You can read more about Action Pack in
|
26
|
+
link:files/vendor/rails/actionpack/README.html.
|
27
|
+
|
28
|
+
|
29
|
+
== Getting Started
|
30
|
+
|
31
|
+
1. At the command prompt, create a new Rails application:
|
32
|
+
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
33
|
+
|
34
|
+
2. Change directory to <tt>myapp</tt> and start the web server:
|
35
|
+
<tt>cd myapp; rails server</tt> (run with --help for options)
|
36
|
+
|
37
|
+
3. Go to http://localhost:3000/ and you'll see:
|
38
|
+
"Welcome aboard: You're riding Ruby on Rails!"
|
39
|
+
|
40
|
+
4. Follow the guidelines to start developing your application. You can find
|
41
|
+
the following resources handy:
|
42
|
+
|
43
|
+
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
44
|
+
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
45
|
+
|
46
|
+
|
47
|
+
== Debugging Rails
|
48
|
+
|
49
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
50
|
+
will help you debug it and get it back on the rails.
|
51
|
+
|
52
|
+
First area to check is the application log files. Have "tail -f" commands
|
53
|
+
running on the server.log and development.log. Rails will automatically display
|
54
|
+
debugging and runtime information to these files. Debugging info will also be
|
55
|
+
shown in the browser on requests from 127.0.0.1.
|
56
|
+
|
57
|
+
You can also log your own messages directly into the log file from your code
|
58
|
+
using the Ruby logger class from inside your controllers. Example:
|
59
|
+
|
60
|
+
class WeblogController < ActionController::Base
|
61
|
+
def destroy
|
62
|
+
@weblog = Weblog.find(params[:id])
|
63
|
+
@weblog.destroy
|
64
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
The result will be a message in your log file along the lines of:
|
69
|
+
|
70
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
71
|
+
|
72
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
73
|
+
|
74
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
75
|
+
several books available online as well:
|
76
|
+
|
77
|
+
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
78
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
79
|
+
|
80
|
+
These two books will bring you up to speed on the Ruby language and also on
|
81
|
+
programming in general.
|
82
|
+
|
83
|
+
|
84
|
+
== Debugger
|
85
|
+
|
86
|
+
Debugger support is available through the debugger command when you start your
|
87
|
+
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
88
|
+
execution at any point in the code, investigate and change the model, and then,
|
89
|
+
resume execution! You need to install ruby-debug19 to run the server in debugging
|
90
|
+
mode. With gems, use <tt>sudo gem install ruby-debug19</tt>. Example:
|
91
|
+
|
92
|
+
class WeblogController < ActionController::Base
|
93
|
+
def index
|
94
|
+
@posts = Post.all
|
95
|
+
debugger
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
So the controller will accept the action, run the first line, then present you
|
100
|
+
with a IRB prompt in the server window. Here you can do things like:
|
101
|
+
|
102
|
+
>> @posts.inspect
|
103
|
+
=> "[#<Post:0x14a6be8
|
104
|
+
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
105
|
+
#<Post:0x14a6620
|
106
|
+
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
107
|
+
>> @posts.first.title = "hello from a debugger"
|
108
|
+
=> "hello from a debugger"
|
109
|
+
|
110
|
+
...and even better, you can examine how your runtime objects actually work:
|
111
|
+
|
112
|
+
>> f = @posts.first
|
113
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
114
|
+
>> f.
|
115
|
+
Display all 152 possibilities? (y or n)
|
116
|
+
|
117
|
+
Finally, when you're ready to resume execution, you can enter "cont".
|
118
|
+
|
119
|
+
|
120
|
+
== Console
|
121
|
+
|
122
|
+
The console is a Ruby shell, which allows you to interact with your
|
123
|
+
application's domain model. Here you'll have all parts of the application
|
124
|
+
configured, just like it is when the application is running. You can inspect
|
125
|
+
domain models, change values, and save to the database. Starting the script
|
126
|
+
without arguments will launch it in the development environment.
|
127
|
+
|
128
|
+
To start the console, run <tt>rails console</tt> from the application
|
129
|
+
directory.
|
130
|
+
|
131
|
+
Options:
|
132
|
+
|
133
|
+
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
134
|
+
made to the database.
|
135
|
+
* Passing an environment name as an argument will load the corresponding
|
136
|
+
environment. Example: <tt>rails console production</tt>.
|
137
|
+
|
138
|
+
To reload your controllers and models after launching the console run
|
139
|
+
<tt>reload!</tt>
|
140
|
+
|
141
|
+
More information about irb can be found at:
|
142
|
+
link:http://www.rubycentral.org/pickaxe/irb.html
|
143
|
+
|
144
|
+
|
145
|
+
== dbconsole
|
146
|
+
|
147
|
+
You can go to the command line of your database directly through <tt>rails
|
148
|
+
dbconsole</tt>. You would be connected to the database with the credentials
|
149
|
+
defined in database.yml. Starting the script without arguments will connect you
|
150
|
+
to the development database. Passing an argument will connect you to a different
|
151
|
+
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
152
|
+
PostgreSQL and SQLite 3.
|
153
|
+
|
154
|
+
== Description of Contents
|
155
|
+
|
156
|
+
The default directory structure of a generated Ruby on Rails application:
|
157
|
+
|
158
|
+
|-- app
|
159
|
+
| |-- assets
|
160
|
+
| |-- images
|
161
|
+
| |-- javascripts
|
162
|
+
| `-- stylesheets
|
163
|
+
| |-- controllers
|
164
|
+
| |-- helpers
|
165
|
+
| |-- mailers
|
166
|
+
| |-- models
|
167
|
+
| `-- views
|
168
|
+
| `-- layouts
|
169
|
+
|-- config
|
170
|
+
| |-- environments
|
171
|
+
| |-- initializers
|
172
|
+
| `-- locales
|
173
|
+
|-- db
|
174
|
+
|-- doc
|
175
|
+
|-- lib
|
176
|
+
| `-- tasks
|
177
|
+
|-- log
|
178
|
+
|-- public
|
179
|
+
|-- script
|
180
|
+
|-- test
|
181
|
+
| |-- fixtures
|
182
|
+
| |-- functional
|
183
|
+
| |-- integration
|
184
|
+
| |-- performance
|
185
|
+
| `-- unit
|
186
|
+
|-- tmp
|
187
|
+
| |-- cache
|
188
|
+
| |-- pids
|
189
|
+
| |-- sessions
|
190
|
+
| `-- sockets
|
191
|
+
`-- vendor
|
192
|
+
|-- assets
|
193
|
+
`-- stylesheets
|
194
|
+
|
195
|
+
app
|
196
|
+
Holds all the code that's specific to this particular application.
|
197
|
+
|
198
|
+
app/assets
|
199
|
+
Contains subdirectories for images, stylesheets, and JavaScript files.
|
200
|
+
|
201
|
+
app/controllers
|
202
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
203
|
+
automated URL mapping. All controllers should descend from
|
204
|
+
ApplicationController which itself descends from ActionController::Base.
|
205
|
+
|
206
|
+
app/models
|
207
|
+
Holds models that should be named like post.rb. Models descend from
|
208
|
+
ActiveRecord::Base by default.
|
209
|
+
|
210
|
+
app/views
|
211
|
+
Holds the template files for the view that should be named like
|
212
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
213
|
+
eRuby syntax by default.
|
214
|
+
|
215
|
+
app/views/layouts
|
216
|
+
Holds the template files for layouts to be used with views. This models the
|
217
|
+
common header/footer method of wrapping views. In your views, define a layout
|
218
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
219
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
220
|
+
layout.
|
221
|
+
|
222
|
+
app/helpers
|
223
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
224
|
+
generated for you automatically when using generators for controllers.
|
225
|
+
Helpers can be used to wrap functionality for your views into methods.
|
226
|
+
|
227
|
+
config
|
228
|
+
Configuration files for the Rails environment, the routing map, the database,
|
229
|
+
and other dependencies.
|
230
|
+
|
231
|
+
db
|
232
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
233
|
+
sequence of Migrations for your schema.
|
234
|
+
|
235
|
+
doc
|
236
|
+
This directory is where your application documentation will be stored when
|
237
|
+
generated using <tt>rake doc:app</tt>
|
238
|
+
|
239
|
+
lib
|
240
|
+
Application specific libraries. Basically, any kind of custom code that
|
241
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
242
|
+
the load path.
|
243
|
+
|
244
|
+
public
|
245
|
+
The directory available for the web server. Also contains the dispatchers and the
|
246
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
247
|
+
server.
|
248
|
+
|
249
|
+
script
|
250
|
+
Helper scripts for automation and generation.
|
251
|
+
|
252
|
+
test
|
253
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
254
|
+
command, template test files will be generated for you and placed in this
|
255
|
+
directory.
|
256
|
+
|
257
|
+
vendor
|
258
|
+
External libraries that the application depends on. If the app has frozen rails,
|
259
|
+
those gems also go here, under vendor/rails/. This directory is in the load path.
|