rails 0.8.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

@@ -0,0 +1,119 @@
1
+ #!/usr/local/bin/ruby
2
+
3
+ require File.dirname(__FILE__) + '/../config/environments/production'
4
+
5
+ def create_mailer_class(class_name, file_name, mail_actions)
6
+ File.open("app/models/" + file_name + ".rb", "w", 0777) do |mailer_file|
7
+ mailer_file.write <<EOF
8
+ class #{class_name} < ActionMailer::Base
9
+ #{mail_actions.collect { |action|
10
+ " def #{action}(sent_on = Time.now)\n" +
11
+ " @recipients = ''\n" +
12
+ " @from = ''\n" +
13
+ " @subject = ''\n" +
14
+ " @body = { }\n" +
15
+ " @sent_on = sent_on\n" +
16
+ " end"
17
+ }.join "\n\n" }
18
+ end
19
+ EOF
20
+ end
21
+ end
22
+
23
+ def create_templates(class_name, file_name, mail_actions)
24
+ Dir.mkdir("app/views/#{file_name}") rescue nil
25
+ mail_actions.each { |action| File.open("app/views/#{file_name}/#{action}.rhtml", "w", 0777) do |template_file|
26
+ template_file.write <<EOF
27
+ #{class_name}##{action}
28
+ EOF
29
+ end }
30
+ end
31
+
32
+ def create_fixtures(class_name, file_name, mail_actions)
33
+ Dir.mkdir("test/fixtures/" + file_name) rescue nil
34
+ mail_actions.each { |action| File.open("test/fixtures/#{file_name}/#{action}", "w", 0777) do |template_file|
35
+ template_file.write <<EOF
36
+ #{class_name}##{action}
37
+ EOF
38
+ end }
39
+ end
40
+
41
+
42
+ def create_test_class(class_name, file_name, mail_actions)
43
+ File.open("test/unit/" + file_name + "_test.rb", "w", 0777) do |test_file|
44
+ test_file.write <<EOF
45
+ require File.dirname(__FILE__) + '/../test_helper'
46
+ require '#{file_name}'
47
+
48
+ class #{class_name}Test < Test::Unit::TestCase
49
+ def setup
50
+ @expected = TMail::Mail.new
51
+ end
52
+
53
+ #{mail_actions.collect { |action|
54
+ " def test_#{action}\n" +
55
+ " @expected.to = ''\n" +
56
+ " @expected.from = ''\n" +
57
+ " @expected.subject = ''\n" +
58
+ " @expected.body = read_notification_fixture \"#{action}\"\n" +
59
+ " @expected.date = Time.now\n" +
60
+ " \n" +
61
+ " actual = #{class_name}.create_#{action}(@expected.date)\n" +
62
+ " \n" +
63
+ " assert_equal @expected.encoded, actual.encoded\n" +
64
+ " end"
65
+ }.join "\n\n" }
66
+
67
+ private
68
+ def read_notification_fixture(name)
69
+ IO.readlines(File.dirname(__FILE__) + "/../fixtures/#{file_name}/\#{name}").join
70
+ end
71
+ end
72
+ EOF
73
+ end
74
+ end
75
+
76
+
77
+ if !ARGV.empty?
78
+ mailer_name = ARGV[0]
79
+ mail_actions = ARGV[1..-1]
80
+
81
+ class_name = Inflector.camelize(mailer_name)
82
+ file_name = Inflector.underscore(mailer_name)
83
+
84
+ create_mailer_class(class_name, file_name, mail_actions)
85
+ create_templates(class_name, file_name, mail_actions)
86
+ create_fixtures(class_name, file_name, mail_actions)
87
+ create_test_class(class_name, file_name, mail_actions)
88
+ else
89
+ puts <<-END_HELP
90
+
91
+ NAME
92
+ new_mailer - create mailer and view stub files
93
+
94
+ SYNOPSIS
95
+ new_mailer [mailer_name] [mail_actions ...]
96
+
97
+ DESCRIPTION
98
+ The new_mailer generator takes the name of the new mailer class as the
99
+ first argument and a variable number of mail action names as subsequent arguments.
100
+
101
+ From the passed arguments, new_mailer generates a class file in
102
+ app/models with a mail action for each of the mail action names passed.
103
+ It then creates a mail test suite in test/unit with one stub test case and one
104
+ stub fixture per mail action. Finally, it creates a template stub for each of the
105
+ mail action names in app/views under a directory with the same name as the class.
106
+
107
+ EXAMPLE
108
+ new_mailer Notifications signup forgot_password invoice
109
+
110
+ This will generate a Notifications class in
111
+ app/models/notifications.rb, a NotificationsTest in
112
+ test/unit/notifications_test.rb, and signup, forgot_password, and invoice
113
+ in test/fixture/notification. It will also create signup.rhtml,
114
+ forgot_password.rhtml, and invoice.rhtml in app/views/notifications.
115
+
116
+ The Notifications class will have the following methods: signup, forgot_password,
117
+ and invoice.
118
+ END_HELP
119
+ end
@@ -0,0 +1,70 @@
1
+ #!/usr/local/bin/ruby
2
+
3
+ require File.dirname(__FILE__) + '/../config/environments/production'
4
+
5
+ def create_model_class(model_name, file_name)
6
+ File.open("app/models/" + file_name + ".rb", "w", 0777) do |model_file|
7
+ model_file.write <<EOF
8
+ require 'active_record'
9
+
10
+ class #{model_name} < ActiveRecord::Base
11
+ end
12
+ EOF
13
+ end
14
+ end
15
+
16
+ def create_test_class(model_name, file_name, table_name)
17
+ File.open("test/unit/" + file_name + "_test.rb", "w", 0777) do |test_file|
18
+ test_file.write <<EOF
19
+ require File.dirname(__FILE__) + '/../test_helper'
20
+ require '#{file_name}'
21
+
22
+ class #{model_name}Test < Test::Unit::TestCase
23
+ def setup
24
+ @#{table_name} = create_fixtures "#{table_name}"
25
+ end
26
+
27
+ def test_something
28
+ assert true, "Test implementation missing"
29
+ end
30
+ end
31
+ EOF
32
+ end
33
+ end
34
+
35
+ def create_fixtures_directory(table_name)
36
+ Dir.mkdir("test/fixtures/" + table_name) rescue puts "Fixtures directory already exists"
37
+ end
38
+
39
+
40
+ if !ARGV.empty?
41
+ model_name = ARGV.shift
42
+ file_name = Inflector.underscore(model_name)
43
+ table_name = ARGV.shift || file_name
44
+
45
+ create_model_class(model_name, file_name)
46
+ create_test_class(model_name, file_name, table_name)
47
+ create_fixtures_directory(table_name)
48
+ else
49
+ puts <<-HELP
50
+
51
+ NAME
52
+ new_model - create model stub files
53
+
54
+ SYNOPSIS
55
+ new_model [model_name]
56
+
57
+ DESCRIPTION
58
+ The new_model generator takes the name of the new model and generates a model
59
+ file in app/models that decents from ActiveRecord::Base but is otherwise empty.
60
+ It then creates a model test suite in test/unit with one failing
61
+ test case. Finally, it creates fixture directory in test/fixtures.
62
+
63
+ EXAMPLE
64
+ new_model Account
65
+
66
+ This will generate a Account class in app/models/account.rb, a AccountTest in
67
+ test/unit/account_test.rb, and the directory test/fixtures/account.
68
+
69
+ HELP
70
+ end
@@ -0,0 +1,8 @@
1
+ require 'action_controller'
2
+ require 'application_helper'
3
+
4
+ # The filters added to this controller will be run for all controllers in the application.
5
+ # Likewise will all the methods added be available for all controllers.
6
+ class AbstractApplicationController < ActionController::Base
7
+ include ApplicationHelper
8
+ end
@@ -0,0 +1,6 @@
1
+ # The methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ def self.append_features(controller) #:nodoc:
4
+ controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + "/../config/environments/test"
2
+
3
+ require 'test/unit'
4
+ require 'active_record/fixtures'
5
+ require 'action_controller/test_process'
6
+
7
+ def create_fixtures(*table_names)
8
+ Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures", table_names)
9
+ end
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <h1>File not found</h1>
4
+ <p>Change this error message for pages not found in public/404.html</p>
5
+ </body>
6
+ </html>
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <h1>Application error (Apache)</h1>
4
+ <p>Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html</p>
5
+ </body>
6
+ </html>
@@ -0,0 +1 @@
1
+ <html><head><META HTTP-EQUIV="Refresh" CONTENT="0;URL=_doc/index.html"></head></html>
@@ -0,0 +1,71 @@
1
+ class CodeStatistics
2
+ def initialize(*pairs)
3
+ @pairs = pairs
4
+ @statistics = calculate_statistics
5
+ @total = calculate_total if pairs.length > 1
6
+ end
7
+
8
+ def to_s
9
+ print_header
10
+ @statistics.each{ |k, v| print_line(k, v) }
11
+ print_splitter
12
+
13
+ if @total
14
+ print_line("Total", @total)
15
+ print_splitter
16
+ end
17
+ end
18
+
19
+ private
20
+ def calculate_statistics
21
+ @pairs.inject({}) { |stats, pair| stats[pair.first] = calculate_directory_statistics(pair.last); stats }
22
+ end
23
+
24
+ def calculate_directory_statistics(directory, pattern = /.*rb/)
25
+ stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
26
+
27
+ Dir.foreach(directory) do |file_name|
28
+ next unless file_name =~ pattern
29
+
30
+ f = File.open(directory + "/" + file_name)
31
+
32
+ while line = f.gets
33
+ stats["lines"] += 1
34
+ stats["classes"] += 1 if line =~ /class [A-Z]/
35
+ stats["methods"] += 1 if line =~ /def [a-z]/
36
+ stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
37
+ end
38
+ end
39
+
40
+ stats
41
+ end
42
+
43
+ def calculate_total
44
+ total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
45
+ @statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
46
+ total
47
+ end
48
+
49
+ def print_header
50
+ print_splitter
51
+ puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
52
+ print_splitter
53
+ end
54
+
55
+ def print_splitter
56
+ puts "+----------------------+-------+-------+---------+---------+-----+-------+"
57
+ end
58
+
59
+ def print_line(name, statistics)
60
+ m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
61
+ loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
62
+
63
+ puts "| #{name.ljust(20)} " +
64
+ "| #{statistics["lines"].to_s.rjust(5)} " +
65
+ "| #{statistics["codelines"].to_s.rjust(5)} " +
66
+ "| #{statistics["classes"].to_s.rjust(7)} " +
67
+ "| #{statistics["methods"].to_s.rjust(7)} " +
68
+ "| #{m_over_c.to_s.rjust(3)} " +
69
+ "| #{loc_over_m.to_s.rjust(5)} |"
70
+ end
71
+ end
@@ -0,0 +1,46 @@
1
+ #--
2
+ # Copyright (c) 2004 David Heinemeier Hansson
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ class Dispatcher
25
+ DEFAULT_SESSION_OPTIONS = { "database_manager" => CGI::Session::PStore, "prefix" => "ruby_sess.", "session_path" => "/" }
26
+
27
+ def self.dispatch(cgi = CGI.new, session_options = DEFAULT_SESSION_OPTIONS, error_page = nil)
28
+ begin
29
+ request = ActionController::CgiRequest.new(cgi, session_options)
30
+ response = ActionController::CgiResponse.new(cgi)
31
+
32
+ controller_name = request.parameters["controller"].gsub(/[^_a-zA-Z0-9]/, "").untaint
33
+
34
+ require "#{Inflector.underscore(controller_name)}_controller"
35
+ Object.const_get("#{Inflector.camelize(controller_name)}Controller").process(request, response).out
36
+ rescue Exception => e
37
+ begin
38
+ ActionController::Base.logger.info "\n\nException throw during dispatch: #{e.message}\n#{e.backtrace.join("\n")}"
39
+ rescue Exception
40
+ # Couldn't log error
41
+ end
42
+
43
+ if error_page then cgi.out{ IO.readlines(error_page) } else raise e end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,158 @@
1
+ # Donated by Florian Gross
2
+
3
+ require 'webrick'
4
+ require 'cgi'
5
+ require 'stringio'
6
+
7
+ include WEBrick
8
+
9
+ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
10
+ def self.dispatch(options = {})
11
+ Socket.do_not_reverse_lookup = true # patch for OS X
12
+
13
+ server = WEBrick::HTTPServer.new(:Port => options[:port].to_i, :ServerType => options[:server_type], :BindAddress => options[:ip])
14
+ server.mount('/', DispatchServlet, options)
15
+
16
+ trap("INT") { server.shutdown }
17
+ server.start
18
+ end
19
+
20
+ def initialize(server, options)
21
+ @server_options = options
22
+ @file_handler = WEBrick::HTTPServlet::FileHandler.new(server, options[:server_root], {:FancyIndexing => true })
23
+ super
24
+ end
25
+
26
+ def do_GET(req, res)
27
+ unless handle_index(req, res)
28
+ unless handle_dispatch(req, res)
29
+ unless handle_file(req, res)
30
+ unless handle_mapped(req, res)
31
+ raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found."
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ alias :do_POST :do_GET
39
+
40
+ def handle_index(req, res)
41
+ if req.request_uri.path == "/"
42
+ if @server_options[:index_controller]
43
+ res.set_redirect WEBrick::HTTPStatus::MovedPermanently, "/#{@server_options[:index_controller]}/"
44
+ else
45
+ res.set_redirect WEBrick::HTTPStatus::MovedPermanently, "/_doc/index.html"
46
+ end
47
+
48
+ return true
49
+ else
50
+ return false
51
+ end
52
+ end
53
+
54
+ def handle_file(req, res)
55
+ begin
56
+ @file_handler.send(:do_GET, req, res)
57
+ return true
58
+ rescue HTTPStatus::PartialContent, HTTPStatus::NotModified => err
59
+ res.set_error(err)
60
+ return true
61
+ rescue => err
62
+ p err
63
+ return false
64
+ end
65
+ end
66
+
67
+ def handle_mapped(req, res)
68
+ parsed_ok, controller, action, id = DispatchServlet.parse_uri(req.request_uri.path)
69
+ if parsed_ok
70
+ query = "controller=#{controller}&action=#{action}&id=#{id}"
71
+ query << "&#{req.request_uri.query}" if req.request_uri.query
72
+ origin = req.request_uri.path + "?" + query
73
+ req.request_uri.path = "/dispatch.rb"
74
+ req.request_uri.query = query
75
+ handle_dispatch(req, res, origin)
76
+ else
77
+ return false
78
+ end
79
+ end
80
+
81
+ def handle_dispatch(req, res, origin = nil)
82
+ return false unless /^\/dispatch\.(?:cgi|rb|fcgi)$/.match(req.request_uri.path)
83
+
84
+ env = req.meta_vars.clone
85
+ env["QUERY_STRING"] = req.request_uri.query
86
+ env["REQUEST_URI"] = origin if origin
87
+
88
+ data = nil
89
+ if @server_options[:cache_classes]
90
+ old_stdin, old_stdout = $stdin, $stdout
91
+ $stdin, $stdout = StringIO.new(req.body || ""), StringIO.new
92
+
93
+ begin
94
+ require 'cgi'
95
+ CGI.send(:define_method, :env_table) { env }
96
+
97
+ load File.join(@server_options[:server_root], "dispatch.rb")
98
+
99
+ $stdout.rewind
100
+ data = $stdout.read
101
+ ensure
102
+ $stdin, $stdout = old_stdin, old_stdout
103
+ end
104
+ else
105
+ begin
106
+ require 'rbconfig'
107
+ ruby_interpreter = Config::CONFIG['ruby_install_name'] || 'ruby'
108
+ rescue Object
109
+ ruby_interpreter = 'ruby'
110
+ end
111
+
112
+ dispatch_rb_path = File.expand_path(File.join(@server_options[:server_root], "dispatch.rb"))
113
+ IO.popen(ruby_interpreter, "r+") do |ruby|
114
+ ruby.puts <<-END
115
+ require 'cgi'
116
+ require 'stringio'
117
+ env = #{env.inspect}
118
+ CGI.send(:define_method, :env_table) { env }
119
+ $stdin = StringIO.new(#{(req.body || "").inspect})
120
+
121
+ eval "load '#{dispatch_rb_path}'", binding, #{dispatch_rb_path.inspect}
122
+ END
123
+ ruby.close_write
124
+ data = ruby.read
125
+ end
126
+ end
127
+
128
+ raw_header, body = *data.split(/^[\xd\xa]+/on, 2)
129
+ header = WEBrick::HTTPUtils::parse_header(raw_header)
130
+ if /^(\d+)/ =~ header['status'][0]
131
+ res.status = $1.to_i
132
+ header.delete('status')
133
+ end
134
+ header.each { |key, val| res[key] = val.join(", ") }
135
+
136
+ res.body = body
137
+ return true
138
+ rescue => err
139
+ p err, err.backtrace
140
+ return false
141
+ end
142
+
143
+ def self.parse_uri(path)
144
+ component = /([-_a-zA-Z0-9]+)/
145
+
146
+ case path.sub(%r{^/(?:fcgi|mruby|cgi)/}, "/")
147
+ when %r{^/#{component}/?$} then
148
+ [true, $1, "index", nil]
149
+ when %r{^/#{component}/#{component}/?$} then
150
+ [true, $1, $2, nil]
151
+ when %r{^/#{component}/#{component}/#{component}/?$} then
152
+ [true, $1, $2, $3]
153
+ else
154
+ [false, nil, nil, nil]
155
+ end
156
+ end
157
+
158
+ end