lookout-data_fabric 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG +79 -0
  3. data/README.rdoc +125 -0
  4. data/Rakefile +93 -0
  5. data/TESTING.rdoc +30 -0
  6. data/example30/Gemfile +5 -0
  7. data/example30/Gemfile.lock +79 -0
  8. data/example30/README +256 -0
  9. data/example30/Rakefile +52 -0
  10. data/example30/app/controllers/accounts_controller.rb +22 -0
  11. data/example30/app/controllers/application_controller.rb +39 -0
  12. data/example30/app/controllers/figments_controller.rb +8 -0
  13. data/example30/app/helpers/application_helper.rb +2 -0
  14. data/example30/app/models/account.rb +3 -0
  15. data/example30/app/models/figment.rb +4 -0
  16. data/example30/app/views/accounts/index.html.erb +47 -0
  17. data/example30/app/views/layouts/application.html.erb +8 -0
  18. data/example30/config.ru +4 -0
  19. data/example30/config/application.rb +42 -0
  20. data/example30/config/boot.rb +13 -0
  21. data/example30/config/database.yml +21 -0
  22. data/example30/config/environment.rb +5 -0
  23. data/example30/config/environments/development.rb +26 -0
  24. data/example30/config/environments/production.rb +49 -0
  25. data/example30/config/environments/test.rb +35 -0
  26. data/example30/config/initializers/backtrace_silencers.rb +7 -0
  27. data/example30/config/initializers/inflections.rb +10 -0
  28. data/example30/config/initializers/mime_types.rb +5 -0
  29. data/example30/config/initializers/secret_token.rb +7 -0
  30. data/example30/config/initializers/session_store.rb +8 -0
  31. data/example30/config/locales/en.yml +5 -0
  32. data/example30/config/routes.rb +65 -0
  33. data/example30/db/migrate/20080702154628_create_accounts.rb +14 -0
  34. data/example30/db/migrate/20080702154820_create_figments.rb +14 -0
  35. data/example30/db/seeds.rb +7 -0
  36. data/example30/script/rails +6 -0
  37. data/example30/test/fixtures/accounts.yml +7 -0
  38. data/example30/test/functional/accounts_controller_test.rb +12 -0
  39. data/example30/test/integration/account_figments_test.rb +97 -0
  40. data/example30/test/performance/browsing_test.rb +9 -0
  41. data/example30/test/test_helper.rb +13 -0
  42. data/lib/data_fabric.rb +107 -0
  43. data/lib/data_fabric/connection_proxy.rb +176 -0
  44. data/lib/data_fabric/extensions.rb +39 -0
  45. data/lib/data_fabric/version.rb +5 -0
  46. data/test/connection_test.rb +142 -0
  47. data/test/database.yml +24 -0
  48. data/test/database.yml.mysql +36 -0
  49. data/test/database_test.rb +51 -0
  50. data/test/shard_test.rb +58 -0
  51. data/test/test_helper.rb +28 -0
  52. data/test/thread_test.rb +83 -0
  53. metadata +172 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWI0YzJiY2Q3NDg2NWY2NjY5ODdkMzJiNDNkMDUxNTA2YTY5MzgyOQ==
5
+ data.tar.gz: !binary |-
6
+ NjU1MDY3Y2ViNjMxNWQ0NDM5YWE3NmM5OTM2MTg4NTg5MWFjMWE4OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZDBiMmExYjEzNmRkMTFmMGQ5ZjBjNzc5YmQ1MzU2NmFmMTJhYzk0NDgzMmFk
10
+ ZTU3Njg1M2IyNWFlODZlZDQzYTkzNWEyY2NjYTZlYjNjNzcyOGM1MjJhZTAy
11
+ YTMxNjBkMTM5OTdlOGFjN2IxMmRjMTg0YWNlZmM2NTQ0YTAyNjI=
12
+ data.tar.gz: !binary |-
13
+ MmNlNjAwMmU1ZjRlY2ZlZTYzNGIzMDI4OTBmNmE0OTliNThkYzAxNjgyOWYw
14
+ Nzk2YTM4NWE1YTdiMmEwNzY1ZmEzNjM0NGIyNWE2ZDgwZDBlNGZlYWQ4M2U4
15
+ MjljNzdjNjcyOTU2YzMyZDA4MmFhZjI3MmVhZTY4M2Y4Y2I0ZTU=
@@ -0,0 +1,79 @@
1
+ DataFabric changelog
2
+
3
+ v1.3.3 - 2011-10-29
4
+
5
+ - Added ConnectionProxy#respond_to? which delegates to underlying connection
6
+ (Paul Gross and Dan Manges)
7
+ - Remove specific handling of nested transactions and let the underlying
8
+ connection handle it (Paul Gross and Dan Manges)
9
+
10
+ v1.3.2 - 2011-06-02
11
+
12
+ - Fix problem fetching spec for connection pool.
13
+ - Fix issues with Rails 3.1.
14
+
15
+ v1.3.1 - 2011-02-10
16
+
17
+ - Add some missing ConnectionPool methods (e.g. clear_reloadable_connections!)
18
+
19
+ v1.3.0 - 2010-11-22
20
+
21
+ - Major implementation refactoring
22
+ - Fix issues under Rails 3.0. Tested with 2.3.10, 3.0.0 and 3.0.3.
23
+ - Drop support for Rails 2.2 (may work but untested)
24
+ - Remove jeweler dependency
25
+
26
+ v1.2.7 - 2010-08-10
27
+
28
+ - NO FUNCTIONAL CHANGES.
29
+ - Fix test suite under Ruby 1.9.2.
30
+ - Update example22 application to example23 and Rails 2.3.
31
+
32
+ v1.2.6 - Never released
33
+
34
+ - STI fixes.
35
+
36
+ v1.2.5 - 2009-03-28
37
+
38
+ - data_fabric should now work as a gem or plugin.
39
+ - Move from echoe to Jeweler for gem maintenance.
40
+
41
+ v1.2.4 - 2009-03-09
42
+
43
+ - Add FiveRuns Dash recipe for data_fabric. See http://dash.fiveruns.com for details.
44
+ - Fixed incorrect remove_connection method signature.
45
+ - The official data_fabric repository is now http://github.com/mperham/data_fabric.
46
+
47
+ v1.2.3 - 2009-02-05
48
+
49
+ - Release connections at the end of each Rails action. If you see 5 second
50
+ pauses in your application, it's because the connection pool is timing out
51
+ and releasing checked out connections.
52
+
53
+ - NB: DataFabric passes all tests against Rails 2.3.0.
54
+
55
+ v1.2.2 - 2008-12-16
56
+
57
+ - Fix multithreading issue with AR 2.2.
58
+
59
+ v1.2.1 - 2008-12-01
60
+
61
+ - Clean up gem of superfluous files, trims gem size from 160k to 50k.
62
+ No functional changes.
63
+
64
+ v1.2.0 - 2008-12-01
65
+
66
+ - Now supports ActiveRecord 2.2.
67
+
68
+ v1.1.0 - 2008-11-22
69
+
70
+ - Cache connections so we don't have to reconnect constantly. (Justin Balthrop, Geni)
71
+ - Clean up logging API (not backwards-compatible)
72
+ - Verify wrapped connections automatically so cached mysql connections older than 8 hours
73
+ don't die due to timeout.
74
+ - Please note this version is not Rails 2.2-compatible. DataFabric 2.0 will be.
75
+
76
+
77
+ Detailed commit history:
78
+
79
+ http://github.com/mperham/data_fabric/commits/master
@@ -0,0 +1,125 @@
1
+ = data_fabric
2
+
3
+ ---------------------------
4
+ I'm looking for someone to take over maintenance and responsibility
5
+ for data_fabric. If you use data_fabric or have an interest in its ongoing
6
+ development, please let me know.
7
+ ---------------------------
8
+
9
+
10
+ DataFabric provides flexible database connection switching for ActiveRecord.
11
+
12
+ We needed two features to scale our mysql database: application-level sharding and
13
+ master/slave replication. Sharding is the process of splitting a dataset across many
14
+ independent databases. This often happens based on geographical region (e.g.
15
+ craigslist) or category (e.g. ebay). Replication provides a near-real-time copy
16
+ of a database which can be used for fault tolerance and to reduce load on the master
17
+ node. Combined, you get a scalable database solution which does not require huge
18
+ hardware to scale to huge volumes. Or: DPAYEIOB - don't put all your eggs in one
19
+ basket. :-)
20
+
21
+ == Installation
22
+
23
+ gem install data_fabric
24
+
25
+ then in config/environment.rb, add a config.gem entry:
26
+
27
+ Rails::Initializer.run do |config|
28
+ ...
29
+ config.gem 'data_fabric'
30
+
31
+ or add data_fabric to your Gemfile:
32
+
33
+ gem 'data_fabric'
34
+
35
+ data_fabric has been tested with Rails 2.3.10 and 3.0.7.
36
+
37
+ == How does it work?
38
+
39
+ You describe the topology for your database infrastructure in your model(s).
40
+ Different models can use different topologies.
41
+
42
+ class MyHugeVolumeOfDataModel < ActiveRecord::Base
43
+ data_fabric :replicated => true, :shard_by => :city
44
+ end
45
+
46
+ There are four supported modes of operation, depending on the options given to the
47
+ data_fabric method. The plugin will look for connections in your
48
+ config/database.yml with the following convention:
49
+
50
+ No connection topology:
51
+ #{environment} - this is the default, as with ActiveRecord, e.g. "production"
52
+
53
+ data_fabric :replicated => true
54
+ #{environment}_#{role} - no sharding, just replication, where role is "master" or "slave", e.g. "production_master"
55
+
56
+ data_fabric :shard_by => :city
57
+ #{group}_#{shard}_#{environment} - sharding, no replication, e.g. "city_austin_production"
58
+
59
+ data_fabric :replicated => true, :shard_by => :city
60
+ #{group}_#{shard}_#{environment}_#{role} - sharding with replication, e.g. "city_austin_production_master"
61
+
62
+
63
+ When marked as replicated, all write and transactional operations for the model
64
+ go to the master, whereas read operations go to the slave.
65
+
66
+ Since sharding is an application-level concern, your application must set the shard
67
+ to use based on the current request or environment. The current shard
68
+ is set on a thread local variable. For example, you can set the shard in an
69
+ ActionController around_filter based on the user as follows:
70
+
71
+ class ApplicationController < ActionController::Base
72
+ around_filter :select_shard
73
+
74
+ private
75
+ def select_shard(&block)
76
+ DataFabric.activate_shard(:city => @current_user.city, &block)
77
+ end
78
+ end
79
+
80
+
81
+ == Warnings
82
+
83
+ * Sharded models should never be placed in the session store or you will get "Shard not set" errors when the session is persisted.
84
+ * DataFabric does not work in development mode with cache_classes = false. You will get an error in ActiveRecord's clear_reloadable_connections! method.
85
+
86
+ == Testing and Bug Reports
87
+
88
+ If you think you've found a problem with data_fabric, please use the example23 or example30 application to reproduce the bug and send me the diff.
89
+
90
+
91
+ == Thanks to...
92
+
93
+ Rick Olsen of Rails Core:: for the Masochism plugin, which showed me how to bend AR's connection handling to my will
94
+ Bradley Taylor of RailsMachine:: for the advice to shard while at acts_as_conference
95
+
96
+
97
+ == Author
98
+
99
+ Mike Perham <mperham@gmail.com>
100
+ http://mikeperham.com
101
+ http://twitter.com/mperham
102
+
103
+
104
+ == LICENSE:
105
+
106
+ Copyright (c) 2010 Mike Perham
107
+
108
+ Permission is hereby granted, free of charge, to any person obtaining
109
+ a copy of this software and associated documentation files (the
110
+ 'Software'), to deal in the Software without restriction, including
111
+ without limitation the rights to use, copy, modify, merge, publish,
112
+ distribute, sublicense, and/or sell copies of the Software, and to
113
+ permit persons to whom the Software is furnished to do so, subject to
114
+ the following conditions:
115
+
116
+ The above copyright notice and this permission notice shall be
117
+ included in all copies or substantial portions of the Software.
118
+
119
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
120
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
121
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
122
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
123
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
124
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
125
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,93 @@
1
+ require 'fileutils'
2
+ include FileUtils::Verbose
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.verbose = true
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/*_test.rb']
9
+ end
10
+
11
+ task :clean do
12
+ rm_f Dir['*.gem']
13
+ rm_f Dir['test/*.db']
14
+ rm_rf 'coverage'
15
+
16
+ puts `cd example30 ; rake app:clean`
17
+ end
18
+
19
+ task :default => :test
20
+ task :test => [:pretest]
21
+
22
+ desc "Test all versions of ActiveRecord installed locally"
23
+ task :test_all do
24
+ Gem.source_index.search(Gem::Dependency.new('activerecord', '>=2.0')).each do |spec|
25
+ puts `rake test AR_VERSION=#{spec.version}`
26
+ end
27
+ end
28
+
29
+ task :pretest do
30
+ setup(false)
31
+ end
32
+
33
+ task :create_db do
34
+ setup(true)
35
+ end
36
+
37
+ def load_database_yml
38
+ filename = "test/database.yml"
39
+ if !File.exist?(filename)
40
+ STDERR.puts "\n*** ERROR ***:\n" <<
41
+ "You must have a 'test/database.yml' file in order to create the test database. " <<
42
+ "An example is provided in 'test/database.yml.mysql'.\n\n"
43
+ exit 1
44
+ end
45
+ YAML::load(ERB.new(IO.read(filename)).result)
46
+ end
47
+
48
+ def setup_connection
49
+ require 'erb'
50
+ require 'logger'
51
+ require 'active_record'
52
+ ActiveRecord::Base.configurations = load_database_yml
53
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
54
+ ActiveRecord::Base.logger.level = Logger::INFO
55
+ end
56
+
57
+ def using_connection(database_identifier, &block)
58
+ ActiveRecord::Base.establish_connection(database_identifier)
59
+ ActiveRecord::Base.connection.instance_eval(&block)
60
+ end
61
+
62
+ def setup(create = false)
63
+ setup_connection
64
+
65
+ ActiveRecord::Base.configurations.each_pair do |identifier, config|
66
+ using_connection(identifier) do
67
+ send("create_#{config['adapter']}", create, config['database'])
68
+ end
69
+ end
70
+ end
71
+
72
+ def create_sqlite3(create, db_name)
73
+ execute "drop table if exists the_whole_burritos"
74
+ execute "drop table if exists enchiladas"
75
+ execute "create table enchiladas (id integer not null primary key, name varchar(30) not null)"
76
+ execute "insert into enchiladas (id, name) values (1, '#{db_name}')"
77
+ execute "create table the_whole_burritos (id integer not null primary key, name varchar(30) not null)"
78
+ execute "insert into the_whole_burritos (id, name) values (1, '#{db_name}')"
79
+ end
80
+
81
+ def create_mysql(create, db_name)
82
+ if create
83
+ execute "drop database if exists #{db_name}"
84
+ execute "create database #{db_name}"
85
+ end
86
+ execute "use #{db_name}"
87
+ execute "drop table if exists the_whole_burritos"
88
+ execute "drop table if exists enchiladas"
89
+ execute "create table enchiladas (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
90
+ execute "insert into enchiladas (id, name) values (1, '#{db_name}')"
91
+ execute "create table the_whole_burritos (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
92
+ execute "insert into the_whole_burritos (id, name) values (1, '#{db_name}')"
93
+ end
@@ -0,0 +1,30 @@
1
+ data_fabric has two layers of tests: unit tests and integration tests.
2
+
3
+
4
+ == Running the Unit Tests
5
+
6
+ The unit tests test both with and without an actual database. test/database_test.rb
7
+ tests against a database. The other unit tests mock AR so no actual database is
8
+ required. You can use the standard test/database.yml which tests against SQLite3
9
+ or customize the provided test/database.yml.mysql. The "rake create_db" task will
10
+ set up the necessary databases and tables.
11
+
12
+
13
+ == Running the Integration Tests
14
+
15
+ data_fabric ships with an example application which shows an actual Rails application
16
+ using data_fabric. You can customize config/database.yml but by default it uses a local
17
+ Sqlite3 database so there should be no customization necessary.
18
+
19
+ Run the Rake task to prepare the application for testing. This will delete existing SQLite
20
+ databases and migrate all the databases from scratch. You should then be able to run the
21
+ example application's tests.
22
+
23
+ cd example
24
+ rake app:prepare
25
+ rake test
26
+
27
+
28
+ == Submitting Bugs
29
+
30
+ If you think you've found a problem with data_fabric, contact me at mperham AT gmail.com.
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', '3.0.3'
4
+ gem 'sqlite3-ruby', :require => 'sqlite3'
5
+ gem 'data_fabric', :path => '..'
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ data_fabric (1.3.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionmailer (3.0.3)
11
+ actionpack (= 3.0.3)
12
+ mail (~> 2.2.9)
13
+ actionpack (3.0.3)
14
+ activemodel (= 3.0.3)
15
+ activesupport (= 3.0.3)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.13)
21
+ rack-test (~> 0.5.6)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.3)
24
+ activesupport (= 3.0.3)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4)
27
+ activerecord (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ arel (~> 2.0.2)
31
+ tzinfo (~> 0.3.23)
32
+ activeresource (3.0.3)
33
+ activemodel (= 3.0.3)
34
+ activesupport (= 3.0.3)
35
+ activesupport (3.0.3)
36
+ arel (2.0.4)
37
+ builder (2.1.2)
38
+ erubis (2.6.6)
39
+ abstract (>= 1.0.0)
40
+ i18n (0.4.1)
41
+ mail (2.2.10)
42
+ activesupport (>= 2.3.6)
43
+ i18n (~> 0.4.1)
44
+ mime-types (~> 1.16)
45
+ treetop (~> 1.4.8)
46
+ mime-types (1.16)
47
+ polyglot (0.3.1)
48
+ rack (1.2.1)
49
+ rack-mount (0.6.13)
50
+ rack (>= 1.0.0)
51
+ rack-test (0.5.6)
52
+ rack (>= 1.0)
53
+ rails (3.0.3)
54
+ actionmailer (= 3.0.3)
55
+ actionpack (= 3.0.3)
56
+ activerecord (= 3.0.3)
57
+ activeresource (= 3.0.3)
58
+ activesupport (= 3.0.3)
59
+ bundler (~> 1.0)
60
+ railties (= 3.0.3)
61
+ railties (3.0.3)
62
+ actionpack (= 3.0.3)
63
+ activesupport (= 3.0.3)
64
+ rake (>= 0.8.7)
65
+ thor (~> 0.14.4)
66
+ rake (0.8.7)
67
+ sqlite3-ruby (1.3.1)
68
+ thor (0.14.4)
69
+ treetop (1.4.8)
70
+ polyglot (>= 0.3.1)
71
+ tzinfo (0.3.23)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ data_fabric!
78
+ rails (= 3.0.3)
79
+ sqlite3-ruby
@@ -0,0 +1,256 @@
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.find(: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.com/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
+ | |-- controllers
160
+ | |-- helpers
161
+ | |-- mailers
162
+ | |-- models
163
+ | `-- views
164
+ | `-- layouts
165
+ |-- config
166
+ | |-- environments
167
+ | |-- initializers
168
+ | `-- locales
169
+ |-- db
170
+ |-- doc
171
+ |-- lib
172
+ | `-- tasks
173
+ |-- log
174
+ |-- public
175
+ | |-- images
176
+ | |-- javascripts
177
+ | `-- stylesheets
178
+ |-- script
179
+ |-- test
180
+ | |-- fixtures
181
+ | |-- functional
182
+ | |-- integration
183
+ | |-- performance
184
+ | `-- unit
185
+ |-- tmp
186
+ | |-- cache
187
+ | |-- pids
188
+ | |-- sessions
189
+ | `-- sockets
190
+ `-- vendor
191
+ `-- plugins
192
+
193
+ app
194
+ Holds all the code that's specific to this particular application.
195
+
196
+ app/controllers
197
+ Holds controllers that should be named like weblogs_controller.rb for
198
+ automated URL mapping. All controllers should descend from
199
+ ApplicationController which itself descends from ActionController::Base.
200
+
201
+ app/models
202
+ Holds models that should be named like post.rb. Models descend from
203
+ ActiveRecord::Base by default.
204
+
205
+ app/views
206
+ Holds the template files for the view that should be named like
207
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
208
+ eRuby syntax by default.
209
+
210
+ app/views/layouts
211
+ Holds the template files for layouts to be used with views. This models the
212
+ common header/footer method of wrapping views. In your views, define a layout
213
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
214
+ Inside default.html.erb, call <% yield %> to render the view using this
215
+ layout.
216
+
217
+ app/helpers
218
+ Holds view helpers that should be named like weblogs_helper.rb. These are
219
+ generated for you automatically when using generators for controllers.
220
+ Helpers can be used to wrap functionality for your views into methods.
221
+
222
+ config
223
+ Configuration files for the Rails environment, the routing map, the database,
224
+ and other dependencies.
225
+
226
+ db
227
+ Contains the database schema in schema.rb. db/migrate contains all the
228
+ sequence of Migrations for your schema.
229
+
230
+ doc
231
+ This directory is where your application documentation will be stored when
232
+ generated using <tt>rake doc:app</tt>
233
+
234
+ lib
235
+ Application specific libraries. Basically, any kind of custom code that
236
+ doesn't belong under controllers, models, or helpers. This directory is in
237
+ the load path.
238
+
239
+ public
240
+ The directory available for the web server. Contains subdirectories for
241
+ images, stylesheets, and javascripts. Also contains the dispatchers and the
242
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
243
+ server.
244
+
245
+ script
246
+ Helper scripts for automation and generation.
247
+
248
+ test
249
+ Unit and functional tests along with fixtures. When using the rails generate
250
+ command, template test files will be generated for you and placed in this
251
+ directory.
252
+
253
+ vendor
254
+ External libraries that the application depends on. Also includes the plugins
255
+ subdirectory. If the app has frozen rails, those gems also go here, under
256
+ vendor/rails/. This directory is in the load path.