Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:rails/rails

  • Loading branch information...
commit 26c5680bd01bd0e525eccc5d47a2e7126d9383f7 2 parents b870dab + 4b91daf
@wycats wycats authored
Showing with 316 additions and 104 deletions.
  1. +6 −1 Gemfile
  2. +0 −1  actionmailer/actionmailer.gemspec
  3. +0 −6 actionmailer/lib/action_mailer.rb
  4. +7 −0 actionmailer/lib/action_mailer/mail_helper.rb
  5. +4 −1 actionpack/actionpack.gemspec
  6. +45 −3 actionpack/lib/action_controller/metal.rb
  7. +4 −4 actionpack/lib/action_dispatch/middleware/stack.rb
  8. +4 −0 actionpack/lib/action_view/base.rb
  9. +0 −7 actionpack/lib/action_view/helpers/active_model_helper.rb
  10. +13 −19 actionpack/lib/action_view/helpers/text_helper.rb
  11. +22 −11 actionpack/test/controller/new_base/middleware_test.rb
  12. +2 −0  activemodel/activemodel.gemspec
  13. +6 −6 activemodel/lib/active_model/errors.rb
  14. +1 −0  activerecord/activerecord.gemspec
  15. +1 −0  activerecord/lib/active_record/railties/databases.rake
  16. +14 −1 activerecord/lib/active_record/relation/query_methods.rb
  17. +5 −0 activerecord/test/cases/relations_test.rb
  18. +2 −0  activesupport/CHANGELOG
  19. +0 −5 activesupport/activesupport.gemspec
  20. +6 −0 activesupport/lib/active_support/builder.rb
  21. +6 −1 activesupport/lib/active_support/cache/mem_cache_store.rb
  22. +10 −4 activesupport/lib/active_support/core_ext/array/conversions.rb
  23. +1 −1  activesupport/lib/active_support/core_ext/hash/conversions.rb
  24. +29 −0 activesupport/lib/active_support/core_ext/string/filters.rb
  25. +1 −0  activesupport/lib/active_support/core_ext/string/interpolation.rb
  26. +7 −2 activesupport/lib/active_support/i18n.rb
  27. +7 −1 activesupport/lib/active_support/values/time_zone.rb
  28. +29 −0 activesupport/test/core_ext/string_ext_test.rb
  29. +1 −1  railties/guides/source/action_view_overview.textile
  30. +33 −0 railties/guides/source/active_support_core_extensions.textile
  31. +5 −9 railties/guides/source/activerecord_validations_callbacks.textile
  32. +4 −8 railties/guides/source/generators.textile
  33. +1 −1  railties/guides/source/getting_started.textile
  34. +2 −2 railties/guides/source/nested_model_forms.textile
  35. +11 −0 railties/lib/rails/application.rb
  36. +1 −1  railties/lib/rails/application/finisher.rb
  37. +1 −0  railties/lib/rails/commands/destroy.rb
  38. +1 −0  railties/lib/rails/commands/generate.rb
  39. +2 −6 railties/lib/rails/generators.rb
  40. +17 −0 railties/test/application/initializers/load_path_test.rb
  41. +5 −2 railties/test/generators/generators_test_helper.rb
View
7 Gemfile
@@ -21,6 +21,12 @@ elsif RUBY_ENGINE == "jruby"
gem "jruby-openssl"
end
+# AS
+gem "memcache-client", ">= 1.7.5"
+
+# AM
+gem "text-format", "~> 1.0.0"
+
# AR
if mri || RUBY_ENGINE == "rbx"
gem "sqlite3-ruby", "= 1.3.0.beta.2", :require => 'sqlite3'
@@ -39,7 +45,6 @@ elsif RUBY_ENGINE == "jruby"
end
# AP
-gem "rack-test", "0.5.3", :require => 'rack/test'
gem "RedCloth", ">= 4.2.2"
group :documentation do
View
1  actionmailer/actionmailer.gemspec
@@ -21,5 +21,4 @@ Gem::Specification.new do |s|
s.add_dependency('actionpack', version)
s.add_dependency('mail', '~> 2.2.1')
- s.add_dependency('text-format', '~> 1.0.0')
end
View
6 actionmailer/lib/action_mailer.rb
@@ -49,9 +49,3 @@ module ActionMailer
autoload :TestCase
autoload :TestHelper
end
-
-module Text
- extend ActiveSupport::Autoload
-
- autoload :Format, 'text/format'
-end
View
7 actionmailer/lib/action_mailer/mail_helper.rb
@@ -3,6 +3,13 @@ module MailHelper
# Uses Text::Format to take the text and format it, indented two spaces for
# each line, and wrapped at 72 columns.
def block_format(text)
+ begin
+ require 'text/format'
+ rescue LoadError => e
+ $stderr.puts "You don't have text-format installed in your application. Please add it to your Gemfile and run bundle install"
+ raise e
+ end unless defined?(Text::Format)
+
formatted = text.split(/\n\r\n/).collect { |paragraph|
Text::Format.new(
:columns => 72, :first_indent => 2, :body_indent => 2, :text => paragraph
View
5 actionpack/actionpack.gemspec
@@ -21,8 +21,11 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', version)
s.add_dependency('activemodel', version)
+ s.add_dependency('builder', '~> 2.1.2')
+ s.add_dependency('i18n', '~> 0.4.0')
s.add_dependency('rack', '~> 1.1.0')
- s.add_dependency('rack-test', '~> 0.5.0')
+ s.add_dependency('rack-test', '~> 0.5.4')
s.add_dependency('rack-mount', '~> 0.6.3')
+ s.add_dependency('tzinfo', '~> 0.3.16')
s.add_dependency('erubis', '~> 2.6.5')
end
View
48 actionpack/lib/action_controller/metal.rb
@@ -1,6 +1,48 @@
require 'active_support/core_ext/class/attribute'
+require 'active_support/core_ext/object/blank'
+require 'action_dispatch/middleware/stack'
module ActionController
+ # Extend ActionDispatch middleware stack to make it aware of options
+ # allowing the following syntax in controllers:
+ #
+ # class PostsController < ApplicationController
+ # use AuthenticationMiddleware, :except => [:index, :show]
+ # end
+ #
+ class MiddlewareStack < ActionDispatch::MiddlewareStack #:nodoc:
+ class Middleware < ActionDispatch::MiddlewareStack::Middleware #:nodoc:
+ def initialize(klass, *args)
+ options = args.extract_options!
+ @only = Array(options.delete(:only)).map(&:to_s)
+ @except = Array(options.delete(:except)).map(&:to_s)
+ args << options unless options.empty?
+ super
+ end
+
+ def valid?(action)
+ if @only.present?
+ @only.include?(action)
+ elsif @except.present?
+ !@except.include?(action)
+ else
+ true
+ end
+ end
+ end
+
+ def build(action, app=nil, &block)
+ app ||= block
+ action = action.to_s
+ raise "MiddlewareStack#build requires an app" unless app
+
+ reverse.inject(app) do |a, middleware|
+ middleware.valid?(action) ?
+ middleware.build(a) : a
+ end
+ end
+ end
+
# ActionController::Metal provides a way to get a valid Rack application from a controller.
#
# In AbstractController, dispatching is triggered directly by calling #process on a new controller.
@@ -91,10 +133,10 @@ def to_a
end
class_attribute :middleware_stack
- self.middleware_stack = ActionDispatch::MiddlewareStack.new
+ self.middleware_stack = ActionController::MiddlewareStack.new
def self.inherited(base)
- self.middleware_stack = base.middleware_stack.dup
+ base.middleware_stack = self.middleware_stack.dup
super
end
@@ -120,7 +162,7 @@ def self.call(env)
# ==== Returns
# Proc:: A rack application
def self.action(name, klass = ActionDispatch::Request)
- middleware_stack.build do |env|
+ middleware_stack.build(name.to_s) do |env|
new.dispatch(name, klass.new(env))
end
end
View
8 actionpack/lib/action_dispatch/middleware/stack.rb
@@ -55,7 +55,7 @@ def initialize(*args, &block)
def insert(index, *args, &block)
index = self.index(index) unless index.is_a?(Integer)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
super(index, middleware)
end
@@ -73,7 +73,7 @@ def swap(target, *args, &block)
end
def use(*args, &block)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
push(middleware)
end
@@ -82,8 +82,8 @@ def active
"was removed from the middleware stack", caller
end
- def build(app = nil, &blk)
- app ||= blk
+ def build(app = nil, &block)
+ app ||= block
raise "MiddlewareStack#build requires an app" unless app
reverse.inject(app) { |a, e| e.build(a) }
end
View
4 actionpack/lib/action_view/base.rb
@@ -165,6 +165,10 @@ module Subclasses
cattr_accessor :debug_rjs
@@debug_rjs = false
+ # Specify the proc used to decorate input tags that refer to attributes with errors.
+ cattr_accessor :field_error_proc
+ @@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
+
class_attribute :helpers
remove_method :helpers
attr_reader :helpers
View
7 actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -4,13 +4,6 @@
require 'active_support/core_ext/object/blank'
module ActionView
- ActiveSupport.on_load(:action_view) do
- class ActionView::Base
- @@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
- cattr_accessor :field_error_proc
- end
- end
-
module Helpers
module ActiveModelHelper
%w(input form error_messages_for error_message_on).each do |method|
View
32 actionpack/lib/action_view/helpers/text_helper.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/string/filters'
require 'action_view/helpers/tag_helper'
module ActionView
@@ -42,28 +43,25 @@ def safe_concat(string)
# ==== Examples
#
# truncate("Once upon a time in a world far far away")
- # # => Once upon a time in a world...
+ # # => "Once upon a time in a world..."
#
- # truncate("Once upon a time in a world far far away", :separator => ' ')
- # # => Once upon a time in a world...
+ # truncate("Once upon a time in a world far far away", :length => 17)
+ # # => "Once upon a ti..."
#
- # truncate("Once upon a time in a world far far away", :length => 14)
- # # => Once upon a...
+ # truncate("Once upon a time in a world far far away", :lenght => 17, :separator => ' ')
+ # # => "Once upon a..."
#
- # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
- # # => And they found t(clipped)
- #
- # truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 25)
- # # => And they f... (continued)
+ # truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
+ # # => "And they f... (continued)"
#
# You can still use <tt>truncate</tt> with the old API that accepts the
# +length+ as its optional second and the +ellipsis+ as its
# optional third parameter:
# truncate("Once upon a time in a world far far away", 14)
- # # => Once upon a...
+ # # => "Once upon a..."
#
# truncate("And they found that many people were sleeping better.", 25, "... (continued)")
- # # => And they f... (continued)
+ # # => "And they f... (continued)"
def truncate(text, *args)
options = args.extract_options!
unless args.empty?
@@ -73,14 +71,10 @@ def truncate(text, *args)
options[:length] = args[0] || 30
options[:omission] = args[1] || "..."
end
- options.reverse_merge!(:length => 30, :omission => "...")
- if text
- l = options[:length] - options[:omission].mb_chars.length
- chars = text.mb_chars
- stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, l) || l) : l
- (chars.length > options[:length] ? chars[0...stop] + options[:omission] : text).to_s
- end
+ options.reverse_merge!(:length => 30)
+
+ text.truncate(options.delete(:length), options) if text
end
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
View
33 actionpack/test/controller/new_base/middleware_test.rb
@@ -28,7 +28,6 @@ def call(env)
class MyController < ActionController::Metal
use MyMiddleware
-
middleware.insert_before MyMiddleware, ExclaimerMiddleware
def index
@@ -39,8 +38,23 @@ def index
class InheritedController < MyController
end
- module MiddlewareTests
- extend ActiveSupport::Testing::Declarative
+ class ActionsController < ActionController::Metal
+ use MyMiddleware, :only => :show
+ middleware.insert_before MyMiddleware, ExclaimerMiddleware, :except => :index
+
+ def index
+ self.response_body = "index"
+ end
+
+ def show
+ self.response_body = "show"
+ end
+ end
+
+ class TestMiddleware < ActiveSupport::TestCase
+ def setup
+ @app = MyController.action(:index)
+ end
test "middleware that is 'use'd is called as part of the Rack application" do
result = @app.call(env_for("/"))
@@ -52,13 +66,13 @@ module MiddlewareTests
result = @app.call(env_for("/"))
assert_equal "First!", result[1]["Middleware-Order"]
end
- end
- class TestMiddleware < ActiveSupport::TestCase
- include MiddlewareTests
+ test "middleware stack accepts only and except as options" do
+ result = ActionsController.action(:show).call(env_for("/"))
+ assert_equal "First!", result[1]["Middleware-Order"]
- def setup
- @app = MyController.action(:index)
+ result = ActionsController.action(:index).call(env_for("/"))
+ assert_nil result[1]["Middleware-Order"]
end
def env_for(url)
@@ -70,8 +84,5 @@ class TestInheritedMiddleware < TestMiddleware
def setup
@app = InheritedController.action(:index)
end
-
- test "middleware inherits" do
- end
end
end
View
2  activemodel/activemodel.gemspec
@@ -20,4 +20,6 @@ Gem::Specification.new do |s|
s.has_rdoc = true
s.add_dependency('activesupport', version)
+ s.add_dependency('builder', '~> 2.1.2')
+ s.add_dependency('i18n', '~> 0.4.0')
end
View
12 activemodel/lib/active_model/errors.rb
@@ -170,13 +170,13 @@ def to_xml(options={})
end
end
- # Adds an error message (+messsage+) to the +attribute+, which will be returned on a call to <tt>on(attribute)</tt>
- # for the same attribute and ensure that this error object returns false when asked if <tt>empty?</tt>. More than one
- # error can be added to the same +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>.
- # If no +messsage+ is supplied, :invalid is assumed.
+ # Adds +message+ to the error messages on +attribute+, which will be returned on a call to
+ # <tt>on(attribute)</tt> for the same attribute. More than one error can be added to the same
+ # +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>.
+ # If no +message+ is supplied, <tt>:invalid</tt> is assumed.
#
- # If +message+ is a Symbol, it will be translated, using the appropriate scope (see translate_error).
- # If +message+ is a Proc, it will be called, allowing for things like Time.now to be used within an error
+ # If +message+ is a symbol, it will be translated using the appropriate scope (see +translate_error+).
+ # If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error.
def add(attribute, message = nil, options = {})
message ||= :invalid
message = generate_message(attribute, message, options) if message.is_a?(Symbol)
View
1  activerecord/activerecord.gemspec
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', version)
s.add_dependency('activemodel', version)
s.add_dependency('arel', '~> 0.3.3')
+ s.add_dependency('tzinfo', '~> 0.3.16')
end
View
1  activerecord/lib/active_record/railties/databases.rake
@@ -435,6 +435,7 @@ namespace :db do
task :create => :environment do
raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
require 'rails/generators'
+ Rails::Generators.configure!
require 'rails/generators/rails/session_migration/session_migration_generator'
Rails::Generators::SessionMigrationGenerator.start [ ENV["MIGRATION"] || "add_sessions_table" ]
end
View
15 activerecord/lib/active_record/relation/query_methods.rb
@@ -9,7 +9,7 @@ module QueryMethods
(ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS).each do |query_method|
attr_accessor :"#{query_method}_values"
- next if [:where, :having].include?(query_method)
+ next if [:where, :having, :select].include?(query_method)
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(*args, &block)
new_relation = clone
@@ -21,6 +21,19 @@ def #{query_method}(*args, &block)
CEVAL
end
+ class_eval <<-CEVAL, __FILE__, __LINE__ + 1
+ def select(*args, &block)
+ if block_given?
+ to_a.select(&block)
+ else
+ new_relation = clone
+ value = Array.wrap(args.flatten).reject {|x| x.blank? }
+ new_relation.select_values += value if value.present?
+ new_relation
+ end
+ end
+ CEVAL
+
[:where, :having].each do |query_method|
class_eval <<-CEVAL, __FILE__, __LINE__ + 1
def #{query_method}(*args, &block)
View
5 activerecord/test/cases/relations_test.rb
@@ -112,6 +112,11 @@ def test_finding_with_group
assert_equal 4, developers.map(&:salary).uniq.size
end
+ def test_select_with_block
+ even_ids = Developer.scoped.select {|d| d.id % 2 == 0 }.map(&:id)
+ assert_equal [2, 4, 6, 8, 10], even_ids
+ end
+
def test_finding_with_hash_conditions_on_joined_table
firms = DependentFirm.joins(:account).where({:name => 'RailsCore', :accounts => { :credit_limit => 55..60 }}).to_a
assert_equal 1, firms.size
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.0.0 [beta 4/release candidate] (unreleased)*
+* Extracted String#truncate from TextHelper#truncate [DHH]
+
* Ruby 1.9: support UTF-8 case folding. #4595 [Norman Clarke]
* Renames Array#rand -> Array#random_element. [Santiago Pastorino, Rizwan Reza]
View
5 activesupport/activesupport.gemspec
@@ -18,9 +18,4 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.has_rdoc = true
-
- s.add_dependency('i18n', '~> 0.4.0')
- s.add_dependency('tzinfo', '~> 0.3.16')
- s.add_dependency('builder', '~> 2.1.2')
- s.add_dependency('memcache-client', '>= 1.7.5')
end
View
6 activesupport/lib/active_support/builder.rb
@@ -0,0 +1,6 @@
+begin
+ require 'builder'
+rescue LoadError => e
+ $stderr.puts "You don't have builder installed in your application. Please add it to your Gemfile and run bundle install"
+ raise e
+end
View
7 activesupport/lib/active_support/cache/mem_cache_store.rb
@@ -1,4 +1,9 @@
-require 'memcache'
+begin
+ require 'memcache'
+rescue LoadError => e
+ $stderr.puts "You don't have memcache installed in your application. Please add it to your Gemfile and run bundle install"
+ raise e
+end
require 'digest/md5'
module ActiveSupport
View
14 activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -9,9 +9,15 @@ class Array
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements in arrays with two elements (default: " and ")
# * <tt>:last_word_connector</tt> - The sign or word used to join the last element in arrays with three or more elements (default: ", and ")
def to_sentence(options = {})
- default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
- default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale])
- default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
+ if defined?(I18n)
+ default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
+ default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale])
+ default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
+ else
+ default_words_connector = ", "
+ default_two_words_connector = " and "
+ default_last_word_connector = ", and "
+ end
options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector
@@ -128,7 +134,7 @@ def to_formatted_s(format = :default)
# </messages>
#
def to_xml(options = {})
- require 'builder' unless defined?(Builder)
+ require 'active_support/builder' unless defined?(Builder)
options = options.dup
options[:indent] ||= 2
View
2  activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -55,7 +55,7 @@ class Hash
# configure your own builder with the <tt>:builder</tt> option. The method also accepts
# options like <tt>:dasherize</tt> and friends, they are forwarded to the builder.
def to_xml(options = {})
- require 'builder' unless defined?(Builder)
+ require 'active_support/builder' unless defined?(Builder)
options = options.dup
options[:indent] ||= 2
View
29 activesupport/lib/active_support/core_ext/string/filters.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/string/multibyte'
+
class String
# Returns the string, first removing all whitespace on both ends of
# the string, and then changing remaining consecutive whitespace
@@ -17,4 +19,31 @@ def squish!
gsub!(/\s+/, ' ')
self
end
+
+ # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
+ #
+ # "Once upon a time in a world far far away".truncate(27)
+ # # => "Once upon a time in a wo..."
+ #
+ # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
+ # for a total length not exceeding <tt>:length</tt>:
+ #
+ # "Once upon a time in a world far far away".truncate(27, :separator => ' ')
+ # # => "Once upon a time in a..."
+ #
+ # Pass a <tt>:separator</tt> to truncate +text+ at a natural break:
+ #
+ # "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
+ # # => "And they f... (continued)"
+ def truncate(length, options = {})
+ text = self.dup
+ options[:omission] ||= "..."
+
+ length_with_room_for_omission = length - options[:omission].mb_chars.length
+ chars = text.mb_chars
+ stop = options[:separator] ?
+ (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
+
+ (chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
+ end
end
View
1  activesupport/lib/active_support/core_ext/string/interpolation.rb
@@ -1 +1,2 @@
+require 'active_support/i18n'
require 'i18n/core_ext/string/interpolate'
View
9 activesupport/lib/active_support/i18n.rb
@@ -1,3 +1,8 @@
-require 'i18n'
+begin
+ require 'i18n'
+rescue LoadError => e
+ $stderr.puts "You don't have i18n installed in your application. Please add it to your Gemfile and run bundle install"
+ raise e
+end
I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml"
-ActiveSupport.run_load_hooks(:i18n)
+ActiveSupport.run_load_hooks(:i18n)
View
8 activesupport/lib/active_support/values/time_zone.rb
@@ -1,5 +1,11 @@
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/try'
+begin
+ require 'tzinfo'
+rescue LoadError => e
+ $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install"
+ raise e
+end
# The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following:
#
@@ -313,7 +319,7 @@ def period_for_local(time, dst=true)
# TODO: Preload instead of lazy load for thread safety
def self.find_tzinfo(name)
- require 'tzinfo' unless defined?(::TZInfo)
+ require 'active_support/tzinfo' unless defined?(::TZInfo)
::TZInfo::TimezoneProxy.new(MAPPING[name] || name)
end
View
29 activesupport/test/core_ext/string_ext_test.rb
@@ -210,6 +210,35 @@ def test_string_squish
# And changes the original string:
assert_equal original, expected
end
+
+ def test_truncate
+ assert_equal "Hello World!", "Hello World!".truncate(12)
+ assert_equal "Hello Wor...", "Hello World!!".truncate(12)
+ end
+
+ def test_truncate_with_omission_and_seperator
+ assert_equal "Hello[...]", "Hello World!".truncate(10, :omission => "[...]")
+ assert_equal "Hello[...]", "Hello Big World!".truncate(13, :omission => "[...]", :separator => ' ')
+ assert_equal "Hello Big[...]", "Hello Big World!".truncate(14, :omission => "[...]", :separator => ' ')
+ assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, :omission => "[...]", :separator => ' ')
+ end
+
+ if RUBY_VERSION < '1.9.0'
+ def test_truncate_multibyte
+ with_kcode 'none' do
+ assert_equal "\354\225\210\353\205\225\355...", "\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224".truncate(10)
+ end
+ with_kcode 'u' do
+ assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
+ "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".truncate(10)
+ end
+ end
+ else
+ def test_truncate_multibyte
+ assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'),
+ "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8').truncate(10)
+ end
+ end
end
class StringBehaviourTest < Test::Unit::TestCase
View
2  railties/guides/source/action_view_overview.textile
@@ -699,7 +699,7 @@ Creates a scope around a specific model object like form_for, but doesn‘t crea
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
- <% fields_for @person.permission do |permission_fields| %>
+ <%= fields_for @person.permission do |permission_fields| %>
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<% end %>
View
33 railties/guides/source/active_support_core_extensions.textile
@@ -1254,6 +1254,39 @@ There's also the destructive version +String#squish!+.
NOTE: Defined in +active_support/core_ext/string/filters.rb+.
+h4. +truncate+
+
+The method +truncate+ returns a copy of its receiver truncated after a given +length+:
+
+<ruby>
+"Oh dear! Oh dear! I shall be late!".truncate(20)
+# => "Oh dear! Oh dear!..."
+</ruby>
+
+Ellipsis can be customized with the +:omission+ option:
+
+<ruby>
+"Oh dear! Oh dear! I shall be late!".truncate(20, :omission => '&hellip;')
+# => "Oh dear! Oh &hellip;"
+</ruby>
+
+Note in particular that truncation takes into account the length of the omission string.
+
+Pass a +:separator+ to truncate the string at a natural break:
+
+<ruby>
+"Oh dear! Oh dear! I shall be late!".truncate(18)
+# => "Oh dear! Oh dea..."
+"Oh dear! Oh dear! I shall be late!".truncate(18, :separator => ' ')
+# => "Oh dear! Oh..."
+</ruby>
+
+In the above example "dear" gets cut first, but then +:separator+ prevents it.
+
+WARNING: The option +:separator+ can't be a regexp.
+
+NOTE: Defined in +active_support/core_ext/string/filters.rb+.
+
h4. Key-based Interpolation
In Ruby 1.9 the <tt>%</tt> string operator supports key-based interpolation, both formatted and unformatted:
View
14 railties/guides/source/activerecord_validations_callbacks.textile
@@ -879,32 +879,28 @@ Here is a list with all the available Active Record callbacks, listed in the sam
h4. Creating an Object
* +before_validation+
-* +before_validation_on_create+
* +after_validation+
-* +after_validation_on_create+
* +before_save+
+* +after_save+
* +before_create+
-* INSERT OPERATION
+* +around_create+
* +after_create+
-* +after_save+
h4. Updating an Object
* +before_validation+
-* +before_validation_on_update+
* +after_validation+
-* +after_validation_on_update+
* +before_save+
+* +after_save+
* +before_update+
-* UPDATE OPERATION
+* +around_update+
* +after_update+
-* +after_save+
h4. Destroying an Object
* +before_destroy+
-* DELETE OPERATION
* +after_destroy+
+* +around_destroy+
WARNING. +after_save+ runs both on create and update, but always _after_ the more specific callbacks +after_create+ and +after_update+, no matter the order in which the macro calls were executed.
View
12 railties/guides/source/generators.textile
@@ -322,14 +322,10 @@ config.generators do |g|
g.template_engine :erb
g.test_framework :shoulda, :fixture => false
g.stylesheets false
-end
-</ruby>
-
-And at the end of the same file:
-<ruby>
-require 'rails/generators'
-Rails::Generators.fallbacks[:shoulda] = :test_unit
+ # Add a fallback!
+ g.fallbacks[:should] = :test_unit
+end
</ruby>
Now, if create a Comment scaffold, you will see that shoulda generators are being invoked, and at the end, they are just falling back to test unit generators:
@@ -361,7 +357,7 @@ $ rails generate scaffold Comment body:text
create test/unit/helpers/comments_helper_test.rb
</shell>
-Such tool allows your generators to have single responsibility, increasing the code reuse and reducing the amount of code duplication.
+Such tool allows your generators to have single responsibility, increasing the code reuse and reducing the amount of duplication.
h3. Changelog
View
2  railties/guides/source/getting_started.textile
@@ -1342,7 +1342,7 @@ We also add a <tt>@post.tags.build</tt> at the top of this form, this is to make
Now create the folder <tt>app/views/tags</tt> and make a file in there called <tt>_form.html.erb</tt> which contains the form for the tag:
<erb>
-<% form.fields_for :tags do |tag_form| %>
+<%= form.fields_for :tags do |tag_form| %>
<div class="field">
<%= tag_form.label :name, 'Tag:' %>
<%= tag_form.text_field :name %>
View
4 railties/guides/source/nested_model_forms.textile
@@ -143,7 +143,7 @@ Now add a nested form for the +address+ association:
<%= form_for @person do |f| %>
<%= f.text_field :name %>
- <% f.fields_for :address do |af| %>
+ <%= f.fields_for :address do |af| %>
<%= f.text_field :street %>
<% end %>
<% end %>
@@ -184,7 +184,7 @@ The form code for an association collection is pretty similar to that of a singl
<%= form_for @person do |f| %>
<%= f.text_field :name %>
- <% f.fields_for :projects do |pf| %>
+ <%= f.fields_for :projects do |pf| %>
<%= f.text_field :name %>
<% end %>
<% end %>
View
11 railties/lib/rails/application.rb
@@ -67,6 +67,7 @@ def inherited(base)
raise "You cannot have more than one Rails::Application" if Rails.application
super
Rails.application = base.instance
+ Rails.application.add_lib_to_load_paths!
ActiveSupport.run_load_hooks(:before_configuration, base.instance)
end
@@ -83,11 +84,21 @@ def method_missing(*args, &block)
delegate :middleware, :to => :config
+ def add_lib_to_load_paths!
+ path = config.root.join('lib').to_s
+ $LOAD_PATH.unshift(path) if File.exists?(path)
+ end
+
def require_environment!
environment = paths.config.environment.to_a.first
require environment if environment
end
+ def eager_load!
+ railties.all(&:eager_load!)
+ super
+ end
+
def routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
View
2  railties/lib/rails/application/finisher.rb
@@ -38,7 +38,7 @@ module Finisher
initializer :eager_load! do
if config.cache_classes && !$rails_rake_task
ActiveSupport.run_load_hooks(:before_eager_load, self)
- railties.all(&:eager_load!)
+ eager_load!
end
end
View
1  railties/lib/rails/commands/destroy.rb
@@ -1,4 +1,5 @@
require 'rails/generators'
+Rails::Generators.configure!
if [nil, "-h", "--help"].include?(ARGV.first)
Rails::Generators.help 'destroy'
View
1  railties/lib/rails/commands/generate.rb
@@ -1,4 +1,5 @@
require 'rails/generators'
+Rails::Generators.configure!
if [nil, "-h", "--help"].include?(ARGV.first)
Rails::Generators.help 'generate'
View
8 railties/lib/rails/generators.rb
@@ -68,6 +68,7 @@ def self.configure!(config = Rails.application.config.generators) #:nodoc:
options.deep_merge! config.options
fallbacks.merge! config.fallbacks
templates_path.concat config.templates
+ templates_path.uniq!
end
def self.templates_path
@@ -328,10 +329,5 @@ def self.namespaces_to_paths(namespaces) #:nodoc:
paths.uniq!
paths
end
-
end
-end
-
-# If the application was already defined, configure generators,
-# otherwise you have to configure it by hand.
-Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application
+end
View
17 railties/test/application/initializers/load_path_test.rb
@@ -19,6 +19,23 @@ def setup
assert $:.include?("#{app_path}/app/models")
end
+ test "initializing an application adds lib path on inheritance hook" do
+ app_file "lib/foo.rb", <<-RUBY
+ module Foo; end
+ RUBY
+
+ add_to_config <<-RUBY
+ require "foo"
+ raise "Expected Foo to be defined" unless defined?(Foo)
+ RUBY
+
+ assert_nothing_raised do
+ require "#{app_path}/config/environment"
+ end
+
+ assert $:.include?("#{app_path}/lib")
+ end
+
test "initializing an application eager load any path under app" do
app_file "app/anything/foo.rb", <<-RUBY
module Foo; end
View
7 railties/test/generators/generators_test_helper.rb
@@ -1,4 +1,6 @@
require 'abstract_unit'
+require 'rails/generators'
+require 'rails/generators/test_case'
module Rails
def self.root
@@ -8,8 +10,9 @@ def self.root
Rails.application.config.root = Rails.root
Rails.application.config.generators.templates = [File.join(Rails.root, "lib", "templates")]
-require 'rails/generators'
-require 'rails/generators/test_case'
+# Call configure to load the settings from
+# Rails.application.config.generators to Rails::Generators
+Rails::Generators.configure!
require 'active_record'
require 'action_dispatch'
Please sign in to comment.
Something went wrong with that request. Please try again.