bushido 0.0.35 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +4 -0
  3. data/Gemfile +6 -13
  4. data/README.md +3 -0
  5. data/Rakefile +31 -7
  6. data/app/controllers/bushido/data_controller.rb +2 -1
  7. data/app/controllers/bushido/envs_controller.rb +7 -5
  8. data/app/controllers/bushido/mail_controller.rb +37 -0
  9. data/lib/bushido.rb +27 -5
  10. data/lib/bushido/action_mailer.rb +35 -0
  11. data/lib/bushido/app.rb +7 -0
  12. data/lib/bushido/bar.rb +19 -0
  13. data/lib/bushido/base.rb +23 -0
  14. data/lib/bushido/command.rb +17 -15
  15. data/lib/bushido/config.rb +12 -0
  16. data/lib/bushido/data.rb +41 -23
  17. data/lib/bushido/data_helper.rb +1 -0
  18. data/lib/bushido/engine.rb +21 -0
  19. data/lib/bushido/envs_helper.rb +1 -0
  20. data/lib/bushido/event.rb +25 -2
  21. data/lib/bushido/event_observer.rb +20 -0
  22. data/lib/bushido/mail_helper.rb +1 -0
  23. data/lib/bushido/mail_route.rb +186 -0
  24. data/lib/bushido/middleware.rb +9 -16
  25. data/lib/bushido/platform.rb +27 -3
  26. data/lib/bushido/smtp.rb +24 -0
  27. data/lib/bushido/user.rb +39 -9
  28. data/lib/bushido/user_helper.rb +7 -0
  29. data/lib/bushido/utils.rb +44 -0
  30. data/lib/bushido/version.rb +1 -1
  31. data/lib/generators/bushido/auth_migration_generator.rb +46 -0
  32. data/lib/generators/bushido/hooks_generator.rb +43 -0
  33. data/lib/generators/bushido/mail_routes_generator.rb +45 -0
  34. data/lib/generators/bushido/routes_generator.rb +22 -0
  35. data/lib/{bushido/hooks.rb → hooks.rb} +0 -0
  36. data/lib/rails/routes.rb +36 -8
  37. data/lib/tasks/bushido.rake +6 -0
  38. data/{test_app/spec → spec/app_spec}/controllers/envs_controller_spec.rb +0 -0
  39. data/spec/app_spec/controllers/mail_controller_spec.rb +28 -0
  40. data/{test_app/spec → spec/app_spec}/integration/app_claim_spec.rb +4 -6
  41. data/spec/gem_spec/base_spec.rb +24 -0
  42. data/spec/gem_spec/command_spec.rb +181 -0
  43. data/spec/gem_spec/config_spec.rb +9 -0
  44. data/spec/gem_spec/data_spec.rb +13 -0
  45. data/spec/gem_spec/hooks_spec.rb +5 -0
  46. data/spec/gem_spec/mail_route_spec.rb +168 -0
  47. data/spec/gem_spec/platform_spec.rb +9 -0
  48. data/spec/gem_spec/user_spec.rb +45 -0
  49. data/spec/spec_helper.rb +21 -0
  50. data/spec/test_apps/rails-2.3/.gitignore +13 -0
  51. data/spec/test_apps/rails-2.3/Gemfile +13 -0
  52. data/spec/test_apps/rails-2.3/README +243 -0
  53. data/spec/test_apps/rails-2.3/Rakefile +10 -0
  54. data/spec/test_apps/rails-2.3/app/controllers/application_controller.rb +10 -0
  55. data/spec/test_apps/rails-2.3/app/helpers/application_helper.rb +3 -0
  56. data/spec/test_apps/rails-2.3/config/boot.rb +114 -0
  57. data/spec/test_apps/rails-2.3/config/environment.rb +41 -0
  58. data/spec/test_apps/rails-2.3/config/environments/development.rb +17 -0
  59. data/spec/test_apps/rails-2.3/config/environments/production.rb +28 -0
  60. data/spec/test_apps/rails-2.3/config/environments/test.rb +30 -0
  61. data/spec/test_apps/rails-2.3/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/test_apps/rails-2.3/config/initializers/cookie_verification_secret.rb +7 -0
  63. data/spec/test_apps/rails-2.3/config/initializers/inflections.rb +10 -0
  64. data/{test_app → spec/test_apps/rails-2.3}/config/initializers/mime_types.rb +0 -0
  65. data/spec/test_apps/rails-2.3/config/initializers/new_rails_defaults.rb +21 -0
  66. data/spec/test_apps/rails-2.3/config/initializers/session_store.rb +15 -0
  67. data/spec/test_apps/rails-2.3/config/locales/en.yml +5 -0
  68. data/spec/test_apps/rails-2.3/config/routes.rb +43 -0
  69. data/spec/test_apps/rails-2.3/db/seeds.rb +7 -0
  70. data/{test_app → spec/test_apps/rails-2.3}/doc/README_FOR_APP +0 -0
  71. data/spec/test_apps/rails-2.3/lib/tasks/rspec.rake +144 -0
  72. data/spec/test_apps/rails-2.3/public/404.html +30 -0
  73. data/spec/test_apps/rails-2.3/public/422.html +30 -0
  74. data/spec/test_apps/rails-2.3/public/500.html +30 -0
  75. data/{test_app → spec/test_apps/rails-2.3}/public/favicon.ico +0 -0
  76. data/{test_app → spec/test_apps/rails-2.3}/public/images/rails.png +0 -0
  77. data/{test_app → spec/test_apps/rails-2.3}/public/javascripts/application.js +0 -0
  78. data/spec/test_apps/rails-2.3/public/javascripts/controls.js +963 -0
  79. data/spec/test_apps/rails-2.3/public/javascripts/dragdrop.js +973 -0
  80. data/spec/test_apps/rails-2.3/public/javascripts/effects.js +1128 -0
  81. data/spec/test_apps/rails-2.3/public/javascripts/prototype.js +4320 -0
  82. data/{test_app → spec/test_apps/rails-2.3}/public/robots.txt +0 -0
  83. data/spec/test_apps/rails-2.3/script/about +4 -0
  84. data/spec/test_apps/rails-2.3/script/autospec +6 -0
  85. data/spec/test_apps/rails-2.3/script/console +3 -0
  86. data/spec/test_apps/rails-2.3/script/dbconsole +3 -0
  87. data/spec/test_apps/rails-2.3/script/destroy +3 -0
  88. data/spec/test_apps/rails-2.3/script/generate +3 -0
  89. data/spec/test_apps/rails-2.3/script/performance/benchmarker +3 -0
  90. data/spec/test_apps/rails-2.3/script/performance/profiler +3 -0
  91. data/spec/test_apps/rails-2.3/script/plugin +3 -0
  92. data/spec/test_apps/rails-2.3/script/runner +3 -0
  93. data/spec/test_apps/rails-2.3/script/server +3 -0
  94. data/spec/test_apps/rails-2.3/script/spec +10 -0
  95. data/spec/test_apps/rails-2.3/spec/all_spec.rb +5 -0
  96. data/spec/test_apps/rails-2.3/spec/rcov.opts +2 -0
  97. data/spec/test_apps/rails-2.3/spec/spec.opts +4 -0
  98. data/spec/test_apps/rails-2.3/spec/spec_helper.rb +83 -0
  99. data/spec/test_apps/rails-2.3/test/performance/browsing_test.rb +9 -0
  100. data/spec/test_apps/rails-2.3/test/test_helper.rb +38 -0
  101. data/{test_app → spec/test_apps/rails-3.0}/.gitignore +0 -0
  102. data/spec/test_apps/rails-3.0/.rspec +2 -0
  103. data/spec/test_apps/rails-3.0/Gemfile +10 -0
  104. data/{test_app → spec/test_apps/rails-3.0}/README +0 -0
  105. data/{test_app → spec/test_apps/rails-3.0}/Rakefile +0 -0
  106. data/{test_app → spec/test_apps/rails-3.0}/app/controllers/application_controller.rb +0 -0
  107. data/{test_app → spec/test_apps/rails-3.0}/app/controllers/static_controller.rb +0 -0
  108. data/{test_app → spec/test_apps/rails-3.0}/app/helpers/application_helper.rb +0 -0
  109. data/{test_app → spec/test_apps/rails-3.0}/app/helpers/static_helper.rb +0 -0
  110. data/{test_app → spec/test_apps/rails-3.0}/app/views/layouts/application.html.erb +0 -0
  111. data/{test_app → spec/test_apps/rails-3.0}/app/views/static/home.html.erb +0 -0
  112. data/{test_app → spec/test_apps/rails-3.0}/config.ru +0 -0
  113. data/{test_app → spec/test_apps/rails-3.0}/config/application.rb +2 -0
  114. data/{test_app → spec/test_apps/rails-3.0}/config/boot.rb +0 -0
  115. data/{test_app → spec/test_apps/rails-3.0}/config/environment.rb +0 -0
  116. data/{test_app → spec/test_apps/rails-3.0}/config/environments/development.rb +0 -0
  117. data/{test_app → spec/test_apps/rails-3.0}/config/environments/production.rb +0 -0
  118. data/{test_app → spec/test_apps/rails-3.0}/config/environments/test.rb +0 -0
  119. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/backtrace_silencers.rb +0 -0
  120. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/inflections.rb +0 -0
  121. data/spec/test_apps/rails-3.0/config/initializers/mime_types.rb +5 -0
  122. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/secret_token.rb +0 -0
  123. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/session_store.rb +0 -0
  124. data/{test_app → spec/test_apps/rails-3.0}/config/locales/en.yml +0 -0
  125. data/{test_app → spec/test_apps/rails-3.0}/config/routes.rb +0 -0
  126. data/{test_app → spec/test_apps/rails-3.0}/db/schema.rb +0 -0
  127. data/{test_app → spec/test_apps/rails-3.0}/db/seeds.rb +0 -0
  128. data/spec/test_apps/rails-3.0/doc/README_FOR_APP +2 -0
  129. data/{test_app → spec/test_apps/rails-3.0}/lib/tasks/.gitkeep +0 -0
  130. data/{test_app → spec/test_apps/rails-3.0}/public/404.html +0 -0
  131. data/{test_app → spec/test_apps/rails-3.0}/public/422.html +0 -0
  132. data/{test_app → spec/test_apps/rails-3.0}/public/500.html +0 -0
  133. data/{test_app/public/stylesheets/.gitkeep → spec/test_apps/rails-3.0/public/favicon.ico} +0 -0
  134. data/spec/test_apps/rails-3.0/public/images/rails.png +0 -0
  135. data/{test_app → spec/test_apps/rails-3.0}/public/index.html +0 -0
  136. data/spec/test_apps/rails-3.0/public/javascripts/application.js +2 -0
  137. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/controls.js +0 -0
  138. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/dragdrop.js +0 -0
  139. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/effects.js +0 -0
  140. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/prototype.js +0 -0
  141. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/rails.js +0 -0
  142. data/spec/test_apps/rails-3.0/public/robots.txt +5 -0
  143. data/{test_app/vendor/plugins → spec/test_apps/rails-3.0/public/stylesheets}/.gitkeep +0 -0
  144. data/{test_app → spec/test_apps/rails-3.0}/script/rails +0 -0
  145. data/spec/test_apps/rails-3.0/spec/all_spec.rb +5 -0
  146. data/{test_app → spec/test_apps/rails-3.0}/spec/spec_helper.rb +13 -1
  147. data/{test_app → spec/test_apps/rails-3.0}/test/performance/browsing_test.rb +0 -0
  148. data/{test_app → spec/test_apps/rails-3.0}/test/test_helper.rb +0 -0
  149. data/spec/test_apps/rails-3.0/vendor/plugins/.gitkeep +0 -0
  150. data/tasks/cover_me.rake +11 -0
  151. metadata +249 -117
  152. data/lib/engine.rb +0 -16
  153. data/test/executable_test.rb +0 -10
  154. data/test/routes_test.rb +0 -12
  155. data/test/test_helper.rb +0 -8
  156. data/test_app/.rspec +0 -1
  157. data/test_app/Gemfile +0 -7
  158. data/test_app/config/database.yml +0 -22
  159. data/test_app/spec/helpers/static_helper_spec.rb +0 -14
  160. data/test_app/spec/support/integration_example_group.rb +0 -35
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Bushido::Config" do
4
+
5
+ it "should have API version" do
6
+ Bushido::Config.api_version.should be_a_kind_of(String)
7
+ end
8
+
9
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Bushido::Data do
4
+
5
+ describe "add_observer" do
6
+ it "should add an observer" do
7
+ observers = ["sample_observer"]
8
+ Bushido::Data.add_observer(observers.first)
9
+ Bushido::Data.class_variable_get("@@observers").should == observers
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,5 @@
1
+ describe "Hooks" do
2
+
3
+ # TODO
4
+
5
+ end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Bushido::Mailroute" do
4
+ def routes
5
+ Bushido::Mailroute.routes
6
+ end
7
+
8
+ def constraints(route_name)
9
+ Bushido::Mailroute.routes.routes[route_name][:constraints]
10
+ end
11
+
12
+ def params
13
+ Bushido::Mailroute.routes.instance_variable_get("@params")
14
+ end
15
+
16
+ before(:each) do
17
+ Bushido::Mailroute.clear_routes!
18
+
19
+ @standard_email = mail = {'subject' => "Bushido: New modal sign-in", 'from' => "Sean Grove <s@gobushido.com>", 'body' => "this is the new modal sign-in form subscribe: s@busi.do"}
20
+ @reply_email = @standard_email
21
+ @reply_email['subject'] = "RE: Bushido: New modal sign-in"
22
+ end
23
+
24
+ context 'when drawing routes' do
25
+ it 'should start with empty routes' do
26
+ routes.routes.should == {}
27
+ end
28
+
29
+ it 'should create an internal instance of Bushido::Mailroute' do
30
+ routes.routes.should == {}
31
+ Bushido::Mailroute.map { }
32
+ routes.class.should == Bushido::Mailroute
33
+ end
34
+
35
+ it 'should have field matchers for standard email fields' do
36
+ [:from, :sender, :subject, :body].each do |field|
37
+ routes.methods.should include(field)
38
+ end
39
+ end
40
+
41
+ it 'should create a route for each callback added' do
42
+ Bushido::Mailroute.map do |mapper|
43
+ mapper.route('test-call-back') { }
44
+ end
45
+
46
+ routes.routes.should == {"test-call-back"=>{:rules => [], :constraints => []}}
47
+ end
48
+
49
+ it 'should create a route with rules for each callback added' do
50
+ Bushido::Mailroute.map do |mapper|
51
+ mapper.route('test-call-back') { mapper.from('test-sender') }
52
+ end
53
+
54
+ routes.routes.should == {"test-call-back"=>{:rules => [["from", "/test-sender/", nil]], :constraints => []}}
55
+ end
56
+
57
+ it 'should allow composable rules for the same field' do
58
+ Bushido::Mailroute.map do |mapper|
59
+ mapper.route('test-call-back') {
60
+ mapper.body('event on {:day}', {:day => mapper.word })
61
+ mapper.body('at {:time}', {:time => mapper.ampm })
62
+ mapper.body('meet at {:place}', {:place => mapper.words_and_spaces })
63
+ }
64
+ end
65
+
66
+ mail = @standard_email.dup
67
+ mail['body'] = "Hey everyone, there's an event on Wednesday at 7PM. We'll meet at The PubHouse, then we'll head over!"
68
+
69
+ routes.process(mail)
70
+ puts params
71
+ params['day'].should == 'Wednesday'
72
+ params['time'].should == '7PM'
73
+ params['place'].should == 'The PubHouse'
74
+
75
+ end
76
+
77
+ it 'should allow for composable rules in any order' do
78
+ Bushido::Mailroute.map do |mapper|
79
+ mapper.route('test-call-back') {
80
+ mapper.body('event on {:day}', {:day => mapper.word })
81
+ mapper.body('at {:time}', {:time => mapper.ampm })
82
+ mapper.body('meet at {:place}', {:place => mapper.words_and_spaces })
83
+ }
84
+ end
85
+
86
+ mail = @standard_email.dup
87
+ mail['body'] = "Hey everyone, we'll meet at The PubHouse, at 7PM for the event on Wednesday. Then we'll head over!"
88
+
89
+ routes.process(mail)
90
+ puts params
91
+ params['day'].should == 'Wednesday'
92
+ params['time'].should == '7PM'
93
+ params['place'].should == 'The PubHouse'
94
+
95
+ end
96
+ end
97
+
98
+ context 'when adding constraints' do
99
+ it 'should raise an error if an unknown check is specified' do
100
+ lambda {
101
+ Bushido::Mailroute.map do |mapper|
102
+ mapper.route('test-callback') do
103
+ mapper.add_constraint(:reply, :invalid_checker)
104
+ end
105
+ end
106
+ }.should raise_error(StandardError)
107
+ end
108
+
109
+
110
+ it 'should add the constraint for the param field' do
111
+ Bushido::Mailroute.map do |mapper|
112
+ mapper.route('test-callback') do
113
+ mapper.add_constraint(:reply, :required)
114
+ end
115
+ end
116
+
117
+ constraints('test-callback').size.should == 1
118
+ constraints('test-callback').first.class.should == Proc
119
+ end
120
+ end
121
+
122
+ context 'when processing routes' do
123
+ before(:each) do
124
+ Bushido::Mailroute.clear_routes!
125
+
126
+ @standard_email = {'subject' => "Bushido: New modal sign-in", 'from' => "Sean Grove <s@gobushido.com>", 'body' => "this is the new modal sign-in form subscribe: s@busi.do"}
127
+ @reply_email = @standard_email.dup
128
+ @reply_email['subject'] = "RE: Bushido: New modal sign-in"
129
+
130
+
131
+ Bushido::Mailroute.map do |mapper|
132
+ mapper.route('example-callback') do
133
+ mapper.add_constraint(:reply, :required)
134
+
135
+ mapper.subject("^{:prefix}: {:command}",
136
+ :prefix => /[a-zA-Z \-_!]*/,
137
+ :command => /[a-zA-Z \-_!]*/)
138
+ end
139
+ end
140
+ end
141
+
142
+ it "should set 'reply' => nil to the params if not a reply" do
143
+ routes.process(@standard_email)
144
+
145
+ params['reply'].should == nil
146
+ end
147
+
148
+ it "should strip the RE: from a mail subject and set 'reply' => true to the params" do
149
+ routes.process(@reply_email)
150
+ params['reply'].should == true
151
+ params['mail']['subject'].should == @standard_email['subject']
152
+ end
153
+
154
+ it "should fire the callback when finding a successful route" do
155
+ Bushido::Data.should_receive(:fire).with(instance_of(Hash), 'mail_example-callback')
156
+
157
+ routes.process(@reply_email)
158
+ end
159
+
160
+ it "should set 'from_email' and 'from_name' in params if available" do
161
+ @standard_email['from'] = 'Test Sender <test@gobushido.com>'
162
+ routes.process(@standard_email)
163
+ params['from_email'].should == 'test@gobushido.com'
164
+ params['from_name'].should == 'Test Sender'
165
+ end
166
+ end
167
+ end
168
+
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Bushido::Platform" do
4
+ describe "publish_url" do
5
+ it "should return publish url" do
6
+ Bushido::Platform.publish_url.should == "#{Bushido::Platform.host}/apps/#{Bushido::Platform.name}/bus"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Bushido::User" do
4
+
5
+ describe "valid?" do
6
+ it "should post_command to valid_unity_url with email and pass in params" do
7
+ params = {:email => "test@example.com", :pass => "GoBushido"}
8
+ Bushido::Command.should_receive(:post_command).with(Bushido::Base.valid_unity_url, params)
9
+ Bushido::User.valid?(params[:email], params[:pass])
10
+ end
11
+ end
12
+
13
+ describe "exists?" do
14
+ it "should post_command to exists_unity_url with email in params" do
15
+ params = {:email => "test@example.com"}
16
+ Bushido::Command.should_receive(:post_command).with(Bushido::Base.exists_unity_url, params)
17
+ Bushido::User.exists?(params[:email])
18
+ end
19
+ end
20
+
21
+ describe "invite" do
22
+ it "should post_command to invite_unity_url with email in params" do
23
+ params = {:email => "test@example.com"}
24
+ Bushido::Command.should_receive(:post_command).with(Bushido::Base.invite_unity_url, params)
25
+ Bushido::User.invite(params[:email])
26
+ end
27
+ end
28
+
29
+ describe "pending_invites" do
30
+ it "should get_command from pending_invites_url with no params" do
31
+ params = {}
32
+ Bushido::Command.should_receive(:get_command).with(Bushido::Base.pending_invites_unity_url, params)
33
+ Bushido::User.pending_invites()
34
+ end
35
+ end
36
+
37
+ describe "remove" do
38
+ it "should post_command to remove_unity_url with ido_id in params" do
39
+ params = {:ido_id => "some_random_ido_id"}
40
+ Bushido::Command.should_receive(:post_command).with(Bushido::Base.remove_unity_url, params)
41
+ Bushido::User.remove(params[:ido_id])
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,21 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require 'bundler/setup'
4
+ require 'cover_me'
5
+ require 'rails'
6
+ require "rails/test_help"
7
+ require 'rspec/rails'
8
+
9
+ $:.unshift File.dirname(__FILE__)
10
+ $:.unshift File.expand_path('../../lib', __FILE__)
11
+
12
+ require 'bushido'
13
+
14
+ CoverMe.config do |c|
15
+ # where is your project's root:
16
+ c.project.root = File.expand_path('../..', __FILE__)
17
+ end
18
+
19
+ RSpec.configure do |config|
20
+ # not yet decided on what to configure here
21
+ end
@@ -0,0 +1,13 @@
1
+ config/facebook.yml
2
+ config/database.yml
3
+ log/*
4
+ db/*.sqlite3
5
+ db/schema.rb
6
+ *.sw?
7
+ *~
8
+ tmtags
9
+ public/mocks
10
+ bin/*
11
+ permanent/*
12
+ public/store/*
13
+ .DS_Store
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rdoc"
4
+ gem "rails", "2.3.14"
5
+ gem "sqlite3"
6
+
7
+ gem "bushido", :path => "../../../"
8
+
9
+ group :development, :test do
10
+ gem "rspec"
11
+ gem "rspec-rails", "1.3.4"
12
+ gem "capybara", "0.3.9"
13
+ end
@@ -0,0 +1,243 @@
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" templates
7
+ that are primarily responsible for inserting pre-built data in between HTML tags.
8
+ The model contains the "smart" domain objects (such as Account, Product, Person,
9
+ Post) that holds all the business logic and knows how to persist themselves to
10
+ a database. The controller handles the incoming requests (such as Save New Account,
11
+ Update Product, Show Post) by manipulating the model and directing data to the view.
12
+
13
+ In Rails, the model is handled by what's called an object-relational mapping
14
+ layer entitled Active Record. This layer allows you to present the data from
15
+ database rows as objects and embellish these data objects with business logic
16
+ methods. You can read more about Active Record in
17
+ link:files/vendor/rails/activerecord/README.html.
18
+
19
+ The controller and view are handled by the Action Pack, which handles both
20
+ layers by its two parts: Action View and Action Controller. These two layers
21
+ are bundled in a single package due to their heavy interdependence. This is
22
+ unlike the relationship between the Active Record and Action Pack that is much
23
+ more separate. Each of these packages can be used independently outside of
24
+ Rails. You can read more about Action Pack in
25
+ link:files/vendor/rails/actionpack/README.html.
26
+
27
+
28
+ == Getting Started
29
+
30
+ 1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
31
+ and your application name. Ex: rails myapp
32
+ 2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
33
+ 3. Go to http://localhost:3000/ and get "Welcome aboard: You're riding the Rails!"
34
+ 4. Follow the guidelines to start developing your application
35
+
36
+
37
+ == Web Servers
38
+
39
+ By default, Rails will try to use Mongrel if it's are installed when started with script/server, otherwise Rails will use WEBrick, the webserver that ships with Ruby. But you can also use Rails
40
+ with a variety of other web servers.
41
+
42
+ Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
43
+ suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
44
+ getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
45
+ More info at: http://mongrel.rubyforge.org
46
+
47
+ Say other Ruby web servers like Thin and Ebb or regular web servers like Apache or LiteSpeed or
48
+ Lighttpd or IIS. The Ruby web servers are run through Rack and the latter can either be setup to use
49
+ FCGI or proxy to a pack of Mongrels/Thin/Ebb servers.
50
+
51
+ == Apache .htaccess example for FCGI/CGI
52
+
53
+ # General Apache options
54
+ AddHandler fastcgi-script .fcgi
55
+ AddHandler cgi-script .cgi
56
+ Options +FollowSymLinks +ExecCGI
57
+
58
+ # If you don't want Rails to look in certain directories,
59
+ # use the following rewrite rules so that Apache won't rewrite certain requests
60
+ #
61
+ # Example:
62
+ # RewriteCond %{REQUEST_URI} ^/notrails.*
63
+ # RewriteRule .* - [L]
64
+
65
+ # Redirect all requests not available on the filesystem to Rails
66
+ # By default the cgi dispatcher is used which is very slow
67
+ #
68
+ # For better performance replace the dispatcher with the fastcgi one
69
+ #
70
+ # Example:
71
+ # RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
72
+ RewriteEngine On
73
+
74
+ # If your Rails application is accessed via an Alias directive,
75
+ # then you MUST also set the RewriteBase in this htaccess file.
76
+ #
77
+ # Example:
78
+ # Alias /myrailsapp /path/to/myrailsapp/public
79
+ # RewriteBase /myrailsapp
80
+
81
+ RewriteRule ^$ index.html [QSA]
82
+ RewriteRule ^([^.]+)$ $1.html [QSA]
83
+ RewriteCond %{REQUEST_FILENAME} !-f
84
+ RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
85
+
86
+ # In case Rails experiences terminal errors
87
+ # Instead of displaying this message you can supply a file here which will be rendered instead
88
+ #
89
+ # Example:
90
+ # ErrorDocument 500 /500.html
91
+
92
+ ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
93
+
94
+
95
+ == Debugging Rails
96
+
97
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
98
+ will help you debug it and get it back on the rails.
99
+
100
+ First area to check is the application log files. Have "tail -f" commands running
101
+ on the server.log and development.log. Rails will automatically display debugging
102
+ and runtime information to these files. Debugging info will also be shown in the
103
+ browser on requests from 127.0.0.1.
104
+
105
+ You can also log your own messages directly into the log file from your code using
106
+ the Ruby logger class from inside your controllers. Example:
107
+
108
+ class WeblogController < ActionController::Base
109
+ def destroy
110
+ @weblog = Weblog.find(params[:id])
111
+ @weblog.destroy
112
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
113
+ end
114
+ end
115
+
116
+ The result will be a message in your log file along the lines of:
117
+
118
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
119
+
120
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
121
+
122
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
123
+
124
+ * The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
125
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
126
+
127
+ These two online (and free) books will bring you up to speed on the Ruby language
128
+ and also on programming in general.
129
+
130
+
131
+ == Debugger
132
+
133
+ Debugger support is available through the debugger command when you start your Mongrel or
134
+ Webrick server with --debugger. This means that you can break out of execution at any point
135
+ in the code, investigate and change the model, AND then resume execution!
136
+ You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
137
+ Example:
138
+
139
+ class WeblogController < ActionController::Base
140
+ def index
141
+ @posts = Post.find(:all)
142
+ debugger
143
+ end
144
+ end
145
+
146
+ So the controller will accept the action, run the first line, then present you
147
+ with a IRB prompt in the server window. Here you can do things like:
148
+
149
+ >> @posts.inspect
150
+ => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
151
+ #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
152
+ >> @posts.first.title = "hello from a debugger"
153
+ => "hello from a debugger"
154
+
155
+ ...and even better is that you can examine how your runtime objects actually work:
156
+
157
+ >> f = @posts.first
158
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
159
+ >> f.
160
+ Display all 152 possibilities? (y or n)
161
+
162
+ Finally, when you're ready to resume execution, you enter "cont"
163
+
164
+
165
+ == Console
166
+
167
+ You can interact with the domain model by starting the console through <tt>script/console</tt>.
168
+ Here you'll have all parts of the application configured, just like it is when the
169
+ application is running. You can inspect domain models, change values, and save to the
170
+ database. Starting the script without arguments will launch it in the development environment.
171
+ Passing an argument will specify a different environment, like <tt>script/console production</tt>.
172
+
173
+ To reload your controllers and models after launching the console run <tt>reload!</tt>
174
+
175
+ == dbconsole
176
+
177
+ You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
178
+ You would be connected to the database with the credentials defined in database.yml.
179
+ Starting the script without arguments will connect you to the development database. Passing an
180
+ argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
181
+ Currently works for mysql, postgresql and sqlite.
182
+
183
+ == Description of Contents
184
+
185
+ app
186
+ Holds all the code that's specific to this particular application.
187
+
188
+ app/controllers
189
+ Holds controllers that should be named like weblogs_controller.rb for
190
+ automated URL mapping. All controllers should descend from ApplicationController
191
+ which itself descends from ActionController::Base.
192
+
193
+ app/models
194
+ Holds models that should be named like post.rb.
195
+ Most models will descend from ActiveRecord::Base.
196
+
197
+ app/views
198
+ Holds the template files for the view that should be named like
199
+ weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby
200
+ syntax.
201
+
202
+ app/views/layouts
203
+ Holds the template files for layouts to be used with views. This models the common
204
+ header/footer method of wrapping views. In your views, define a layout using the
205
+ <tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
206
+ call <% yield %> to render the view using this layout.
207
+
208
+ app/helpers
209
+ Holds view helpers that should be named like weblogs_helper.rb. These are generated
210
+ for you automatically when using script/generate for controllers. Helpers can be used to
211
+ wrap functionality for your views into methods.
212
+
213
+ config
214
+ Configuration files for the Rails environment, the routing map, the database, and other dependencies.
215
+
216
+ db
217
+ Contains the database schema in schema.rb. db/migrate contains all
218
+ the sequence of Migrations for your schema.
219
+
220
+ doc
221
+ This directory is where your application documentation will be stored when generated
222
+ using <tt>rake doc:app</tt>
223
+
224
+ lib
225
+ Application specific libraries. Basically, any kind of custom code that doesn't
226
+ belong under controllers, models, or helpers. This directory is in the load path.
227
+
228
+ public
229
+ The directory available for the web server. Contains subdirectories for images, stylesheets,
230
+ and javascripts. Also contains the dispatchers and the default HTML files. This should be
231
+ set as the DOCUMENT_ROOT of your web server.
232
+
233
+ script
234
+ Helper scripts for automation and generation.
235
+
236
+ test
237
+ Unit and functional tests along with fixtures. When using the script/generate scripts, template
238
+ test files will be generated for you and placed in this directory.
239
+
240
+ vendor
241
+ External libraries that the application depends on. Also includes the plugins subdirectory.
242
+ If the app has frozen rails, those gems also go here, under vendor/rails/.
243
+ This directory is in the load path.