aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/guides/source/active_support_core_extensions.textile56
-rw-r--r--railties/guides/source/api_documentation_guidelines.textile187
-rw-r--r--railties/guides/source/index.html.erb16
-rw-r--r--railties/guides/source/initialization.textile13
-rw-r--r--railties/guides/source/layout.html.erb5
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb1
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/application.rb9
-rw-r--r--railties/lib/rails/paths.rb40
-rw-r--r--railties/test/generators/app_generator_test.rb7
-rw-r--r--railties/test/paths_test.rb24
10 files changed, 305 insertions, 53 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index 097d51e007..a0ed8d6a90 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -981,7 +981,9 @@ h3. Extensions to +Class+
h4. Class Attributes
-The method +Class#class_attribute+ declares one or more inheritable class attributes that can be overridden at any level down the hierarchy:
+h5. +class_attribute+
+
+The method +class_attribute+ declares one or more inheritable class attributes that can be overridden at any level down the hierarchy:
<ruby>
class A
@@ -1005,17 +1007,50 @@ A.x # => :a
B.x # => :b
</ruby>
-For example that's the way the +allow_forgery_protection+ flag is implemented for controllers:
+For example +ActionMailer::Base+ defines:
+
+<ruby>
+class_attribute :default_params
+self.default_params = {
+ :mime_version => "1.0",
+ :charset => "UTF-8",
+ :content_type => "text/plain",
+ :parts_order => [ "text/plain", "text/enriched", "text/html" ]
+}.freeze
+</ruby>
+
+They can be also accessed and overridden at the instance level:
+
+<ruby>
+A.x = 1
+
+a1 = A.new
+a2 = A.new
+a2.x = 2
+
+a1.x # => 1, comes from A
+a2.x # => 2, overridden in a2
+</ruby>
+
+The generation of the writer instance method can be prevented by setting the option +:instance_writer+ to false, as in
<ruby>
-class_attribute :allow_forgery_protection
-self.allow_forgery_protection = true
+module AcitveRecord
+ class Base
+ class_attribute :table_name_prefix, :instance_writer => false
+ self.table_name_prefix = ""
+ end
+end
</ruby>
-For convenience +class_attribute+ defines also a predicate, so that declaration also generates +allow_forgery_protection?+. Such predicate returns the double boolean negation of the value.
+A model may find that option useful as a way to prevent mass-assignment from setting the attribute.
+
+For convenience +class_attribute+ defines also an instance predicate which is the double negation of what the instance reader returns. In the examples above it would be called +x?+.
NOTE: Defined in +active_support/core_ext/class/attribute.rb+
+h5. +cattr_reader+, +cattr_writer+, and +cattr_accessor+
+
The macros +cattr_reader+, +cattr_writer+, and +cattr_accessor+ are analogous to their +attr_*+ counterparts but for classes. They initialize a class variable to +nil+ unless it already exists, and generate the corresponding class methods to access it:
<ruby>
@@ -1026,17 +1061,18 @@ class MysqlAdapter < AbstractAdapter
end
</ruby>
-Instance methods are created as well for convenience. For example given
+Instance methods are created as well for convenience, they are just proxies to the class attribute. So, instances can change the class attribute, but cannot override it as it happens with +class_attribute+ (see above). For example given
<ruby>
-module ActionController
+module ActionView
class Base
- cattr_accessor :logger
+ cattr_accessor :field_error_proc
+ @@field_error_proc = Proc.new{ ... }
end
end
</ruby>
-we can access +logger+ in actions. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):
+we can access +field_error_proc+ in views. The generation of the writer instance method can be prevented by setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):
<ruby>
module ActiveRecord
@@ -1047,7 +1083,7 @@ module ActiveRecord
end
</ruby>
-A model may find that option useful as a way to prevent mass-assignment from setting the attribute.
+A model may find that option useful as a way to prevent mass-assignment from setting the attribute.
NOTE: Defined in +active_support/core_ext/class/attribute_accessors.rb+.
diff --git a/railties/guides/source/api_documentation_guidelines.textile b/railties/guides/source/api_documentation_guidelines.textile
new file mode 100644
index 0000000000..d9a0d39d9d
--- /dev/null
+++ b/railties/guides/source/api_documentation_guidelines.textile
@@ -0,0 +1,187 @@
+h2. API Documentation Guidelines
+
+This guide documents the Ruby on Rails API documentation guidelines.
+
+endprologue.
+
+h3. RDoc
+
+The Rails API documentation is generated with RDoc 2.5. Please consult the "RDoc documentation":http://rdoc.rubyforge.org/RDoc.htmlFor for help with its markup.
+
+h3. Wording
+
+Write simple, declarative sentences. Brevity is a plus: get to the point.
+
+Write in present tense: "Returns a hash that...", rather than "Returned a hash that..." or "Will return a hash that...".
+
+Start comments in upper case, follow regular punctuation rules:
+
+<ruby>
+# Declares an attribute reader backed by an internally-named instance variable.
+def attr_internal_reader(*attrs)
+ ...
+end
+</ruby>
+
+Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the recommended idioms in edge, reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.
+
+Documentation has to be concise but comprehensive. Explore and document edge cases. What happens if a module is anonymous? What if a collection is empty? What if an argument is nil?
+
+The proper names of Rails components have a space in between the words, like "Active Support". +ActiveRecord+ is a Ruby module, whereas Active Record is an ORM. Historically there has been lack of consistency regarding this, but we checked with David when docrails started. All Rails documentation consistently refer to Rails components by their proper name, and if in your next blog post or presentation you remember this tidbit and take it into account that'd be fenomenal :).
+
+Spell names correctly: HTML, MySQL, JavaScript, ERb.
+
+h3. Example Code
+
+Choose meaningful examples that depict and cover the basics as well as interesting points or gotchas.
+
+Use two spaces to indent chunks of code.—that is two spaces with respect to the left margin; the examples
+themselves should use "Rails code conventions":http://rails.lighthouseapp.com/projects/8994/source-style.
+
+Short docs do not need an explicit "Examples" label to introduce snippets, they just follow paragraphs:
+
+<ruby>
+# Converts a collection of elements into a formatted string by calling
+# <tt>to_s</tt> on all elements and joining them.
+#
+# Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
+</ruby>
+
+On the other hand big chunks of structured documentation may have a separate "Examples" section:
+
+<ruby>
+# ==== Examples
+#
+# Person.exists?(5)
+# Person.exists?('5')
+# Person.exists?(:name => "David")
+# Person.exists?(['name LIKE ?', "%#{query}%"])
+</ruby>
+
+The result of expressions follow them and are introduced by "# => ", vertically aligned:
+
+<ruby>
+# For checking if a fixnum is even or odd.
+#
+# 1.even? # => false
+# 1.odd? # => true
+# 2.even? # => true
+# 2.odd? # => false
+</ruby>
+
+If a line is too long, the comment may be placed on the next line:
+
+<ruby>
+ # label(:post, :title)
+ # # => <label for="post_title">Title</label>
+ #
+ # label(:post, :title, "A short title")
+ # # => <label for="post_title">A short title</label>
+ #
+ # label(:post, :title, "A short title", :class => "title_label")
+ # # => <label for="post_title" class="title_label">A short title</label>
+</ruby>
+
+Avoid using any printing methods like +puts+ or +p+ for that purpose.
+
+On the other hand, regular comments do not use an arrow:
+
+<ruby>
+# polymorphic_url(record) # same as comment_url(record)
+</ruby>
+
+h3. Filenames
+
+As a rule of thumb use filenames relative to the application root:
+
+<plain>
+config/routes.rb # YES
+routes.rb # NO
+RAILS_ROOT/config/routes.rb # NO
+</plain>
+
+
+h3. Fonts
+
+h4. Fixed-width Font
+
+Use fixed-width fonts for:
+* constants, in particular class and module names
+* method names
+* literals like +nil+, +false+, +true+, +self+
+* symbols
+* method parameters
+* file names
+
+<ruby>
+# Copies the instance variables of +object+ into +self+.
+#
+# Instance variable names in the +exclude+ array are ignored. If +object+
+# responds to <tt>protected_instance_variables</tt> the ones returned are
+# also ignored. For example, Rails controllers implement that method.
+# ...
+def copy_instance_variables_from(object, exclude = [])
+ ...
+end
+</ruby>
+
+WARNING: Using a pair of +&#43;...&#43;+ for fixed-width font only works with *words*; that is: anything matching <tt>\A\w&#43;\z</tt>. For anything else use +&lt;tt&gt;...&lt;/tt&gt;+, notably symbols, setters, inline snippets, etc:
+
+h4. Regular Font
+
+When "true" and "false" are English words rather than Ruby keywords use a regular font:
+
+<ruby>
+# If <tt>reload_plugins?</tt> is false, add this to your plugin's <tt>init.rb</tt>
+# to make it reloadable:
+#
+# Dependencies.load_once_paths.delete lib_path
+</ruby>
+
+h3. Description Lists
+
+In lists of options, parameters, etc. use a hyphen between the item and its description (reads better than a colon because normally options are symbols):
+
+<ruby>
+# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
+</ruby>
+
+The description starts in upper case and ends with a full stop—it's standard English.
+
+h3. Dynamically Generated Methods
+
+Methods created with +(module|class)_eval(STRING)+ have a comment by their side with an instance of the generated code. That comment is 2 spaces apart from the template:
+
+<ruby>
+for severity in Severity.constants
+ class_eval <<-EOT, __FILE__, __LINE__
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
+ end # end
+ #
+ def #{severity.downcase}? # def debug?
+ #{severity} >= @level # DEBUG >= @level
+ end # end
+ EOT
+end
+</ruby>
+
+If the resulting lines are too wide, say 200 columns or more, we put the comment above the call:
+
+<ruby>
+# def self.find_by_login_and_activated(*args)
+# options = args.extract_options!
+# ...
+# end
+self.class_eval %{
+ def self.#{method_id}(*args)
+ options = args.extract_options!
+ ...
+ end
+}
+</ruby>
+
+h3. Changelog
+
+* July 17, 2010: ported from the docrails wiki and revised by "Xavier Noria":credits.html#fxn
+
diff --git a/railties/guides/source/index.html.erb b/railties/guides/source/index.html.erb
index a930db0f1d..254ee91ab4 100644
--- a/railties/guides/source/index.html.erb
+++ b/railties/guides/source/index.html.erb
@@ -123,10 +123,6 @@ Ruby on Rails Guides
<%= guide("Caching with Rails", 'caching_with_rails.html', :ticket => 10) do %>
<p>Various caching techniques provided by Rails.</p>
<% end %>
-
-<%= guide("Contributing to Rails", 'contributing_to_rails.html') do %>
- <p>Rails is not &quot;somebody else's framework.&quot; This guide covers a variety of ways that you can get involved in the ongoing development of Rails.</p>
-<% end %>
</dl>
<h3>Extending Rails</h3>
@@ -147,6 +143,18 @@ Ruby on Rails Guides
<% end %>
</dl>
+<h3>Contributing to Rails</h3>
+
+<dl>
+ <%= guide("Contributing to Rails", 'contributing_to_rails.html') do %>
+ <p>Rails is not &quot;somebody else's framework.&quot; This guide covers a variety of ways that you can get involved in the ongoing development of Rails.</p>
+ <% end %>
+
+ <%= guide('API Documentation Guidelines', 'api_documentation_guidelines.html') do %>
+ <p>This guide documents the Ruby on Rails API documentation guidelines.</p>
+ <% end %>
+</dl>
+
<h3>Release Notes</h3>
<dl>
diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile
index 7a44ef7c77..305602e57d 100644
--- a/railties/guides/source/initialization.textile
+++ b/railties/guides/source/initialization.textile
@@ -141,20 +141,21 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This
* activesupport-3.0.0.beta4.gem
* arel-0.4.0.gem
* builder-2.1.2.gem
-* bundler-1.0.0.beta.2.gem
+* bundler-1.0.0.beta.5.gem
* erubis-2.6.6.gem
* i18n-0.4.1.gem
-* mail-2.2.4.gem
-* memcache-client-1.8.3.gem
+* mail-2.2.5.gem
+* memcache-client-1.8.5.gem
* mime-types-1.16.gem
+* nokogiri-1.4.2.gem
* polyglot-0.3.1.gem
-* rack-1.1.0.gem
-* rack-mount-0.6.5.gem
+* rack-1.2.1.gem
+* rack-mount-0.6.9.gem
* rack-test-0.5.4.gem
* rails-3.0.0.beta4.gem
* railties-3.0.0.beta4.gem
* rake-0.8.7.gem
-* sqlite3-ruby-1.3.0.gem
+* sqlite3-ruby-1.3.1.gem
* text-format-1.0.0.gem
* text-hyphen-1.0.0.gem
* thor-0.13.7.gem
diff --git a/railties/guides/source/layout.html.erb b/railties/guides/source/layout.html.erb
index 501d8fef6d..c4758316ea 100644
--- a/railties/guides/source/layout.html.erb
+++ b/railties/guides/source/layout.html.erb
@@ -71,13 +71,16 @@
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
- <dd><a href="contributing_to_rails.html">Contributing to Rails</a></dd>
<dt>Extending Rails</dt>
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Adding a Generator to Your Plugin</a></dd>
+ <dt>Contributing to Rails</dt>
+ <dd><a href="contributing_to_rails.html">Contributing to Rails</a></dd>
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
+
<dt>Release Notes</dt>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index 7d50e7da67..c99aa3c0cd 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -115,6 +115,7 @@ module Rails
directory "public/javascripts"
else
empty_directory_with_gitkeep "public/javascripts"
+ create_file "public/javascripts/application.js"
end
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb
index 67a38ea1d5..7a94d6e05c 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/application.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb
@@ -22,7 +22,7 @@ module <%= app_const_base %>
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
- # config.autoload_paths += %W( #{config.root}/extras )
+ # config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
@@ -39,6 +39,13 @@ module <%= app_const_base %>
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
+ # JavaScript files you want as :defaults (application.js is always included).
+<% if options[:skip_prototype] -%>
+ config.action_view.javascript_expansions[:defaults] = %w()
+<% else -%>
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
+<% end -%>
+
# Configure generators values. Many other options are available, be sure to check the documentation.
# config.generators do |g|
# g.orm :active_record
diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb
index 7a65188a9a..d303212f52 100644
--- a/railties/lib/rails/paths.rb
+++ b/railties/lib/rails/paths.rb
@@ -116,36 +116,20 @@ module Rails
@paths.concat paths
end
- def autoload_once!
- @autoload_once = true
- end
-
- def autoload_once?
- @autoload_once
- end
-
- def eager_load!
- @eager_load = true
- end
-
- def eager_load?
- @eager_load
- end
-
- def autoload!
- @autoload = true
- end
-
- def autoload?
- @autoload
- end
+ %w(autoload_once eager_load autoload load_path).each do |m|
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{m}!
+ @#{m} = true
+ end
- def load_path!
- @load_path = true
- end
+ def skip_#{m}!
+ @#{m} = false
+ end
- def load_path?
- @load_path
+ def #{m}?
+ @#{m}
+ end
+ RUBY
end
def paths
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index 6a3b5de9de..7018816af0 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -129,14 +129,19 @@ class AppGeneratorTest < Rails::Generators::TestCase
def test_prototype_and_test_unit_are_added_by_default
run_generator
+ assert_file "config/application.rb", /#\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(jquery rails\)/
+ assert_file "public/javascripts/application.js"
assert_file "public/javascripts/prototype.js"
+ assert_file "public/javascripts/rails.js"
assert_file "test"
end
def test_prototype_and_test_unit_are_skipped_if_required
run_generator [destination_root, "--skip-prototype", "--skip-testunit"]
+ assert_file "config/application.rb", /^\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(\)/
+ assert_file "public/javascripts/application.js"
assert_no_file "public/javascripts/prototype.js"
- assert_file "public/javascripts"
+ assert_no_file "public/javascripts/rails.js"
assert_no_file "test"
end
diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb
index 008247cb1a..80fae8c543 100644
--- a/railties/test/paths_test.rb
+++ b/railties/test/paths_test.rb
@@ -118,13 +118,23 @@ class PathsTest < ActiveSupport::TestCase
assert_raise(RuntimeError) { @root << "/biz" }
end
- test "it is possible to add a path that should be loaded only once" do
+ test "it is possible to add a path that should be autoloaded only once" do
@root.app = "/app"
@root.app.autoload_once!
assert @root.app.autoload_once?
assert @root.autoload_once.include?(@root.app.paths.first)
end
+ test "it is possible to remove a path that should be autoloaded only once" do
+ @root.app = "/app"
+ @root.app.autoload_once!
+ assert @root.app.autoload_once?
+
+ @root.app.skip_autoload_once!
+ assert !@root.app.autoload_once?
+ assert !@root.autoload_once.include?(@root.app.paths.first)
+ end
+
test "it is possible to add a path without assignment and specify it should be loaded only once" do
@root.app "/app", :autoload_once => true
assert @root.app.autoload_once?
@@ -152,13 +162,23 @@ class PathsTest < ActiveSupport::TestCase
assert_equal 2, @root.autoload_once.size
end
- test "it is possible to mark a path as eager" do
+ test "it is possible to mark a path as eager loaded" do
@root.app = "/app"
@root.app.eager_load!
assert @root.app.eager_load?
assert @root.eager_load.include?(@root.app.paths.first)
end
+ test "it is possible to skip a path from eager loading" do
+ @root.app = "/app"
+ @root.app.eager_load!
+ assert @root.app.eager_load?
+
+ @root.app.skip_eager_load!
+ assert !@root.app.eager_load?
+ assert !@root.eager_load.include?(@root.app.paths.first)
+ end
+
test "it is possible to add a path without assignment and mark it as eager" do
@root.app "/app", :eager_load => true
assert @root.app.eager_load?