axel 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +21 -0
  3. data/.octopolo.yml +2 -0
  4. data/.rspec +2 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +23 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +271 -0
  11. data/Rakefile +13 -0
  12. data/app/models/axel/api_proxy.rb +86 -0
  13. data/app/models/axel/associations/base.rb +64 -0
  14. data/app/models/axel/associations/belongs_to.rb +43 -0
  15. data/app/models/axel/associations/has_many.rb +29 -0
  16. data/app/models/axel/associations/has_one.rb +30 -0
  17. data/app/models/axel/payload.rb +4 -0
  18. data/app/models/axel/payload/base.rb +107 -0
  19. data/app/models/axel/payload/errors.rb +62 -0
  20. data/app/models/axel/payload/metadata.rb +57 -0
  21. data/app/models/axel/querier.rb +166 -0
  22. data/app/models/axel/router.rb +119 -0
  23. data/app/models/axel/service_resource.rb +4 -0
  24. data/app/models/axel/service_resource/associations.rb +80 -0
  25. data/app/models/axel/service_resource/attributes.rb +23 -0
  26. data/app/models/axel/service_resource/automatic_resource.rb +23 -0
  27. data/app/models/axel/service_resource/base.rb +47 -0
  28. data/app/models/axel/service_resource/builder.rb +40 -0
  29. data/app/models/axel/service_resource/inspects.rb +17 -0
  30. data/app/models/axel/service_resource/payload_parser.rb +46 -0
  31. data/app/models/axel/service_resource/queries.rb +25 -0
  32. data/app/models/axel/service_resource/requesters.rb +49 -0
  33. data/app/models/axel/service_resource/routes.rb +19 -0
  34. data/app/models/axel/service_resource/typhoid_extensions.rb +134 -0
  35. data/app/views/axel/base/empty.json.erb +0 -0
  36. data/app/views/axel/base/empty.xml.builder +0 -0
  37. data/app/views/layouts/axel.json.jbuilder +7 -0
  38. data/app/views/layouts/axel.xml.builder +12 -0
  39. data/axel.gemspec +42 -0
  40. data/lib/axel.rb +56 -0
  41. data/lib/axel/application_extensions.rb +13 -0
  42. data/lib/axel/application_helper.rb +27 -0
  43. data/lib/axel/base_controller.rb +6 -0
  44. data/lib/axel/cascadable_attribute.rb +33 -0
  45. data/lib/axel/configurations/resource.rb +29 -0
  46. data/lib/axel/configurations/service.rb +28 -0
  47. data/lib/axel/configurator.rb +54 -0
  48. data/lib/axel/configurators/services.rb +29 -0
  49. data/lib/axel/controller_base.rb +27 -0
  50. data/lib/axel/controller_helpers.rb +209 -0
  51. data/lib/axel/controller_parameters.rb +32 -0
  52. data/lib/axel/engine.rb +14 -0
  53. data/lib/axel/inspector.rb +91 -0
  54. data/lib/axel/payload/remote_error.rb +14 -0
  55. data/lib/axel/request_options.rb +26 -0
  56. data/lib/axel/uri.rb +47 -0
  57. data/lib/axel/version.rb +3 -0
  58. data/lib/generators/axel/install_generator.rb +16 -0
  59. data/lib/generators/templates/README.md +22 -0
  60. data/lib/generators/templates/axel.rb +81 -0
  61. data/script/rails +5 -0
  62. data/spec/controllers/pages_controller_spec.rb +217 -0
  63. data/spec/dummy/README.rdoc +261 -0
  64. data/spec/dummy/Rakefile +7 -0
  65. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  66. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  67. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  68. data/spec/dummy/app/controllers/pages_controller.rb +6 -0
  69. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  70. data/spec/dummy/app/mailers/.gitkeep +0 -0
  71. data/spec/dummy/app/models/.gitkeep +0 -0
  72. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  73. data/spec/dummy/config.ru +4 -0
  74. data/spec/dummy/config/application.rb +62 -0
  75. data/spec/dummy/config/boot.rb +10 -0
  76. data/spec/dummy/config/database.yml +25 -0
  77. data/spec/dummy/config/environment.rb +5 -0
  78. data/spec/dummy/config/environments/development.rb +37 -0
  79. data/spec/dummy/config/environments/production.rb +67 -0
  80. data/spec/dummy/config/environments/test.rb +37 -0
  81. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  82. data/spec/dummy/config/initializers/inflections.rb +15 -0
  83. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  84. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  85. data/spec/dummy/config/initializers/session_store.rb +8 -0
  86. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  87. data/spec/dummy/config/locales/en.yml +5 -0
  88. data/spec/dummy/config/routes.rb +3 -0
  89. data/spec/dummy/db/development.sqlite3 +0 -0
  90. data/spec/dummy/db/test.sqlite3 +0 -0
  91. data/spec/dummy/lib/assets/.gitkeep +0 -0
  92. data/spec/dummy/log/.gitignore +1 -0
  93. data/spec/dummy/log/.gitkeep +0 -0
  94. data/spec/dummy/public/404.html +26 -0
  95. data/spec/dummy/public/422.html +26 -0
  96. data/spec/dummy/public/500.html +25 -0
  97. data/spec/dummy/public/favicon.ico +0 -0
  98. data/spec/dummy/script/rails +6 -0
  99. data/spec/envelope_integration_check.rb +96 -0
  100. data/spec/helpers/axel/application_helper_spec.rb +31 -0
  101. data/spec/lib/axel/associations/base_spec.rb +28 -0
  102. data/spec/lib/axel/associations/belongs_to_spec.rb +62 -0
  103. data/spec/lib/axel/associations/has_many_spec.rb +23 -0
  104. data/spec/lib/axel/associations/has_one_spec.rb +23 -0
  105. data/spec/lib/axel/configurations/resource_spec.rb +15 -0
  106. data/spec/lib/axel/configurations/service_spec.rb +31 -0
  107. data/spec/lib/axel/configurator_spec.rb +26 -0
  108. data/spec/lib/axel/configurators/services_spec.rb +37 -0
  109. data/spec/lib/axel/controller_base_spec.rb +16 -0
  110. data/spec/lib/axel/controller_parameters_spec.rb +31 -0
  111. data/spec/lib/axel/inspector_spec.rb +45 -0
  112. data/spec/lib/axel/request_options_spec.rb +50 -0
  113. data/spec/lib/axel/uri_spec.rb +42 -0
  114. data/spec/lib/axel_spec.rb +64 -0
  115. data/spec/models/axel/api_proxy_spec.rb +66 -0
  116. data/spec/models/axel/payload/errors_spec.rb +165 -0
  117. data/spec/models/axel/payload/metadata_spec.rb +141 -0
  118. data/spec/models/axel/querier_spec.rb +158 -0
  119. data/spec/models/axel/router_spec.rb +115 -0
  120. data/spec/models/axel/service_resource/base_spec.rb +244 -0
  121. data/spec/models/axel/service_resource/builder_spec.rb +64 -0
  122. data/spec/models/axel/service_resource/payload_parser_spec.rb +60 -0
  123. data/spec/spec_helper.rb +39 -0
  124. data/spec/support/address.rb +5 -0
  125. data/spec/support/persona.rb +15 -0
  126. data/spec/support/user.rb +6 -0
  127. data/spec/views/axel/base/empty_spec.rb +34 -0
  128. metadata +508 -0
@@ -0,0 +1,47 @@
1
+ module Axel
2
+ class Uri < Typhoid::Uri
3
+ def config
4
+ (
5
+ Axel._config.environment_uri_config || {}
6
+ ).with_indifferent_access
7
+ end
8
+
9
+ def to(config_key, staging_number = nil)
10
+ new_base = apply_suffix dashed_app_name,
11
+ staging_number,
12
+ config_for(config_key)[:host]
13
+ base.host = new_base
14
+ base.scheme = config_for(config_key)[:scheme]
15
+ self
16
+ end
17
+
18
+ def app_name
19
+ dashed_app_name.underscore.humanize.titleize
20
+ end
21
+
22
+ def dashed_app_name
23
+ base.host.to_s.gsub(/\..*$/, "")
24
+ end
25
+
26
+ def config_for(config_key)
27
+ config[config_key] || default_handler
28
+ end
29
+ private :config_for
30
+
31
+ def default_handler
32
+ config[:default] || { host: ".dev", scheme: "http" }
33
+ end
34
+ private :default_handler
35
+
36
+ def apply_suffix(base_name, staging_number, config_handler)
37
+ config_handler = convert_string_to_proc config_handler
38
+ config_handler.call base_name, staging_number
39
+ end
40
+ private :apply_suffix
41
+
42
+ def convert_string_to_proc(handler)
43
+ handler.is_a?(Proc) ? handler : Proc.new { |base, n| "#{base}#{handler}" }
44
+ end
45
+ private :convert_string_to_proc
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Axel
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ module Axel
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Creates a Axel initializer."
6
+
7
+ def copy_initializer
8
+ template "axel.rb", "config/initializers/axel.rb"
9
+ end
10
+
11
+ def show_readme
12
+ readme "README.md" if behavior == :invoke
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ --------------------------------------
2
+
3
+ # Welcome to Axel
4
+
5
+ ## Configuration
6
+
7
+ In your `app/controllers/application_controller.rb` change to:
8
+
9
+ ```ruby
10
+ class ApplicationController < Axel::BaseController
11
+ layout "axel" # For json and xml API output
12
+ #.......
13
+ #.......
14
+ end
15
+ ```
16
+
17
+ Please see `config/initializers/axel.rb` to configure inter-service connection.
18
+
19
+ You'll also want to have a look at the README.md found at the project homepage:
20
+ http://github.com/tstmedia/axel. There you'll find more information about what the
21
+ `Axel::BaseController` can do for your API controllers and how to operate inter-
22
+ service requests.
@@ -0,0 +1,81 @@
1
+ Axel.config do |config|
2
+ # The API Proxy's `/registry` endpoint holds information about
3
+ # what resources connect to what services and where. We can leverage
4
+ # that endpoint to automatically configure objects for data retrieval
5
+ # and creation. To start, you'll want to configure the API Proxy location for
6
+ # automatically retrieving resource location configs:
7
+ #
8
+ # config.set_proxy_url "http://api.your-platform.com"
9
+ #
10
+ # Services may be added manually:
11
+ #
12
+ # config.add_service "github", "http://api.github.com/v3/"
13
+ #
14
+ # Service locations can be forced to a specific URL:
15
+ #
16
+ # config.service_configs[:user_service].url = "http://localhost:1337"
17
+ #
18
+ # Because of the API Proxy, many resources should be available automatically.
19
+ # You can then start setting what attributes you want easier access
20
+ # to for certain resources:
21
+ #
22
+ # config.resources[:users].attributes << :user_name
23
+ # config.resources[:personas].attributes = [:first_name, :last_name]
24
+ #
25
+ # While many resources are automatically created, you can also manually add
26
+ # resources:
27
+ #
28
+ # config.add_resource "github", "repos" # `, service: { url: ".." } needed if the service was not added previously
29
+ #
30
+ # Resources can set their paths manually:
31
+ #
32
+ # config.resources[:personas].path = "people"
33
+ #
34
+ # The Api proxy config and configs for altering resources/services will make
35
+ # it so when you inherit from Axel::ServiceResource::Base you get some
36
+ # free configuration. Example:
37
+ #
38
+ # module MyApp
39
+ # class User
40
+ # resource # => <#Axel::Configurations::Resource users service: #<Axel::Configurations::Service user-service>>
41
+ # path # => "/users"
42
+ # site # => "https://user-service.your-platform.com"
43
+ # end
44
+ # end
45
+ #
46
+ # To configure URI conversion to certain environment URIs. Strategies
47
+ # are either suffix strings or Procs that accept a base-name (usually app name)
48
+ # and a number (for staging numbers). Define strategies here like so:
49
+ #
50
+ # config.environment_uri_config = {
51
+ # dev: {
52
+ # host: ".dev",
53
+ # scheme: "http"
54
+ # },
55
+ # stage: {
56
+ # host: ->(base, n) { "#{base}.stage#{n}.ngin-staging.com" },
57
+ # scheme: "https"
58
+ # },
59
+ # prod: {
60
+ # host: ".your-platform.com",
61
+ # scheme: "https"
62
+ # }
63
+ # }
64
+ #
65
+ # This now means you can do:
66
+ #
67
+ # Axel::Uri.new("http://user-service.dev/users").to(:prod).to_s
68
+ # # => "http://user-service.your-platform.com/users"
69
+ #
70
+ # With that set you can quickly convert all URLs in objects that use axel
71
+ # for resource location to another environment:
72
+ #
73
+ # config.set_environment :stage, 2
74
+ #
75
+ # Do you use rails-api instead of Rails proper? Set:
76
+ #
77
+ # config.uses_rails_api = true # Default: false
78
+ #
79
+ # This ensures that you use the ActionController::API rather than Rails' default
80
+ # ActionController::Base
81
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_PATH = File.expand_path('../..', __FILE__)
5
+ load File.expand_path('../../spec/dummy/script/rails', __FILE__)
@@ -0,0 +1,217 @@
1
+ require 'spec_helper'
2
+ Page = Class.new
3
+ describe PagesController do
4
+ it "gets json" do
5
+ get :show, format: :json
6
+ subject.send(:_layout).should == "axel"
7
+ end
8
+
9
+ it "gets xml" do
10
+ get :show, format: :xml
11
+ subject.send(:_layout).should == "axel"
12
+ end
13
+
14
+ it "doesn't get any" do
15
+ get :show # This helper defailts to html which we haven't specified to handle
16
+ response.code.should == "406"
17
+ end
18
+
19
+ its(:metadata) { should be_a Axel::Payload::Metadata }
20
+
21
+ context "rescue error" do
22
+ context "list of errors and 403" do
23
+ let(:messages) { ["Broke 1", "Broke 2"] }
24
+ let(:params) { { format: :json } }
25
+ before do
26
+ subject.stub respond_to: true
27
+ subject.rescue_error messages: messages, status: :forbidden
28
+ end
29
+
30
+ it "sets error object" do
31
+ subject.errors.display.should == { status: 403, messages: messages }
32
+ end
33
+ end
34
+ end
35
+
36
+ context "query helpers" do
37
+ its(:query_params) { should be_a HashWithIndifferentAccess }
38
+ its(:post_params) { should be_a HashWithIndifferentAccess }
39
+ end
40
+
41
+ context "formatters" do
42
+ context "format" do
43
+ its(:format) { should == :json }
44
+ its(:render_nil_format) { should be_nil }
45
+ context "with format param" do
46
+ before { subject.stub params: { format: :xml } }
47
+
48
+ its(:format) { should == :xml }
49
+ its(:render_nil_format) { should == "" }
50
+ end
51
+ end
52
+ end
53
+
54
+ context "rabl render" do
55
+ let(:string) { "" }
56
+ it "asks rabl to render" do
57
+ Rabl.should_receive(:render).with string, "view", view_path: "app/views", scope: subject
58
+ subject.rabl_render string, "view"
59
+ end
60
+ end
61
+
62
+ context "xml_clean" do
63
+ let(:non_dirty) { <<-XML
64
+ <document>
65
+ <metadata>
66
+ <current_user nil="true"/>
67
+ </metadata>
68
+ <result>
69
+ </result>
70
+ </document>
71
+ XML
72
+ }
73
+
74
+ let(:dirty) { <<-XML
75
+ <document>
76
+ <metadata>
77
+ <?xml version="1.0" encoding="UTF-8"?>
78
+ <hash>
79
+ <current_user nil="true"/>
80
+ </hash>
81
+ </metadata>
82
+ <result>
83
+ </result>
84
+ </document>
85
+ XML
86
+ }
87
+
88
+ let(:cleaned) { <<-XML
89
+ <document>
90
+ <metadata>
91
+
92
+ <current_user nil="true"/>
93
+ </metadata>
94
+ <result>
95
+ </result>
96
+ </document>
97
+ XML
98
+ }
99
+
100
+ it "doesn't touch the non_dirty" do
101
+ subject.xml_clean(non_dirty).should == non_dirty.strip
102
+ end
103
+
104
+ it "cleans the dirty payload" do
105
+ subject.xml_clean(dirty).should == cleaned.strip
106
+ end
107
+ end
108
+
109
+ context "render action" do
110
+ context "method" do
111
+ let(:formatter) { double }
112
+ it "calls formatter with action" do
113
+ subject.should_receive(:respond_with).and_yield formatter
114
+ subject.should_receive(:render).with({ action: :show}).twice
115
+ formatter.should_receive(:json).and_yield
116
+ formatter.should_receive(:xml).and_yield
117
+ subject.respond_with_action(:show)
118
+ end
119
+ end
120
+
121
+ context "id param" do
122
+ let(:page) { Page.new }
123
+ context "found record" do
124
+ before do
125
+ subject.stub params: { id: 1 }
126
+ Page.should_receive(:where).with(:id => 1).and_return [page]
127
+ end
128
+
129
+ its(:find_resource) { should == page }
130
+ end
131
+
132
+ context "no record" do
133
+ let(:page) { Page.new }
134
+ before do
135
+ subject.stub params: { id: 1 }
136
+ Page.should_receive(:where).with(:id => 1).and_return []
137
+ end
138
+
139
+ it "raises not found" do
140
+ expect { subject.find_resource }.to raise_error Axel::RecordNotFound
141
+ end
142
+ end
143
+
144
+ context "with custom finder and value" do
145
+ before do
146
+ subject.stub params: { id: 1 }
147
+ Page.should_not_receive(:where).with(:id => 1)
148
+ Page.should_receive(:where).with(:name => "two").and_return [page]
149
+ end
150
+
151
+ it "raises not found" do
152
+ subject.find_resource(finder: :name, value: "two").should == page
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ context "drop meta" do
159
+ context "doesn't drop meta" do
160
+ its(:drop_meta?) { should be_falsey }
161
+ end
162
+
163
+ context "drops meta" do
164
+ before do
165
+ subject.drop_meta!
166
+ end
167
+
168
+ its(:drop_meta?) { should be_truthy }
169
+ end
170
+ end
171
+
172
+ context "object_params" do
173
+ context "nested in object name" do
174
+ before do
175
+ subject.stub post_params: { page: { id: 1 } }.with_indifferent_access
176
+ end
177
+
178
+ its(:object_params) { should == { id: 1 }.with_indifferent_access }
179
+ end
180
+
181
+ context "not nested in name" do
182
+ before do
183
+ subject.stub post_params: { id: 1 }.with_indifferent_access
184
+ end
185
+
186
+ its(:object_params) { should == { id: 1 }.with_indifferent_access }
187
+ end
188
+ end
189
+
190
+ context "object_name" do
191
+ its(:object_name) { should == "page" }
192
+ end
193
+
194
+ context "force_ssl!" do
195
+ its(:force_ssl!) { should be_truthy }
196
+
197
+ context "productionish" do
198
+ before do
199
+ Rails::Application.stub productionish?: true
200
+ end
201
+
202
+ it "raises force ssl error" do
203
+ expect { subject.force_ssl! }.to raise_error Axel::ForceSSL
204
+ end
205
+ end
206
+ end
207
+
208
+ describe "safe_json_load" do
209
+ it "loads a string of json" do
210
+ subject.safe_json_load("{ \"user\": true }").should == { "user" => true }
211
+ end
212
+
213
+ it "returns nil for no json" do
214
+ subject.safe_json_load(nil).should == nil
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,261 @@
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-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</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
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.