data_fabric 1.2.7 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/CHANGELOG +7 -0
  2. data/README.rdoc +9 -8
  3. data/Rakefile +9 -41
  4. data/TESTING.rdoc +3 -3
  5. data/example23/Rakefile +28 -27
  6. data/example23/config/environment.rb +4 -2
  7. data/example30/Gemfile +5 -0
  8. data/example30/Gemfile.lock +79 -0
  9. data/example30/README +256 -0
  10. data/example30/Rakefile +52 -0
  11. data/{example → example30}/app/controllers/accounts_controller.rb +0 -0
  12. data/{example/app/controllers/application.rb → example30/app/controllers/application_controller.rb} +0 -0
  13. data/{example → example30}/app/controllers/figments_controller.rb +0 -0
  14. data/example30/app/helpers/application_helper.rb +2 -0
  15. data/{example → example30}/app/models/account.rb +0 -0
  16. data/{example → example30}/app/models/figment.rb +0 -0
  17. data/{example → example30}/app/views/accounts/index.html.erb +2 -2
  18. data/{example → example30}/app/views/layouts/application.html.erb +0 -0
  19. data/example30/config.ru +4 -0
  20. data/example30/config/application.rb +42 -0
  21. data/example30/config/boot.rb +13 -0
  22. data/{example → example30}/config/database.yml +0 -0
  23. data/example30/config/environment.rb +5 -0
  24. data/example30/config/environments/development.rb +26 -0
  25. data/example30/config/environments/production.rb +49 -0
  26. data/example30/config/environments/test.rb +35 -0
  27. data/example30/config/initializers/backtrace_silencers.rb +7 -0
  28. data/{example → example30}/config/initializers/inflections.rb +2 -2
  29. data/{example → example30}/config/initializers/mime_types.rb +0 -0
  30. data/example30/config/initializers/secret_token.rb +7 -0
  31. data/example30/config/initializers/session_store.rb +8 -0
  32. data/example30/config/locales/en.yml +5 -0
  33. data/example30/config/routes.rb +65 -0
  34. data/example30/db/development.sqlite3 +0 -0
  35. data/{example → example30}/db/migrate/20080702154628_create_accounts.rb +0 -0
  36. data/{example → example30}/db/migrate/20080702154820_create_figments.rb +0 -0
  37. data/example30/db/s0_development.sqlite3 +0 -0
  38. data/example30/db/s0_test.sqlite3 +0 -0
  39. data/example30/db/s1_development.sqlite3 +0 -0
  40. data/example30/db/s1_test.sqlite3 +0 -0
  41. data/{example23 → example30}/db/schema.rb +7 -6
  42. data/example30/db/seeds.rb +7 -0
  43. data/example30/db/test.sqlite3 +0 -0
  44. data/example30/log/development.log +132 -0
  45. data/example30/log/test.log +444 -0
  46. data/example30/script/rails +6 -0
  47. data/{example → example30}/test/fixtures/accounts.yml +0 -0
  48. data/{example → example30}/test/functional/accounts_controller_test.rb +0 -0
  49. data/{example → example30}/test/integration/account_figments_test.rb +0 -0
  50. data/example30/test/performance/browsing_test.rb +9 -0
  51. data/example30/test/test_helper.rb +13 -0
  52. data/lib/data_fabric.rb +14 -21
  53. data/lib/data_fabric/{ar22.rb → connection_proxy.rb} +43 -63
  54. data/lib/data_fabric/extensions.rb +29 -0
  55. data/lib/data_fabric/version.rb +1 -1
  56. data/test/connection_test.rb +17 -12
  57. data/test/database_test.rb +3 -11
  58. data/test/test_helper.rb +3 -8
  59. data/test/thread_test.rb +0 -14
  60. data/test/vr_austin_master.db +0 -0
  61. data/test/vr_austin_slave.db +0 -0
  62. data/test/vr_dallas_master.db +0 -0
  63. data/test/vr_dallas_slave.db +0 -0
  64. metadata +72 -99
  65. data/example/Rakefile +0 -58
  66. data/example/app/helpers/accounts_helper.rb +0 -2
  67. data/example/app/helpers/application_helper.rb +0 -3
  68. data/example/app/helpers/figments_helper.rb +0 -2
  69. data/example/config/boot.rb +0 -109
  70. data/example/config/environment.rb +0 -67
  71. data/example/config/environments/development.rb +0 -17
  72. data/example/config/environments/production.rb +0 -22
  73. data/example/config/environments/test.rb +0 -22
  74. data/example/config/initializers/new_rails_defaults.rb +0 -15
  75. data/example/config/routes.rb +0 -45
  76. data/example/db/schema.rb +0 -28
  77. data/example/public/404.html +0 -30
  78. data/example/public/422.html +0 -30
  79. data/example/public/500.html +0 -30
  80. data/example/public/dispatch.cgi +0 -10
  81. data/example/public/dispatch.fcgi +0 -24
  82. data/example/public/dispatch.rb +0 -10
  83. data/example/public/favicon.ico +0 -0
  84. data/example/public/images/rails.png +0 -0
  85. data/example/public/robots.txt +0 -5
  86. data/example/script/about +0 -3
  87. data/example/script/console +0 -3
  88. data/example/script/dbconsole +0 -3
  89. data/example/script/destroy +0 -3
  90. data/example/script/generate +0 -3
  91. data/example/script/performance/benchmarker +0 -3
  92. data/example/script/performance/profiler +0 -3
  93. data/example/script/performance/request +0 -3
  94. data/example/script/plugin +0 -3
  95. data/example/script/process/inspector +0 -3
  96. data/example/script/process/reaper +0 -3
  97. data/example/script/process/spawner +0 -3
  98. data/example/script/runner +0 -3
  99. data/example/script/server +0 -3
  100. data/example/test/test_helper.rb +0 -41
  101. data/example23/db/development.sqlite3 +0 -0
  102. data/example23/db/s0_development.sqlite3 +0 -0
  103. data/example23/db/s0_test.sqlite3 +0 -0
  104. data/example23/db/s1_development.sqlite3 +0 -0
  105. data/example23/db/s1_test.sqlite3 +0 -0
  106. data/example23/db/test.sqlite3 +0 -0
  107. data/example23/log/development.log +0 -295
  108. data/example23/log/test.log +0 -551
  109. data/example23/vendor/plugins/data_fabric/init.rb +0 -3
  110. data/example23/vendor/plugins/data_fabric/lib/data_fabric.rb +0 -106
  111. data/example23/vendor/plugins/data_fabric/lib/data_fabric/ar20.rb +0 -133
  112. data/example23/vendor/plugins/data_fabric/lib/data_fabric/ar22.rb +0 -181
  113. data/example23/vendor/plugins/data_fabric/lib/data_fabric/dash.rb +0 -20
  114. data/example23/vendor/plugins/data_fabric/lib/data_fabric/version.rb +0 -7
  115. data/init.rb +0 -3
  116. data/lib/data_fabric/ar20.rb +0 -133
  117. data/lib/data_fabric/dash.rb +0 -20
  118. data/rails/init.rb +0 -1
data/CHANGELOG CHANGED
@@ -1,5 +1,12 @@
1
1
  DataFabric changelog
2
2
 
3
+ v1.3.0 - 2010-11-22
4
+
5
+ - Major implementation refactoring
6
+ - Fix issues under Rails 3.0. Tested with 2.3.10, 3.0.0 and 3.0.3.
7
+ - Drop support for Rails 2.2 (may work but untested)
8
+ - Remove jeweler dependency
9
+
3
10
  v1.2.7 - 2010-08-10
4
11
 
5
12
  - NO FUNCTIONAL CHANGES.
data/README.rdoc CHANGED
@@ -21,6 +21,11 @@ then in config/environment.rb, add a config.gem entry:
21
21
  ...
22
22
  config.gem 'data_fabric'
23
23
 
24
+ or add data_fabric to your Gemfile:
25
+
26
+ gem 'data_fabric'
27
+
28
+ data_fabric has been tested with Rails 2.3.10, 3.0.0 and 3.0.3.
24
29
 
25
30
  == How does it work?
26
31
 
@@ -69,19 +74,17 @@ ActionController around_filter based on the user as follows:
69
74
  == Warnings
70
75
 
71
76
  * Sharded models should never be placed in the session store or you will get "Shard not set" errors when the session is persisted.
72
- * DataFabric does not support running with ActiveRecord's allow_concurrency = true in AR 2.0 and 2.1. allow_concurrency is gone in AR 2.2.
73
77
  * DataFabric does not work in development mode with cache_classes = false. You will get an error in ActiveRecord's clear_reloadable_connections! method.
74
78
 
75
79
  == Testing and Bug Reports
76
80
 
77
- If you think you've found a problem with data_fabric, please use the example application to reproduce the bug and send me the diff. The example application is a stock Rails 2.1 application which uses data_fabric in the expected fashion.
81
+ 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.
78
82
 
79
83
 
80
84
  == Thanks to...
81
85
 
82
86
  Rick Olsen of Rails Core:: for the Masochism plugin, which showed me how to bend AR's connection handling to my will
83
87
  Bradley Taylor of RailsMachine:: for the advice to shard while at acts_as_conference
84
- FiveRuns:: for paying me to develop this code and allowing its release
85
88
 
86
89
 
87
90
  == Author
@@ -92,11 +95,9 @@ http://twitter.com/mperham
92
95
 
93
96
 
94
97
  == LICENSE:
95
-
96
- (The FiveRuns License)
97
-
98
- Copyright (c) 2008 FiveRuns Corporation
99
-
98
+
99
+ Copyright (c) 2010 Mike Perham
100
+
100
101
  Permission is hereby granted, free of charge, to any person obtaining
101
102
  a copy of this software and associated documentation files (the
102
103
  'Software'), to deal in the Software without restriction, including
data/Rakefile CHANGED
@@ -1,55 +1,21 @@
1
- require 'rubygems'
2
-
3
- begin
4
- require './lib/data_fabric/version'
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |p|
7
- p.version = DataFabric::Version::STRING
8
- p.authors = ["Mike Perham"]
9
- p.email = 'mperham@gmail.com'
10
- p.summary = 'Sharding and replication support for ActiveRecord 2.x'
11
- p.homepage = "http://github.com/mperham/data_fabric"
12
- p.name = "data_fabric"
13
- p.files = FileList['*.rdoc', 'Rakefile', 'init.rb', 'CHANGELOG', "{lib,test,rails,example,example23}/**/*", ]
14
- p.add_development_dependency 'jeweler'
15
- p.add_development_dependency 'flexmock'
16
- end
17
- rescue LoadError => le
18
- puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler: #{le.message}"
19
- end
1
+ require 'fileutils'
2
+ include FileUtils::Verbose
20
3
 
21
4
  require 'rake/testtask'
22
-
23
5
  Rake::TestTask.new do |t|
24
6
  t.verbose = true
25
- t.libs << 'test' << 'rails'
7
+ t.libs << 'test'
26
8
  t.test_files = FileList['test/*_test.rb']
27
9
  end
28
10
 
29
- task :gemspec => [:clean]
30
-
31
11
  task :clean do
32
- FileUtils.rm_f Dir['*.gem']
33
- FileUtils.rm_f Dir['test/*.db']
34
- FileUtils.rm_rf 'coverage'
35
- end
36
-
37
- desc "Install gem locally"
38
- task :installer do
39
- sh "gem install data_fabric-*.gem"
40
- end
12
+ rm_f Dir['*.gem']
13
+ rm_f Dir['test/*.db']
14
+ rm_rf 'coverage'
41
15
 
42
- task :gem do
43
- sh "gem build data_fabric.gemspec"
16
+ puts `cd example23 ; rake app:clean`
44
17
  end
45
18
 
46
- desc "Push gem to RubyForge"
47
- task :publish => [:clean, :gemspec, :gem, :installer] do
48
- require './lib/data_fabric/version'
49
- sh "gem push data_fabric-#{DataFabric::Version::STRING}.gem"
50
- end
51
-
52
-
53
19
  task :default => :test
54
20
  task :test => [:pretest]
55
21
 
@@ -80,6 +46,8 @@ def load_database_yml
80
46
  end
81
47
 
82
48
  def setup_connection
49
+ require 'erb'
50
+ require 'logger'
83
51
  require 'active_record'
84
52
  ActiveRecord::Base.configurations = load_database_yml
85
53
  ActiveRecord::Base.logger = Logger.new(STDOUT)
data/TESTING.rdoc CHANGED
@@ -16,9 +16,9 @@ data_fabric ships with an example application which shows an actual Rails applic
16
16
  using data_fabric. You can customize config/database.yml but by default it uses a local
17
17
  Sqlite3 database so there should be no customization necessary.
18
18
 
19
- Run the Rake task to prepare the application for testing. This will copy the latest data_fabric code
20
- to vendor/plugins, delete existing databases and migrate all the databases from scratch. You should
21
- then be able to run the example application's tests.
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
22
 
23
23
  cd example
24
24
  rake app:prepare
data/example23/Rakefile CHANGED
@@ -13,28 +13,28 @@ require 'fileutils'
13
13
  include FileUtils::Verbose
14
14
 
15
15
  namespace :db do
16
- task :migrate do
17
- require 'erb'
18
- require 'logger'
19
- require 'active_record'
20
- reference = YAML::load(ERB.new(IO.read("config/database.yml")).result)
21
- env = RAILS_ENV = ENV['RAILS_ENV'] || 'development'
22
- ActiveRecord::Base.logger = Logger.new(STDOUT)
23
- ActiveRecord::Base.logger.level = Logger::WARN
24
- ActiveRecord::Base.configurations = reference.dup
25
- old_config = reference[env]
26
- reference.each_key do |name|
27
- next unless name.include? env
28
- next if name.include? 'slave' # Replicated databases should not be touched directly
16
+ task :migrate do
17
+ require 'erb'
18
+ require 'logger'
19
+ require 'active_record'
20
+ reference = YAML::load(ERB.new(IO.read("config/database.yml")).result)
21
+ env = RAILS_ENV = ENV['RAILS_ENV'] || 'development'
22
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
23
+ ActiveRecord::Base.logger.level = Logger::INFO
24
+ ActiveRecord::Base.configurations = reference.dup
25
+ old_config = reference[env]
26
+ reference.each_key do |name|
27
+ next unless name.include? env
28
+ next if name.include? 'slave' # Replicated databases should not be touched directly
29
29
 
30
- puts "Migrating #{name}"
31
- ActiveRecord::Base.clear_active_connections!
32
- ActiveRecord::Base.configurations[env] = reference[name]
33
- ActiveRecord::Base.establish_connection RAILS_ENV
34
- ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
35
- ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
36
- end
30
+ puts "Migrating #{name}"
31
+ ActiveRecord::Base.clear_active_connections!
32
+ ActiveRecord::Base.configurations[env] = reference[name]
33
+ ActiveRecord::Base.establish_connection RAILS_ENV
34
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
35
+ ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
37
36
  end
37
+ end
38
38
  end
39
39
 
40
40
  task :default => :test
@@ -50,18 +50,19 @@ namespace :app do
50
50
  task :prepare => [:clean, :copy_plugin, :migrate]
51
51
 
52
52
  task :copy_plugin do
53
- mkdir_p 'vendor/plugins/data_fabric'
54
- cp_r '../lib', 'vendor/plugins/data_fabric'
55
- cp '../init.rb', 'vendor/plugins/data_fabric'
53
+ mkdir_p 'vendor/gems/data_fabric-1.0.0'
54
+ cp_r '../lib', 'vendor/gems/data_fabric-1.0.0'
56
55
  end
57
56
 
58
57
  task :clean do
59
- rm_rf 'vendor/plugins/data_fabric'
60
- rm_f 'db/*.sqlite3'
58
+ rm_rf 'log'
59
+ rm_rf 'vendor/gems/data_fabric-1.0.0'
60
+ rm_rf Dir['db/*.sqlite3']
61
+ rm_f 'db/schema.rb'
61
62
  end
62
63
 
63
64
  task :migrate do
64
- sh "rake db:migrate"
65
- sh "rake RAILS_ENV=test db:migrate"
65
+ puts `rake db:migrate --trace`
66
+ puts `rake RAILS_ENV=test db:migrate`
66
67
  end
67
68
  end
@@ -5,7 +5,7 @@
5
5
  # ENV['RAILS_ENV'] ||= 'production'
6
6
 
7
7
  # Specifies gem version of Rails to use when vendor/rails is not present
8
- #RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
8
+ RAILS_GEM_VERSION = '2.3.10' unless defined? RAILS_GEM_VERSION
9
9
 
10
10
  # Bootstrap the Rails environment, frameworks, and default configuration
11
11
  require File.join(File.dirname(__FILE__), 'boot')
@@ -28,6 +28,8 @@ Rails::Initializer.run do |config|
28
28
  # config.gem "sqlite3-ruby", :lib => "sqlite3"
29
29
  # config.gem "aws-s3", :lib => "aws/s3"
30
30
 
31
+ config.gem 'data_fabric', :version => '1.0.0'
32
+
31
33
  # Only load the plugins named here, in the order given. By default, all plugins
32
34
  # in vendor/plugins are loaded in alphabetical order.
33
35
  # :all can be used as a placeholder for all plugins not explicitly named
@@ -55,7 +57,7 @@ Rails::Initializer.run do |config|
55
57
  # Make sure the secret is at least 30 characters and all random,
56
58
  # no regular words or you'll be exposed to dictionary attacks.
57
59
  config.action_controller.session = {
58
- :key => '_example22_session',
60
+ :key => '_example23_session',
59
61
  :secret => 'a726ae17b1f84e51e5ae5f8bc861a92dcffb188a7a1e9cb88c0c513ce9722b0983ee718c79741260669bfbc69b68044129fc11d8792b90fa912d719992cb9574'
60
62
  }
61
63
 
data/example30/Gemfile ADDED
@@ -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
data/example30/README ADDED
@@ -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.