custom_associations 0.1.0.pre

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 (43) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +26 -0
  4. data/Rakefile +35 -0
  5. data/lib/custom_associations.rb +316 -0
  6. data/lib/custom_associations/version.rb +3 -0
  7. data/spec/dummy/README.rdoc +261 -0
  8. data/spec/dummy/Rakefile +7 -0
  9. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  10. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  11. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  12. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  13. data/spec/dummy/app/models/address.rb +2 -0
  14. data/spec/dummy/app/models/customer.rb +11 -0
  15. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  16. data/spec/dummy/config.ru +4 -0
  17. data/spec/dummy/config/application.rb +65 -0
  18. data/spec/dummy/config/boot.rb +10 -0
  19. data/spec/dummy/config/database.yml +25 -0
  20. data/spec/dummy/config/environment.rb +5 -0
  21. data/spec/dummy/config/environments/development.rb +37 -0
  22. data/spec/dummy/config/environments/production.rb +67 -0
  23. data/spec/dummy/config/environments/test.rb +37 -0
  24. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  25. data/spec/dummy/config/initializers/inflections.rb +15 -0
  26. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  27. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  28. data/spec/dummy/config/initializers/session_store.rb +8 -0
  29. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  30. data/spec/dummy/config/locales/en.yml +5 -0
  31. data/spec/dummy/config/routes.rb +58 -0
  32. data/spec/dummy/db/migrate/20130604054100_create_customers.rb +16 -0
  33. data/spec/dummy/db/migrate/20130604054147_create_addresses.rb +13 -0
  34. data/spec/dummy/db/schema.rb +40 -0
  35. data/spec/dummy/public/404.html +26 -0
  36. data/spec/dummy/public/422.html +26 -0
  37. data/spec/dummy/public/500.html +25 -0
  38. data/spec/dummy/public/favicon.ico +0 -0
  39. data/spec/dummy/script/rails +6 -0
  40. data/spec/dummy/spec/factories/addresses.rb +10 -0
  41. data/spec/dummy/spec/factories/customers.rb +9 -0
  42. data/spec/spec_helper.rb +40 -0
  43. metadata +192 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07d9a48c6bf107a6817bad0ce5c3f17a2dd0a3e4
4
+ data.tar.gz: 17da1a0e7773abf7cb053616ddcf3396d6181021
5
+ SHA512:
6
+ metadata.gz: fbd43e83424c0adaaf9f7049e4570d8c2925da334e596ce24626219a6370ce504af41b793731b75ca16daf06cb4fe211788574c41bc0a5c84270eca4336490f1
7
+ data.tar.gz: e3edab075e4065f76c0fa0bb2f9f2538a540953d46eaa952ce4f5f6700e40b9af4e698996f19bb480434671f160b25537d781ac8e4155366bd7bdbcc6e434967
@@ -0,0 +1,20 @@
1
+ Copyright 2007-2013 Joseph Khoobyar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ = CustomAssociations
2
+
3
+ This is a quick rewrite of a very old (ActiveRecord 1.2.x) plugin of mine.
4
+
5
+ It allows you to define associations based on joins and conditions, instead of keys.
6
+
7
+ Future versions of this plugin will provide a better DSL, and greater flexibility.
8
+
9
+ NOTE: Only tested with ActiveRecord 3.2.13.
10
+ NOTE: Preloading is disabled for custom associations - eager-loading will be used instead.
11
+
12
+ == Brief example
13
+
14
+ NOTE: The +rescue+ hack below is just a quick way of showing what is possible with this plugin. Future versions will support a unified syntax.
15
+
16
+ class Customer
17
+
18
+ has_many_custom :addresses,
19
+ :joins=>['INNER JOIN customer_addresses ON customer_addresses.address_id = addresses.id'],
20
+ :conditions=>proc{ {:'customer_addresses.customer_number'=>(customer_number rescue Customer.arel_table[:customer_number])} }
21
+
22
+ has_one_custom :address,
23
+ :joins=>['INNER JOIN customer_addresses ON customer_addresses.address_id = addresses.id'],
24
+ :conditions=>proc{ {:'customer_addresses.customer_number'=>(customer_number rescue Customer.arel_table[:customer_number])} }
25
+
26
+ end
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
11
+
12
+ require 'rspec/core'
13
+ require 'rspec/core/rake_task'
14
+
15
+ desc "Run all specs in spec directory (excluding plugin specs)"
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ task :default => :spec
19
+
20
+ begin
21
+ require 'rdoc/task'
22
+ rescue LoadError
23
+ require 'rdoc/rdoc'
24
+ require 'rake/rdoctask'
25
+ RDoc::Task = Rake::RDocTask
26
+ end
27
+
28
+ RDoc::Task.new(:rdoc) do |rdoc|
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = 'CustomAssociations'
31
+ rdoc.options << '--line-numbers'
32
+ rdoc.rdoc_files.include('README.rdoc')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
35
+
@@ -0,0 +1,316 @@
1
+ require 'active_record'
2
+ require 'active_record/base'
3
+ require 'active_support/concern'
4
+ require 'custom_associations/version'
5
+
6
+ module CustomAssociations
7
+
8
+ # Builders for the DSL.
9
+ module Builder
10
+
11
+ class CustomizableAssociation < ActiveRecord::Associations::Builder::Association
12
+ self.valid_options -= [ :foreign_key, :validate ]
13
+ self.valid_options += [ :as, :table_name, :joins, :order, :group, :having, :limit, :offset, :inverse_of ]
14
+
15
+ private
16
+
17
+ # Ensure that the association is readonly.
18
+ def define_writers
19
+ mixin.remove_possible_method("#{name}=")
20
+ end
21
+ end
22
+
23
+ class HasOneCustom < CustomizableAssociation
24
+ self.macro = :has_one_custom
25
+ end
26
+
27
+ class HasManyCustom < CustomizableAssociation
28
+ self.macro = :has_many_custom
29
+
30
+ self.valid_options += [ :finder_sql, :counter_sql ]
31
+
32
+ attr_reader :block_extension
33
+
34
+ def self.build(model, name, options, &extension)
35
+ new(model, name, options, &extension).build
36
+ end
37
+
38
+ def initialize(model, name, options, &extension)
39
+ super(model, name, options)
40
+ @block_extension = extension
41
+ end
42
+
43
+ def build
44
+ wrap_block_extension
45
+ super
46
+ end
47
+
48
+ private
49
+
50
+ def wrap_block_extension
51
+ options[:extend] = Array.wrap(options[:extend])
52
+
53
+ if block_extension
54
+ silence_warnings do
55
+ model.parent.const_set(extension_module_name, Module.new(&block_extension))
56
+ end
57
+ options[:extend].push("#{model.parent}::#{extension_module_name}".constantize)
58
+ end
59
+ end
60
+
61
+ def extension_module_name
62
+ @extension_module_name ||= "#{model.to_s.demodulize}#{name.to_s.camelize}AssociationExtension"
63
+ end
64
+
65
+ def define_readers
66
+ super
67
+
68
+ name = self.name
69
+ mixin.redefine_method("#{name.to_s.singularize}_ids") do
70
+ association(name).ids_reader
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ # Customizable associations
77
+ module Associations
78
+
79
+ # Extensions to support custom associations.
80
+ module JoinAssociation
81
+ extend ActiveSupport::Concern
82
+
83
+ included do
84
+ alias_method_chain :build_constraint, :custom
85
+ alias_method_chain :join_to, :custom
86
+ end
87
+
88
+ # HACK: Dummy constraint node for integrating custom associations with ActiveRecord.
89
+ DummyConstraint = Object.new.tap{|o| def o.and(conditions) conditions end }.freeze
90
+
91
+ # Overridden to support custom associations.
92
+ def build_constraint_with_custom(reflection, table, key, foreign_table, foreign_key)
93
+ case reflection.source_macro
94
+ when :has_one_custom, :has_many_custom
95
+ return DummyConstraint unless reflection.klass.finder_needs_type_condition?
96
+ reflection.klass.send(:type_condition, table)
97
+ else
98
+ build_constraint_without_custom(reflection, table, key, foreign_table, foreign_key)
99
+ end
100
+ end
101
+
102
+ # Overridden to support custom associations.
103
+ def join_to_with_custom(relation)
104
+ case reflection.source_macro
105
+ when :has_one_custom, :has_many_custom
106
+ Array.wrap(reflection.options[:joins]).each do |join|
107
+ next if join.blank? or ! join.is_a?(String)
108
+ if join !~ /^\s*(?:(?:LEFT|RIGHT|INNER|OUTER|STRAIGHT)\s+)+JOIN\s+/ism
109
+ join = "#{join_type==Arel::OuterJoin ? 'LEFT OUTER' : 'INNER'} JOIN #{join}"
110
+ elsif join_type==Arel::OuterJoin
111
+ join = join.sub(/^\s*(?:LEFT\s+)?INNER\b/,'LEFT OUTER')
112
+ end
113
+ relation.join(Arel.sql(join))
114
+ end
115
+ end
116
+ join_to_without_custom(relation)
117
+ end
118
+
119
+ end
120
+
121
+ # Extensions to support custom associations.
122
+ module JoinDependency
123
+ extend ActiveSupport::Concern
124
+
125
+ included do
126
+ alias_method_chain :construct_association, :custom
127
+ end
128
+
129
+ protected
130
+
131
+ # Overridden to support custom associations.
132
+ def construct_association_with_custom(record, join_part, row)
133
+ return if record.id.to_s != join_part.parent.record_id(row).to_s
134
+
135
+ macro = join_part.reflection.macro
136
+ if macro == :has_one_custom
137
+ if record.association_cache.key?(join_part.reflection.name)
138
+ association = record.association(join_part.reflection.name).target
139
+ else
140
+ association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
141
+ set_target_and_inverse(join_part, association, record)
142
+ end
143
+ elsif macro == :has_many_custom
144
+ association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
145
+ other = record.association(join_part.reflection.name)
146
+ other.loaded!
147
+ other.target.push(association) if association
148
+ other.set_inverse_instance(association)
149
+ else
150
+ associaton = construct_association_without_custom(record, join_part, row)
151
+ end
152
+ association
153
+ end
154
+ end
155
+
156
+ # Gutted to support associations without explicit keys.
157
+ class CustomizableAssociationScope < ActiveRecord::Associations::AssociationScope
158
+
159
+ private
160
+
161
+ # Overridden to remove chain support.
162
+ def add_constraints(scope)
163
+ table, foreign_table = construct_tables
164
+
165
+ if reflection.type
166
+ scope = scope.where(table[reflection.type].eq(owner.class.base_class.name))
167
+ end
168
+
169
+ conditions.first.each do |condition|
170
+ scope = scope.where(interpolate(condition))
171
+ end
172
+
173
+ scope
174
+ end
175
+
176
+ end
177
+
178
+ module CustomizableAssociation
179
+
180
+ # Undefine all mutator methods.
181
+ def self.included(base)
182
+ [ :writer, :ids_writer, :set_owner_attributes, :add_to_target, :set_new_record,
183
+ :replace, :replace_records, :callback, :callbacks_for,
184
+ :build, :build_record, :create_scope, :creation_attributes,
185
+ :create, :create!, :concat, :create_record, :insert_record, :concat_records,
186
+ :delete_all, :delete_all_on_destroy, :destroy_all, :delete, :destroy,
187
+ :delete_or_destroy, :remove_records, :delete_records
188
+ ].each do |name|
189
+ begin undef_method name
190
+ rescue NameError
191
+ end
192
+ end
193
+ end
194
+
195
+ # Overridden to always return true.
196
+ def foreign_key_present?
197
+ true
198
+ end
199
+
200
+ # Overridden to use CustomizableAssociationScope.
201
+ def association_scope
202
+ @association_scope ||= CustomizableAssociationScope.new(self).scope if klass
203
+ end
204
+ end
205
+
206
+ # Patterned after belongs_to and has_one associations in AR.
207
+ class HasOneCustomAssociation < ActiveRecord::Associations::SingularAssociation
208
+ include CustomizableAssociation
209
+ end
210
+
211
+ # Patterned after has_many associations in AR.
212
+ class HasManyCustomAssociation < ActiveRecord::Associations::CollectionAssociation
213
+ include CustomizableAssociation
214
+
215
+ private
216
+
217
+ # Copied from ActiveRecord::Associations::HasManyAssociation
218
+ def count_records
219
+ count = if options[:counter_sql] || options[:finder_sql]
220
+ reflection.klass.count_by_sql(custom_counter_sql)
221
+ else
222
+ scoped.count
223
+ end
224
+
225
+ # If there's nothing in the database and @target has no new records
226
+ # we are certain the current target is an empty array. This is a
227
+ # documented side-effect of the method that may avoid an extra SELECT.
228
+ @target ||= [] and loaded! if count == 0
229
+
230
+ [options[:limit], count].compact.min
231
+ end
232
+ end
233
+
234
+ end
235
+
236
+ # Reflection for custom associations.
237
+ class Reflection < ActiveRecord::Reflection::AssociationReflection
238
+ def initialize(macro, name, options, active_record)
239
+ super
240
+ @collection = (macro==:has_many_custom)
241
+ end
242
+
243
+ def association_class
244
+ case macro
245
+ when :has_one_custom
246
+ Associations::HasOneCustomAssociation
247
+ when :has_many_custom
248
+ Associations::HasManyCustomAssociation
249
+ end
250
+ end
251
+ end
252
+
253
+ # Relation extensions to disable preloading for custom associations.
254
+ module Relation
255
+ extend ActiveSupport::Concern
256
+
257
+ included do
258
+ alias_method_chain :eager_loading?, :custom
259
+ end
260
+
261
+ def eager_loading_with_custom?
262
+ @should_eager_load ||= eager_loading_without_custom? || ! supports_preloading?
263
+ end
264
+
265
+ private
266
+
267
+ # Checks if this relation supports preloading (which is disabled for custom associations)
268
+ def supports_preloading?(klass=@klass, includes=@includes_values)
269
+ includes.all? do |(a,b)|
270
+ a = klass.reflections[a.to_sym]
271
+ a && ! a.is_a?(Reflection) && (! b.present? || supports_preloading?(a.klass, Array.wrap(b)))
272
+ end
273
+ end
274
+ end
275
+
276
+ # Extensions to support custom associations.
277
+ module Core
278
+ extend ActiveSupport::Concern
279
+
280
+ module ClassMethods
281
+ # DSL method
282
+ def has_one_custom(name, options = {}, &extension)
283
+ Builder::HasOneCustom.build(self, name, options, &extension)
284
+ end
285
+
286
+ # DSL method
287
+ def has_many_custom(name, options = {}, &extension)
288
+ Builder::HasManyCustom.build(self, name, options, &extension)
289
+ end
290
+
291
+ # Overridden to create reflections for custom associations.
292
+ def create_reflection(macro, name, options, active_record)
293
+ case macro
294
+ when :has_one_custom, :has_many_custom
295
+ reflection = CustomAssociations::Reflection.new(macro, name, options, active_record)
296
+ self.reflections = self.reflections.merge(name => reflection)
297
+ reflection
298
+ else
299
+ super
300
+ end
301
+ end
302
+ end
303
+ end
304
+
305
+ # Installs this extension into ActiveRecord.
306
+ def self.initialize!
307
+ ::ActiveRecord::Base.send :include, Core
308
+ ::ActiveRecord::Relation.send :include, Relation
309
+ ::ActiveRecord::Associations::JoinDependency.send :include, Associations::JoinDependency
310
+ ::ActiveRecord::Associations::JoinDependency::JoinAssociation.send :include, Associations::JoinAssociation
311
+ end
312
+ end
313
+
314
+ ActiveSupport.on_load(:active_record) do
315
+ CustomAssociations.initialize!
316
+ end
@@ -0,0 +1,3 @@
1
+ module CustomAssociations
2
+ VERSION = "0.1.0.pre"
3
+ 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
+ | |-- assets
177
+ | `-- tasks
178
+ |-- log
179
+ |-- public
180
+ |-- script
181
+ |-- test
182
+ | |-- fixtures
183
+ | |-- functional
184
+ | |-- integration
185
+ | |-- performance
186
+ | `-- unit
187
+ |-- tmp
188
+ | `-- cache
189
+ | `-- assets
190
+ `-- vendor
191
+ |-- assets
192
+ | |-- javascripts
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.