diff options
75 files changed, 910 insertions, 690 deletions
@@ -8,7 +8,7 @@ else gem 'arel' end -gem 'minitest', '~> 3.1.0' +gem 'minitest', '~> 3.2.0' gem 'mocha', '>= 0.11.2' gem 'rack-test', github: "brynary/rack-test" gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/actionmailer/lib/action_mailer/async.rb b/actionmailer/lib/action_mailer/async.rb index 86a5b0d035..a364342745 100644 --- a/actionmailer/lib/action_mailer/async.rb +++ b/actionmailer/lib/action_mailer/async.rb @@ -1,39 +1,41 @@ require 'delegate' -module ActionMailer::Async - def method_missing(method_name, *args) - if action_methods.include?(method_name.to_s) - QueuedMessage.new(queue, self, method_name, *args) - else - super +module ActionMailer + module Async + def method_missing(method_name, *args) + if action_methods.include?(method_name.to_s) + QueuedMessage.new(queue, self, method_name, *args) + else + super + end end - end - def queue - Rails.queue - end + def queue + Rails.queue + end - class QueuedMessage < ::Delegator - attr_reader :queue + class QueuedMessage < ::Delegator + attr_reader :queue - def initialize(queue, mailer_class, method_name, *args) - @queue = queue - @mailer_class = mailer_class - @method_name = method_name - @args = args - end + def initialize(queue, mailer_class, method_name, *args) + @queue = queue + @mailer_class = mailer_class + @method_name = method_name + @args = args + end - def __getobj__ - @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message - end + def __getobj__ + @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message + end - def run - __getobj__.deliver - end + def run + __getobj__.deliver + end - # Will push the message onto the Queue to be processed - def deliver - @queue << self + # Will push the message onto the Queue to be processed + def deliver + @queue << self + end end end -end +end
\ No newline at end of file diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 89e0adc04d..f31e1e007b 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -457,10 +457,11 @@ module ActionMailer #:nodoc: end # Will force ActionMailer to push new messages to the queue defined - # in the ActionMailer class when set to true + # in the ActionMailer class when set to true. # # class WelcomeMailer < ActionMailer::Base # self.async = true + # end def async=(truth) if truth require 'action_mailer/async' @@ -785,5 +786,4 @@ module ActionMailer #:nodoc: ActiveSupport.run_load_hooks(:action_mailer, self) end -end - +end
\ No newline at end of file diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index 7d73c6af8d..3da2834af0 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -49,9 +49,19 @@ module AbstractController module ClassMethods def view_context_class @view_context_class ||= begin - routes = _routes if respond_to?(:_routes) - helpers = _helpers if respond_to?(:_helpers) - ActionView::Base.prepare(routes, helpers) + routes = respond_to?(:_routes) && _routes + helpers = respond_to?(:_helpers) && _helpers + + Class.new(ActionView::Base) do + if routes + include routes.url_helpers + include routes.mounted_helpers + end + + if helpers + include helpers + end + end end end end diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 94242ad962..53a4afecb3 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1316,22 +1316,6 @@ module ActionDispatch parent_resource.instance_of?(Resource) && @scope[:shallow] end - def draw(name) - path = @draw_paths.find do |_path| - File.exists? "#{_path}/#{name}.rb" - end - - unless path - msg = "Your router tried to #draw the external file #{name}.rb,\n" \ - "but the file was not found in:\n\n" - msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n") - raise ArgumentError, msg - end - - route_path = "#{path}/#{name}.rb" - instance_eval(File.read(route_path), route_path.to_s) - end - # match 'path' => 'controller#action' # match 'path', to: 'controller#action' # match 'path', 'otherpath', on: :member, via: :get @@ -1581,7 +1565,6 @@ module ActionDispatch def initialize(set) #:nodoc: @set = set - @draw_paths = set.draw_paths @scope = { :path_names => @set.resources_path_names } end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 64b1d58ae9..1d6ca0c78d 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -237,7 +237,6 @@ module ActionDispatch attr_accessor :formatter, :set, :named_routes, :default_scope, :router attr_accessor :disable_clear_and_finalize, :resources_path_names attr_accessor :default_url_options, :request_class, :valid_conditions - attr_accessor :draw_paths alias :routes :set @@ -249,7 +248,6 @@ module ActionDispatch self.named_routes = NamedRouteCollection.new self.resources_path_names = self.class.default_resources_path_names.dup self.default_url_options = {} - self.draw_paths = [] self.request_class = request_class @valid_conditions = { :controller => true, :action => true } diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index f98648d930..7bfbc1f0aa 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -148,7 +148,6 @@ module ActionView #:nodoc: cattr_accessor :prefix_partial_path_with_controller_namespace @@prefix_partial_path_with_controller_namespace = true - class_attribute :helpers class_attribute :_routes class_attribute :logger @@ -166,22 +165,6 @@ module ActionView #:nodoc: def xss_safe? #:nodoc: true end - - # This method receives routes and helpers from the controller - # and return a subclass ready to be used as view context. - def prepare(routes, helpers) #:nodoc: - Class.new(self) do - if routes - include routes.url_helpers - include routes.mounted_helpers - end - - if helpers - include helpers - self.helpers = helpers - end - end - end end attr_accessor :view_renderer diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb index bd078d2b21..8070bdec8a 100644 --- a/actionpack/test/dispatch/mapper_test.rb +++ b/actionpack/test/dispatch/mapper_test.rb @@ -4,12 +4,11 @@ module ActionDispatch module Routing class MapperTest < ActiveSupport::TestCase class FakeSet - attr_reader :routes, :draw_paths + attr_reader :routes alias :set :routes def initialize @routes = [] - @draw_paths = [] end def resources_path_names diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index f15dd7214b..fed26d89f8 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -2324,55 +2324,6 @@ class TestNamespaceWithControllerOption < ActionDispatch::IntegrationTest end end -class TestDrawExternalFile < ActionDispatch::IntegrationTest - class ExternalController < ActionController::Base - def index - render :text => "external#index" - end - end - - DRAW_PATH = File.expand_path('../../fixtures/routes', __FILE__) - - DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new.tap do |app| - app.draw_paths << DRAW_PATH - end - - def app - DefaultScopeRoutes - end - - def test_draw_external_file - DefaultScopeRoutes.draw do - scope :module => 'test_draw_external_file' do - draw :external - end - end - - get '/external' - assert_equal "external#index", @response.body - end - - def test_draw_nonexistent_file - exception = assert_raise ArgumentError do - DefaultScopeRoutes.draw do - draw :nonexistent - end - end - assert_match 'Your router tried to #draw the external file nonexistent.rb', exception.message - assert_match DRAW_PATH.to_s, exception.message - end - - def test_draw_bogus_file - exception = assert_raise NoMethodError do - DefaultScopeRoutes.draw do - draw :bogus - end - end - assert_match "undefined method `wrong'", exception.message - assert_match 'test/fixtures/routes/bogus.rb:1', exception.backtrace.first - end -end - class TestDefaultScope < ActionDispatch::IntegrationTest module ::Blog class PostsController < ActionController::Base diff --git a/actionpack/test/fixtures/routes/bogus.rb b/actionpack/test/fixtures/routes/bogus.rb deleted file mode 100644 index 41fbf0cd64..0000000000 --- a/actionpack/test/fixtures/routes/bogus.rb +++ /dev/null @@ -1 +0,0 @@ -wrong :route diff --git a/actionpack/test/fixtures/routes/external.rb b/actionpack/test/fixtures/routes/external.rb deleted file mode 100644 index d103c39f53..0000000000 --- a/actionpack/test/fixtures/routes/external.rb +++ /dev/null @@ -1 +0,0 @@ -get '/external' => 'external#index' diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb index d7f30f0920..89d87a8b6f 100644 --- a/activemodel/lib/active_model/conversion.rb +++ b/activemodel/lib/active_model/conversion.rb @@ -18,17 +18,23 @@ module ActiveModel # end # # cm = ContactMessage.new - # cm.to_model == self # => true - # cm.to_key # => nil - # cm.to_param # => nil - # cm.to_partial_path # => "contact_messages/contact_message" - # + # cm.to_model == cm # => true + # cm.to_key # => nil + # cm.to_param # => nil + # cm.to_partial_path # => "contact_messages/contact_message" module Conversion extend ActiveSupport::Concern # If your object is already designed to implement all of the Active Model # you can use the default <tt>:to_model</tt> implementation, which simply - # returns self. + # returns +self+. + # + # class Person + # include ActiveModel::Conversion + # end + # + # person = Person.new + # person.to_model == person # => true # # If your model does not act like an Active Model object, then you should # define <tt>:to_model</tt> yourself returning a proxy object that wraps @@ -37,21 +43,28 @@ module ActiveModel self end - # Returns an Enumerable of all key attributes if any is set, regardless - # if the object is persisted or not. + # Returns an Enumerable of all key attributes if any is set, regardless if + # the object is persisted or not. If there no key attributes, returns +nil+. def to_key key = respond_to?(:id) && id key ? [key] : nil end # Returns a string representing the object's key suitable for use in URLs, - # or nil if <tt>persisted?</tt> is false. + # or +nil+ if <tt>persisted?</tt> is false. def to_param persisted? ? to_key.join('-') : nil end # Returns a string identifying the path associated with the object. # ActionPack uses this to find a suitable partial to represent the object. + # + # class Person + # include ActiveModel::Conversion + # end + # + # person = Person.new + # person.to_partial_path # => "people/person" def to_partial_path self.class._to_partial_path end diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 7014d8114f..9bd5f903cd 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -80,8 +80,8 @@ module ActiveModel # person.changes # => {"name" => ["Bill", "Bob"]} # # If an attribute is modified in-place then make use of <tt>[attribute_name]_will_change!</tt> - # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to - # in-place attributes. + # to mark that the attribute is changing. Otherwise ActiveModel can't track + # changes to in-place attributes. # # person.name_will_change! # person.name_change # => ["Bill", "Bill"] @@ -115,7 +115,7 @@ module ActiveModel end # Returns a hash of changed attributes indicating their original - # and new values like <tt>attr => [original value, new value]</tt>. + # and new values like <tt>attr => [original value, new value]</tt>. # # person.changes # => {} # person.name = 'bob' diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 567677bbb9..4ed3462e7e 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -54,8 +54,8 @@ module ActiveModel # The above allows you to do: # # p = Person.new - # p.validate! # => ["can not be nil"] - # p.errors.full_messages # => ["name can not be nil"] + # person.validate! # => ["can not be nil"] + # person.errors.full_messages # => ["name can not be nil"] # # etc.. class Errors include Enumerable @@ -81,28 +81,50 @@ module ActiveModel super end - # Clear the messages + # Clear the error messages. + # + # person.errors.full_messages #Â => ["name can not be nil"] + # person.errors.clear + # person.errors.full_messages # => [] def clear messages.clear end - # Do the error messages include an error with key +error+? + # Returns +true+ if the error messages include an error for the given key + # +attribute+, +false+ otherwise. + # + # person.errors.messages #Â => { :name => ["can not be nil"] } + # person.errors.include?(:name) # => true + # person.errors.include?(:age) #Â => false def include?(attribute) (v = messages[attribute]) && v.any? end + # aliases include? alias :has_key? :include? - # Get messages for +key+ + # Get messages for +key+. + # + # person.errors.messages #Â => { :name => ["can not be nil"] } + # person.errors.get(:name) # => ["can not be nil"] + # person.errors.get(:age) # => nil def get(key) messages[key] end - # Set messages for +key+ to +value+ + # Set messages for +key+ to +value+. + # + # person.errors.get(:name) # => ["can not be nil"] + # person.errors.set(:name, ["can't be nil"]) + # person.errors.get(:name) # => ["can't be nil"] def set(key, value) messages[key] = value end - # Delete messages for +key+ + # Delete messages for +key+. Returns the deleted messages. + # + # person.errors.get(:name) # => ["can not be nil"] + # person.errors.delete(:name) #Â => ["can not be nil"] + # person.errors.get(:name) # => nil def delete(key) messages.delete(key) end @@ -110,16 +132,16 @@ module ActiveModel # When passed a symbol or a name of a method, returns an array of errors # for the method. # - # p.errors[:name] # => ["can not be nil"] - # p.errors['name'] # => ["can not be nil"] + # person.errors[:name] # => ["can not be nil"] + # person.errors['name'] # => ["can not be nil"] def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end # Adds to the supplied attribute the supplied error message. # - # p.errors[:name] = "must be set" - # p.errors[:name] # => ['must be set'] + # person.errors[:name] = "must be set" + # person.errors[:name] # => ['must be set'] def []=(attribute, error) self[attribute] << error end @@ -128,13 +150,13 @@ module ActiveModel # Yields the attribute and the error for that attribute. If the attribute # has more than one error message, yields once for each error message. # - # p.errors.add(:name, "can't be blank") - # p.errors.each do |attribute, error| + # person.errors.add(:name, "can't be blank") + # person.errors.each do |attribute, error| # # Will yield :name and "can't be blank" # end # - # p.errors.add(:name, "must be specified") - # p.errors.each do |attribute, error| + # person.errors.add(:name, "must be specified") + # person.errors.each do |attribute, error| # # Will yield :name and "can't be blank" # # then yield :name and "must be specified" # end @@ -146,54 +168,65 @@ module ActiveModel # Returns the number of error messages. # - # p.errors.add(:name, "can't be blank") - # p.errors.size # => 1 - # p.errors.add(:name, "must be specified") - # p.errors.size # => 2 + # person.errors.add(:name, "can't be blank") + # person.errors.size # => 1 + # person.errors.add(:name, "must be specified") + # person.errors.size # => 2 def size values.flatten.size end - # Returns all message values + # Returns all message values. + # + # person.errors.messages # => { :name => ["can not be nil", "must be specified"] } + # person.errors.values # => [["can not be nil", "must be specified"]] def values messages.values end - # Returns all message keys + # Returns all message keys. + # + # person.errors.messages # => { :name => ["can not be nil", "must be specified"] } + # person.errors.keys # => [:name] def keys messages.keys end - # Returns an array of error messages, with the attribute name included + # Returns an array of error messages, with the attribute name included. # - # p.errors.add(:name, "can't be blank") - # p.errors.add(:name, "must be specified") - # p.errors.to_a # => ["name can't be blank", "name must be specified"] + # person.errors.add(:name, "can't be blank") + # person.errors.add(:name, "must be specified") + # person.errors.to_a # => ["name can't be blank", "name must be specified"] def to_a full_messages end # Returns the number of error messages. - # p.errors.add(:name, "can't be blank") - # p.errors.count # => 1 - # p.errors.add(:name, "must be specified") - # p.errors.count # => 2 + # + # person.errors.add(:name, "can't be blank") + # person.errors.count # => 1 + # person.errors.add(:name, "must be specified") + # person.errors.count # => 2 def count to_a.size end - # Returns true if no errors are found, false otherwise. + # Returns +true+ if no errors are found, +false+ otherwise. # If the error message is a string it can be empty. + # + # person.errors.full_messages #Â => ["name can not be nil"] + # person.errors.empty? # => false def empty? all? { |k, v| v && v.empty? && !v.is_a?(String) } end + # aliases empty? alias_method :blank?, :empty? # Returns an xml formatted representation of the Errors hash. # - # p.errors.add(:name, "can't be blank") - # p.errors.add(:name, "must be specified") - # p.errors.to_xml + # person.errors.add(:name, "can't be blank") + # person.errors.add(:name, "must be specified") + # person.errors.to_xml # # => # # <?xml version=\"1.0\" encoding=\"UTF-8\"?> # # <errors> @@ -204,14 +237,21 @@ module ActiveModel to_a.to_xml({ :root => "errors", :skip_types => true }.merge!(options)) end - # Returns an Hash that can be used as the JSON representation for this object. - # Options: - # * <tt>:full_messages</tt> - determines if json object should contain - # full messages or not. Default: <tt>false</tt>. + # Returns a Hash that can be used as the JSON representation for this + # object. You can pass the <tt>:full_messages</tt> option. This determines + # if the json object should contain full messages or not (false by default). + # + # person.as_json # => { :name => ["can not be nil"] } + # person.as_json(full_messages: true) # => { :name => ["name can not be nil"] } def as_json(options=nil) to_hash(options && options[:full_messages]) end + #Â Returns a Hash of attributes with their error messages. If +full_messages+ + # is +true+, it will contain full messages (see +full_message+). + # + # person.to_hash # => { :name => ["can not be nil"] } + # person.to_hash(true) # => { :name => ["name can not be nil"] } def to_hash(full_messages = false) if full_messages messages = {} @@ -224,12 +264,31 @@ module ActiveModel end end - # Adds +message+ to the error messages on +attribute+. More than one error can be added to the same - # +attribute+. - # If no +message+ is supplied, <tt>:invalid</tt> is assumed. + # Adds +message+ to the error messages on +attribute+. More than one error + # can be added to the same +attribute+. If no +message+ is supplied, + # <tt>:invalid</tt> is assumed. # - # If +message+ is a symbol, it will be translated using the appropriate scope (see +generate_message+). - # If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error. + # person.errors.add(:name) + # # => ["is invalid"] + # person.errors.add(:name, 'must be implemented') + # # => ["is invalid", "must be implemented"] + # + # person.errors.messages + # # => { :name => ["must be implemented", "is invalid"] } + # + # If +message+ is a symbol, it will be translated using the appropriate + # scope (see +generate_message+). + # + # If +message+ is a proc, it will be called, allowing for things like + # <tt>Time.now</tt> to be used within an error. + # + # If the <tt>:strict</tt> option is set to true will raise + # ActiveModel::StrictValidationFailed instead of adding the error. + # + # person.errors.add(:name, nil, strict: true) + # #Â => ActiveModel::StrictValidationFailed: name is invalid + # + # person.errors.messages # => {} def add(attribute, message = nil, options = {}) message = normalize_message(attribute, message, options) if options[:strict] @@ -239,7 +298,12 @@ module ActiveModel self[attribute] << message end - # Will add an error message to each of the attributes in +attributes+ that is empty. + # Will add an error message to each of the attributes in +attributes+ + # that is empty. + # + # person.errors.add_on_empty(:name) + # person.errors.messages + # #Â => { :name => ["can't be empty"] } def add_on_empty(attributes, options = {}) [attributes].flatten.each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) @@ -248,7 +312,12 @@ module ActiveModel end end - # Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?). + # Will add an error message to each of the attributes in +attributes+ that + # is blank (using Object#blank?). + # + # person.errors.add_on_blank(:name) + # person.errors.messages + # # => { :name => ["can't be blank"] } def add_on_blank(attributes, options = {}) [attributes].flatten.each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) @@ -256,10 +325,11 @@ module ActiveModel end end - # Returns true if an error on the attribute with the given message is present, false otherwise. - # +message+ is treated the same as for +add+. - # p.errors.add :name, :blank - # p.errors.added? :name, :blank # => true + # Returns +true+ if an error on the attribute with the given message is + # present, +false+ otherwise. +message+ is treated the same as for +add+. + # + # person.errors.add :name, :blank + # person.errors.added? :name, :blank # => true def added?(attribute, message = nil, options = {}) message = normalize_message(attribute, message, options) self[attribute].include? message @@ -267,22 +337,21 @@ module ActiveModel # Returns all the full error messages in an array. # - # class Company + # class Person # validates_presence_of :name, :address, :email - # validates_length_of :name, :in => 5..30 + # validates_length_of :name, in: 5..30 # end # - # company = Company.create(:address => '123 First St.') - # company.errors.full_messages # => - # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"] + # person = Person.create(address: '123 First St.') + # person.errors.full_messages + # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"] def full_messages map { |attribute, message| full_message(attribute, message) } end # Returns a full message for a given attribute. # - # company.errors.full_message(:name, "is invalid") # => - # "Name is invalid" + # person.errors.full_message(:name, 'is invalid') # => "Name is invalid" def full_message(attribute, message) return message if attribute == :base attr_name = attribute.to_s.tr('.', '_').humanize @@ -298,10 +367,11 @@ module ActiveModel # (<tt>activemodel.errors.messages</tt>). # # Error messages are first looked up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>, - # if it's not there, it's looked up in <tt>models.MODEL.MESSAGE</tt> and if that is not - # there also, it returns the translation of the default message - # (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model name, - # translated attribute name and the value are available for interpolation. + # if it's not there, it's looked up in <tt>models.MODEL.MESSAGE</tt> and if + # that is not there also, it returns the translation of the default message + # (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model + # name, translated attribute name and the value are available for + # interpolation. # # When using inheritance in your models, it will check all the inherited # models too, but only if the model itself hasn't been found. Say you have @@ -317,7 +387,6 @@ module ActiveModel # * <tt>activemodel.errors.messages.blank</tt> # * <tt>errors.attributes.title.blank</tt> # * <tt>errors.messages.blank</tt> - # def generate_message(attribute, type = :invalid, options = {}) type = options.delete(:message) if options[:message].is_a?(Symbol) @@ -366,6 +435,8 @@ module ActiveModel end end + # Raised when a validation cannot be corrected by end users and are considered + # exceptional. class StrictValidationFailed < StandardError end end diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index 88b730626c..550fa474ea 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -12,19 +12,20 @@ module ActiveModel # you want all features out of the box. # # These tests do not attempt to determine the semantic correctness of the - # returned values. For instance, you could implement valid? to always - # return true, and the tests would pass. It is up to you to ensure that - # the values are semantically meaningful. + # returned values. For instance, you could implement <tt>valid?</tt> to + # always return true, and the tests would pass. It is up to you to ensure + # that the values are semantically meaningful. # - # Objects you pass in are expected to return a compliant object from a - # call to to_model. It is perfectly fine for to_model to return self. + # Objects you pass in are expected to return a compliant object from a call + # to <tt>to_model</tt>. It is perfectly fine for <tt>to_model</tt> to return + # +self+. module Tests # == Responds to <tt>to_key</tt> # # Returns an Enumerable of all (primary) key attributes - # or nil if model.persisted? is false. This is used by - # dom_id to generate unique ids for the object. + # or nil if <tt>model.persisted?</tt> is false. This is used by + # <tt>dom_id</tt> to generate unique ids for the object. def test_to_key assert model.respond_to?(:to_key), "The model should respond to to_key" def model.persisted?() false end @@ -34,13 +35,14 @@ module ActiveModel # == Responds to <tt>to_param</tt> # # Returns a string representing the object's key suitable for use in URLs - # or nil if model.persisted? is false. + # or +nil+ if <tt>model.persisted?</tt> is +false+. # - # Implementers can decide to either raise an exception or provide a default - # in case the record uses a composite primary key. There are no tests for this - # behavior in lint because it doesn't make sense to force any of the possible - # implementation strategies on the implementer. However, if the resource is - # not persisted?, then to_param should always return nil. + # Implementers can decide to either raise an exception or provide a + # default in case the record uses a composite primary key. There are no + # tests for this behavior in lint because it doesn't make sense to force + # any of the possible implementation strategies on the implementer. + # However, if the resource is not persisted?, then <tt>to_param</tt> + # should always return +nil+. def test_to_param assert model.respond_to?(:to_param), "The model should respond to to_param" def model.to_key() [1] end @@ -50,9 +52,8 @@ module ActiveModel # == Responds to <tt>to_partial_path</tt> # - # Returns a string giving a relative path. This is used for looking up + # Returns a string giving a relative path. This is used for looking up # partials. For example, a BlogPost model might return "blog_posts/blog_post" - # def test_to_partial_path assert model.respond_to?(:to_partial_path), "The model should respond to to_partial_path" assert_kind_of String, model.to_partial_path @@ -60,11 +61,11 @@ module ActiveModel # == Responds to <tt>persisted?</tt> # - # Returns a boolean that specifies whether the object has been persisted yet. - # This is used when calculating the URL for an object. If the object is - # not persisted, a form for that object, for instance, will route to the - # create action. If it is persisted, a form for the object will routes to - # the update action. + # Returns a boolean that specifies whether the object has been persisted + # yet. This is used when calculating the URL for an object. If the object + # is not persisted, a form for that object, for instance, will route to + # the create action. If it is persisted, a form for the object will routes + # to the update action. def test_persisted? assert model.respond_to?(:persisted?), "The model should respond to persisted?" assert_boolean model.persisted?, "persisted?" @@ -73,8 +74,8 @@ module ActiveModel # == Naming # # Model.model_name must return a string with some convenience methods: - # :human, :singular, and :plural. Check ActiveModel::Naming for more information. - # + # <tt>:human</tt>, <tt>:singular</tt> and <tt>:plural</tt>. Check + # ActiveModel::Naming for more information. def test_model_naming assert model.class.respond_to?(:model_name), "The model should respond to model_name" model_name = model.class.model_name diff --git a/activemodel/lib/active_model/model.rb b/activemodel/lib/active_model/model.rb index cb0a051ca5..33a530e6bd 100644 --- a/activemodel/lib/active_model/model.rb +++ b/activemodel/lib/active_model/model.rb @@ -2,11 +2,11 @@ module ActiveModel # == Active Model Basic Model # - # Includes the required interface for an object to interact with <tt>ActionPack</tt>, - # using different <tt>ActiveModel</tt> modules. It includes model name introspections, - # conversions, translations and validations. Besides that, it allows you to - # initialize the object with a hash of attributes, pretty much like - # <tt>ActiveRecord</tt> does. + # Includes the required interface for an object to interact with + # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules. + # It includes model name introspections, conversions, translations and + # validations. Besides that, it allows you to initialize the object with a + # hash of attributes, pretty much like <tt>ActiveRecord</tt> does. # # A minimal implementation could be: # @@ -15,13 +15,13 @@ module ActiveModel # attr_accessor :name, :age # end # - # person = Person.new(:name => 'bob', :age => '18') + # person = Person.new(name: 'bob', age: '18') # person.name # => 'bob' - # person.age # => 18 + # person.age # => 18 # - # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt> to - # return <tt>false</tt>, which is the most common case. You may want to override it - # in your class to simulate a different scenario: + # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt> + # to return +false+, which is the most common case. You may want to override + # it in your class to simulate a different scenario: # # class Person # include ActiveModel::Model @@ -32,11 +32,12 @@ module ActiveModel # end # end # - # person = Person.new(:id => 1, :name => 'bob') + # person = Person.new(id: 1, name: 'bob') # person.persisted? # => true # - # Also, if for some reason you need to run code on <tt>initialize</tt>, make sure you - # call super if you want the attributes hash initialization to happen. + # Also, if for some reason you need to run code on <tt>initialize</tt>, make + # sure you call +super+ if you want the attributes hash initialization to + # happen. # # class Person # include ActiveModel::Model @@ -48,11 +49,12 @@ module ActiveModel # end # end # - # person = Person.new(:id => 1, :name => 'bob') + # person = Person.new(id: 1, name: 'bob') # person.omg # => true # - # For more detailed information on other functionalities available, please refer - # to the specific modules included in <tt>ActiveModel::Model</tt> (see below). + # For more detailed information on other functionalities available, please + # refer to the specific modules included in <tt>ActiveModel::Model</tt> + # (see below). module Model def self.included(base) #:nodoc: base.class_eval do @@ -63,12 +65,31 @@ module ActiveModel end end + # Initializes a new model with the given +params+. + # + # class Person + # include ActiveModel::Model + # attr_accessor :name, :age + # end + # + # person = Person.new(name: 'bob', age: '18') + # person.name # => "bob" + # person.age # => 18 def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params end + # Indicates if the model is persisted. Default is +false+. + # + # class Person + # include ActiveModel::Model + # attr_accessor :id, :name + # end + # + # person = Person.new(id: 1, name: 'bob') + # person.persisted? # => false def persisted? false end diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 2b5fc57a3a..ea4f9341c6 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -14,9 +14,137 @@ module ActiveModel alias_method :cache_key, :collection + ## + # :method: == + # + # :call-seq: + # ==(other) + # + # Equivalent to <tt>String#==</tt>. Returns +true+ if the class name and + # +other+ are equal, otherwise +false+. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name == 'BlogPost' # => true + # BlogPost.model_name == 'Blog Post' # => false + + ## + # :method: === + # + # :call-seq: + # ===(other) + # + # Equivalent to <tt>#==</tt>. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name === 'BlogPost' # => true + # BlogPost.model_name === 'Blog Post' # => false + + ## + # :method: <=> + # + # :call-seq: + # ==(other) + # + # Equivalent to <tt>String#<=></tt>. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name <=> 'BlogPost' # => 0 + # BlogPost.model_name <=> 'Blog' #Â => 1 + # BlogPost.model_name <=> 'BlogPosts' #Â => -1 + + ## + # :method: =~ + # + # :call-seq: + # =~(regexp) + # + # Equivalent to <tt>String#=~</tt>. Match the class name against the given + # regexp. Returns the position where the match starts or +nil+ if there is + # no match. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name =~ /Post/ # => 4 + # BlogPost.model_name =~ /\d/ # => nil + + ## + # :method: !~ + # + # :call-seq: + # !~(regexp) + # + # Equivalent to <tt>String#!~</tt>. Match the class name against the given + # regexp. Returns +true+ if there is no match, otherwise +false+. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name !~ /Post/ # => false + # BlogPost.model_name !~ /\d/ # => true + + ## + # :method: eql? + # + # :call-seq: + # eql?(other) + # + # Equivalent to <tt>String#eql?</tt>. Returns +true+ if the class name and + # +other+ have the same length and content, otherwise +false+. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name.eql?('BlogPost') # => true + # BlogPost.model_name.eql?('Blog Post') # => false + + ## + # :method: to_s + # + # :call-seq: + # to_s() + # + # Returns the class name. + # + # class BlogPost + # extend ActiveModel::Naming + # end + # + # BlogPost.model_name.to_s # => "BlogPost" + + ## + # :method: to_str + # + # :call-seq: + # to_str() + # + # Equivalent to +to_s+. delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s, :to_str, :to => :name + # Returns a new ActiveModel::Name instance. By default, the +namespace+ + # and +name+ option will take the namespace and name of the given class + # respectively. + # + # module Foo + # class Bar + # end + # end + # + # ActiveModel::Name.new(Foo::Bar).to_s + # # => "Foo::Bar" def initialize(klass, namespace = nil, name = nil) @name = name || klass.name @@ -38,7 +166,11 @@ module ActiveModel end # Transform the model name into a more humane format, using I18n. By default, - # it will underscore then humanize the class name + # it will underscore then humanize the class name. + # + # class BlogPost + # extend ActiveModel::Naming + # end # # BlogPost.model_name.human # => "Blog post" # @@ -82,11 +214,12 @@ module ActiveModel # BookModule::BookCover.model_name.i18n_key # => :"book_module/book_cover" # # Providing the functionality that ActiveModel::Naming provides in your object - # is required to pass the Active Model Lint test. So either extending the provided - # method below, or rolling your own is required. + # is required to pass the Active Model Lint test. So either extending the + # provided method below, or rolling your own is required. module Naming # Returns an ActiveModel::Name object for module. It can be - # used to retrieve all kinds of naming-related information. + # used to retrieve all kinds of naming-related information + # (See ActiveModel::Name for more information). def model_name @_model_name ||= begin namespace = self.parents.detect do |n| @@ -96,7 +229,7 @@ module ActiveModel end end - # Returns the plural class name of a record or class. Examples: + # Returns the plural class name of a record or class. # # ActiveModel::Naming.plural(post) # => "posts" # ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people" @@ -104,7 +237,7 @@ module ActiveModel model_name_from_record_or_class(record_or_class).plural end - # Returns the singular class name of a record or class. Examples: + # Returns the singular class name of a record or class. # # ActiveModel::Naming.singular(post) # => "post" # ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person" @@ -112,10 +245,10 @@ module ActiveModel model_name_from_record_or_class(record_or_class).singular end - # Identifies whether the class name of a record or class is uncountable. Examples: + # Identifies whether the class name of a record or class is uncountable. # # ActiveModel::Naming.uncountable?(Sheep) # => true - # ActiveModel::Naming.uncountable?(Post) => false + # ActiveModel::Naming.uncountable?(Post) # => false def self.uncountable?(record_or_class) plural(record_or_class) == singular(record_or_class) end diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 1822ba8e0e..aeb3773c99 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,24 @@ ## Rails 4.0.0 (unreleased) ## +* `ActiveRelation#inspect` no longer calls `#to_a` + + *Brian Cardarella* + +* Add `collate` and `ctype` support to PostgreSQL. These are available for PostgreSQL 8.4 or later. + Example: + + development: + adapter: postgresql + host: localhost + database: rails_development + username: foo + password: bar + encoding: UTF8 + collate: ja_JP.UTF8 + ctype: ja_JP.UTF8 + + *kennyj* + * `FinderMethods#exists?` now returns `false` with the `false` argument. *Egor Lynko* diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 68f8bbeb1c..a62fce4756 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1167,6 +1167,8 @@ module ActiveRecord # If true, always save the associated objects or destroy them if marked for destruction, # when saving the parent object. If false, never save or destroy the associated objects. # By default, only save associated objects that are new records. + # + # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>. # [:inverse_of] # Specifies the name of the <tt>belongs_to</tt> association on the associated object # that is the inverse of this <tt>has_many</tt> association. Does not work in combination @@ -1288,6 +1290,8 @@ module ActiveRecord # If true, always save the associated object or destroy it if marked for destruction, # when saving the parent object. If false, never save or destroy the associated object. # By default, only save the associated object if it's a new record. + # + # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>. # [:inverse_of] # Specifies the name of the <tt>belongs_to</tt> association on the associated object # that is the inverse of this <tt>has_one</tt> association. Does not work in combination @@ -1404,6 +1408,8 @@ module ActiveRecord # saving the parent object. # If false, never save or destroy the associated object. # By default, only save the associated object if it's a new record. + # + # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>. # [:touch] # If true, the associated object will be touched (the updated_at/on attributes set to now) # when this record is either saved or destroyed. If you specify a symbol, that attribute @@ -1589,6 +1595,8 @@ module ActiveRecord # If false, never save or destroy the associated objects. # By default, only save associated objects that are new records. # + # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>. + # # Option examples: # has_and_belongs_to_many :projects # has_and_belongs_to_many :projects, :include => [ :milestones, :manager ] diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb index ab6d253ef8..5b41f72e52 100644 --- a/activerecord/lib/active_record/attribute_assignment.rb +++ b/activerecord/lib/active_record/attribute_assignment.rb @@ -158,11 +158,12 @@ module ActiveRecord begin send(name + "=", read_value_from_parameter(name, values_with_empty_parameters)) rescue => ex - errors << AttributeAssignmentError.new("error on assignment #{values_with_empty_parameters.values.inspect} to #{name}", ex, name) + errors << AttributeAssignmentError.new("error on assignment #{values_with_empty_parameters.values.inspect} to #{name} (#{ex.message})", ex, name) end end unless errors.empty? - raise MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes" + error_descriptions = errors.map { |ex| ex.message }.join(",") + raise MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes [#{error_descriptions}]" end end @@ -180,15 +181,27 @@ module ActiveRecord end def read_time_parameter_value(name, values_hash_from_param) - # If Date bits were not provided, error - raise "Missing Parameter" if [1,2,3].any?{|position| !values_hash_from_param.has_key?(position)} - max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param, 6) - # If Date bits were provided but blank, then return nil - return nil if (1..3).any? {|position| values_hash_from_param[position].blank?} + # If column is a :time (and not :date or :timestamp) there is no need to validate if + # there are year/month/day fields + if column_for_attribute(name).type == :time + # if the column is a time set the values to their defaults as January 1, 1970, but only if they're nil + {1 => 1970, 2 => 1, 3 => 1}.each do |key,value| + values_hash_from_param[key] ||= value + end + else + # else column is a timestamp, so if Date bits were not provided, error + if missing_parameter = [1,2,3].detect{ |position| !values_hash_from_param.has_key?(position) } + raise ArgumentError.new("Missing Parameter - #{name}(#{missing_parameter}i)") + end + + # If Date bits were provided but blank, then return nil + return nil if (1..3).any? { |position| values_hash_from_param[position].blank? } + end - set_values = (1..max_position).collect{|position| values_hash_from_param[position] } + max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param, 6) + set_values = (1..max_position).collect{ |position| values_hash_from_param[position] } # If Time bits are not there, then default to 0 - (3..5).each {|i| set_values[i] = set_values[i].blank? ? 0 : set_values[i]} + (3..5).each { |i| set_values[i] = set_values[i].blank? ? 0 : set_values[i] } instantiate_time_object(name, set_values) end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 921278d145..df4a9d5afc 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -72,6 +72,8 @@ module ActiveRecord when /^mediumint/i; 3 when /^smallint/i; 2 when /^tinyint/i; 1 + when /^enum\((.+)\)/i + $1.split(',').map{|enum| enum.strip.length - 2}.max else super end diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb index 8491d42b86..dd40351a38 100644 --- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb @@ -1,3 +1,5 @@ +require 'uri' + module ActiveRecord module ConnectionAdapters class ConnectionSpecification #:nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index a9940209fa..507e937c3e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -916,7 +916,8 @@ module ActiveRecord end # Create a new PostgreSQL database. Options include <tt>:owner</tt>, <tt>:template</tt>, - # <tt>:encoding</tt>, <tt>:tablespace</tt>, and <tt>:connection_limit</tt> (note that MySQL uses + # <tt>:encoding</tt>, <tt>:collate</tt>, <tt>:ctype</tt>, + # <tt>:tablespace</tt>, and <tt>:connection_limit</tt> (note that MySQL uses # <tt>:charset</tt> while PostgreSQL uses <tt>:encoding</tt>). # # Example: @@ -933,6 +934,10 @@ module ActiveRecord " TEMPLATE = \"#{value}\"" when :encoding " ENCODING = '#{value}'" + when :collate + " LC_COLLATE = '#{value}'" + when :ctype + " LC_CTYPE = '#{value}'" when :tablespace " TABLESPACE = \"#{value}\"" when :connection_limit @@ -1059,6 +1064,20 @@ module ActiveRecord end_sql end + # Returns the current database collate. + def collate + query(<<-end_sql, 'SCHEMA')[0][0] + SELECT pg_database.datcollate FROM pg_database WHERE pg_database.datname LIKE '#{current_database}' + end_sql + end + + # Returns the current database ctype. + def ctype + query(<<-end_sql, 'SCHEMA')[0][0] + SELECT pg_database.datctype FROM pg_database WHERE pg_database.datname LIKE '#{current_database}' + end_sql + end + # Returns an array of schema names. def schema_names query(<<-SQL, 'SCHEMA').flatten diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index 858b667e22..5f157fde6d 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -106,13 +106,11 @@ module ActiveRecord attr_reader :record, :attempted_action def initialize(record, attempted_action) + super("Attempted to #{attempted_action} a stale object: #{record.class.name}") @record = record @attempted_action = attempted_action end - def message - "Attempted to #{attempted_action} a stale object: #{record.class.name}" - end end # Raised when association is being configured improperly or @@ -168,9 +166,9 @@ module ActiveRecord class AttributeAssignmentError < ActiveRecordError attr_reader :exception, :attribute def initialize(message, exception, attribute) + super(message) @exception = exception @attribute = attribute - @message = message end end @@ -189,12 +187,10 @@ module ActiveRecord attr_reader :model def initialize(model) + super("Unknown primary key for table #{model.table_name} in model #{model}.") @model = model end - def message - "Unknown primary key for table #{model.table_name} in model #{model}." - end end class ImmutableRelation < ActiveRecordError diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index c6ec946b73..78ecb1cdc5 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -148,13 +148,10 @@ db_namespace = namespace :db do # desc "Retrieves the collation for the current environment's database" task :collation => [:environment, :load_config] do - config = ActiveRecord::Base.configurations[Rails.env || 'development'] - case config['adapter'] - when /mysql/ - ActiveRecord::Base.establish_connection(config) - puts ActiveRecord::Base.connection.collation - else - $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' + begin + puts ActiveRecord::Tasks::DatabaseTasks.collation_current + rescue NoMethodError + $stderr.puts 'Sorry, your database adapter is not supported yet, feel free to submit a patch' end end @@ -274,7 +271,7 @@ db_namespace = namespace :db do ENV['ISC_USER'] = config['username'].to_s if config['username'] ENV['ISC_PASSWORD'] = config['password'].to_s if config['password'] end - + def firebird_db_string(config) FireRuby::Database.db_string_for(config.symbolize_keys) end @@ -435,7 +432,7 @@ namespace :railties do task :migrations => :'db:load_config' do to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip } railties = {} - Rails.application.railties.all do |railtie| + Rails.application.railties.each do |railtie| next unless to_load == :all || to_load.include?(railtie.railtie_name) if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index fe3aa00a74..e268d451e0 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -492,10 +492,6 @@ module ActiveRecord end end - def inspect - to_a.inspect - end - def pretty_print(q) q.pp(self.to_a) end diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index e6a67b76fe..974cd326ef 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -146,8 +146,8 @@ module ActiveRecord to_a end - # Returns true if a record exists in the table that matches the +id+ or - # conditions given, or false otherwise. The argument can take five forms: + # Returns +true+ if a record exists in the table that matches the +id+ or + # conditions given, or +false+ otherwise. The argument can take six forms: # # * Integer - Finds the record with this primary key. # * String - Finds the record with a primary key corresponding to this @@ -155,8 +155,9 @@ module ActiveRecord # * Array - Finds the record that matches these +find+-style conditions # (such as <tt>['color = ?', 'red']</tt>). # * Hash - Finds the record that matches these +find+-style conditions - # (such as <tt>{:color => 'red'}</tt>). - # * No args - Returns false if the table is empty, true otherwise. + # (such as <tt>{color: 'red'}</tt>). + # * +false+ - Returns always +false+. + # * No args - Returns +false+ if the table is empty, +true+ otherwise. # # For more information about specifying conditions as a Hash or Array, # see the Conditions section in the introduction to ActiveRecord::Base. @@ -168,7 +169,8 @@ module ActiveRecord # Person.exists?(5) # Person.exists?('5') # Person.exists?(['name LIKE ?', "%#{query}%"]) - # Person.exists?(:name => "David") + # Person.exists?(name: 'David') + # Person.exists?(false) # Person.exists? def exists?(conditions = :none) conditions = conditions.id if ActiveRecord::Model === conditions diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index d61df216bc..6f49548aab 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -415,10 +415,10 @@ module ActiveRecord # # Can accept other relation objects. For example: # - # Topic.select('title').from(Topics.approved) + # Topic.select('title').from(Topic.approved) # # => SELECT title FROM (SELECT * FROM topics WHERE approved = 't') subquery # - # Topics.select('a.title').from(Topics.approved, :a) + # Topic.select('a.title').from(Topic.approved, :a) # # => SELECT a.title FROM (SELECT * FROM topics WHERE approved = 't') a # def from(value, subquery_name = nil) diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 999b2ebc85..f1241502f5 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -56,6 +56,15 @@ module ActiveRecord class_for_adapter(configuration['adapter']).new(*arguments).charset end + def collation_current(environment = Rails.env) + collation ActiveRecord::Base.configurations[environment] + end + + def collation(*arguments) + configuration = arguments.first + class_for_adapter(configuration['adapter']).new(*arguments).collation + end + def purge(configuration) class_for_adapter(configuration['adapter']).new(configuration).purge end diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index b39cd2282f..bf62dfd5b5 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -44,6 +44,10 @@ module ActiveRecord connection.charset end + def collation + connection.collation + end + def structure_dump(filename) establish_connection configuration File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump } diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index a210392e53..4139460273 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -29,6 +29,10 @@ module ActiveRecord connection.encoding end + def collation + connection.collate + end + def purge clear_active_connections! drop diff --git a/activerecord/test/cases/adapters/mysql/enum_test.rb b/activerecord/test/cases/adapters/mysql/enum_test.rb new file mode 100644 index 0000000000..40af317ad1 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql/enum_test.rb @@ -0,0 +1,10 @@ +require "cases/helper" + +class MysqlEnumTest < ActiveRecord::TestCase + class EnumTest < ActiveRecord::Base + end + + def test_enum_limit + assert_equal 5, EnumTest.columns.first.limit + end +end diff --git a/activerecord/test/cases/adapters/mysql2/enum_test.rb b/activerecord/test/cases/adapters/mysql2/enum_test.rb new file mode 100644 index 0000000000..f3a05e48ad --- /dev/null +++ b/activerecord/test/cases/adapters/mysql2/enum_test.rb @@ -0,0 +1,10 @@ +require "cases/helper" + +class Mysql2EnumTest < ActiveRecord::TestCase + class EnumTest < ActiveRecord::Base + end + + def test_enum_limit + assert_equal 5, EnumTest.columns.first.limit + end +end diff --git a/activerecord/test/cases/adapters/postgresql/active_schema_test.rb b/activerecord/test/cases/adapters/postgresql/active_schema_test.rb index 447d729e52..148584214e 100644 --- a/activerecord/test/cases/adapters/postgresql/active_schema_test.rb +++ b/activerecord/test/cases/adapters/postgresql/active_schema_test.rb @@ -21,6 +21,10 @@ class PostgresqlActiveSchemaTest < ActiveRecord::TestCase assert_equal %(CREATE DATABASE "aimonetti" ENCODING = 'latin1'), create_database(:aimonetti, :encoding => :latin1) end + def test_create_database_with_collate_and_ctype + assert_equal %(CREATE DATABASE "aimonetti" ENCODING = 'UTF8' LC_COLLATE = 'ja_JP.UTF8' LC_CTYPE = 'ja_JP.UTF8'), create_database(:aimonetti, :encoding => :"UTF8", :collate => :"ja_JP.UTF8", :ctype => :"ja_JP.UTF8") + end + def test_add_index # add_index calls index_name_exists? which can't work since execute is stubbed ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:define_method, :index_name_exists?) do |*| diff --git a/activerecord/test/cases/adapters/postgresql/connection_test.rb b/activerecord/test/cases/adapters/postgresql/connection_test.rb index f6e168caf2..9b5c5eb72f 100644 --- a/activerecord/test/cases/adapters/postgresql/connection_test.rb +++ b/activerecord/test/cases/adapters/postgresql/connection_test.rb @@ -21,6 +21,14 @@ module ActiveRecord assert_not_nil @connection.encoding end + def test_collate + assert_not_nil @connection.collate + end + + def test_ctype + assert_not_nil @connection.ctype + end + def test_default_client_min_messages assert_equal "warning", @connection.client_min_messages end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index b8481175b2..3ea6201d60 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -193,7 +193,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_no_sql_should_be_fired_if_association_already_loaded Car.create(:name => 'honda') bulbs = Car.first.bulbs - bulbs.inspect # to load all instances of bulbs + bulbs.to_a # to load all instances of bulbs assert_no_queries do bulbs.first() diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 1093fedea1..fe385feb4a 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -792,6 +792,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase end private + def cached_columns Topic.columns.find_all { |column| !Topic.serialized_attributes.include? column.name diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 232a542158..e34f505a02 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -872,24 +872,39 @@ class BasicsTest < ActiveRecord::TestCase assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on end - def test_multiparameter_assignment_of_aggregation_with_missing_values - ex = assert_raise(ActiveRecord::MultiparameterAssignmentErrors) do - customer = Customer.new - address = Address.new("The Street", "The City", "The Country") - attributes = { "address(2)" => address.city, "address(3)" => address.country } - customer.attributes = attributes - end - assert_equal("address", ex.errors[0].attribute) - end - - def test_multiparameter_assignment_of_aggregation_with_large_index - ex = assert_raise(ActiveRecord::MultiparameterAssignmentErrors) do - customer = Customer.new - address = Address.new("The Street", "The City", "The Country") - attributes = { "address(1)" => "The Street", "address(2)" => address.city, "address(3000)" => address.country } - customer.attributes = attributes + def test_multiparameter_attributes_setting_time_attribute + return skip "Oracle does not have TIME data type" if current_adapter? :OracleAdapter + + topic = Topic.new( "bonus_time(4i)"=> "01", "bonus_time(5i)" => "05" ) + assert_equal 1, topic.bonus_time.hour + assert_equal 5, topic.bonus_time.min + end + + def test_multiparameter_attributes_setting_date_attribute + topic = Topic.new( "written_on(1i)" => "1952", "written_on(2i)" => "3", "written_on(3i)" => "11" ) + assert_equal 1952, topic.written_on.year + assert_equal 3, topic.written_on.month + assert_equal 11, topic.written_on.day + end + + def test_multiparameter_attributes_setting_date_and_time_attribute + topic = Topic.new( + "written_on(1i)" => "1952", + "written_on(2i)" => "3", + "written_on(3i)" => "11", + "written_on(4i)" => "13", + "written_on(5i)" => "55") + assert_equal 1952, topic.written_on.year + assert_equal 3, topic.written_on.month + assert_equal 11, topic.written_on.day + assert_equal 13, topic.written_on.hour + assert_equal 55, topic.written_on.min + end + + def test_multiparameter_attributes_setting_time_but_not_date_on_date_field + assert_raise( ActiveRecord::MultiparameterAssignmentErrors ) do + Topic.new( "written_on(4i)" => "13", "written_on(5i)" => "55" ) end - assert_equal("address", ex.errors[0].attribute) end def test_attributes_on_dummy_time diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index 5f36b2c841..8c96a8aaa1 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -251,6 +251,17 @@ module ActiveRecord end end + class DatabaseTasksCollationTest < ActiveRecord::TestCase + include DatabaseTasksSetupper + + ADAPTERS_TASKS.each do |k, v| + define_method("test_#{k}_collation") do + eval("@#{v}").expects(:collation) + ActiveRecord::Tasks::DatabaseTasks.collation 'adapter' => k + end + end + end + class DatabaseTasksStructureDumpTest < ActiveRecord::TestCase include DatabaseTasksSetupper diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index 42a11b0171..9a0eb423bd 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -195,6 +195,24 @@ module ActiveRecord end end + class MysqlDBCollationTest < ActiveRecord::TestCase + def setup + @connection = stub(:create_database => true) + @configuration = { + 'adapter' => 'mysql', + 'database' => 'my-app-db' + } + + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_collation + @connection.expects(:collation) + ActiveRecord::Tasks::DatabaseTasks.collation @configuration + end + end + class MySQLStructureDumpTest < ActiveRecord::TestCase def setup @connection = stub(:structure_dump => true) diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index e8769bd4df..df1075d816 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -38,6 +38,14 @@ module ActiveRecord merge('encoding' => 'latin') end + def test_creates_database_with_given_collate_and_ctype + @connection.expects(:create_database). + with('my-app-db', @configuration.merge('encoding' => 'utf8', 'collate' => 'ja_JP.UTF8', 'ctype' => 'ja_JP.UTF8')) + + ActiveRecord::Tasks::DatabaseTasks.create @configuration. + merge('collate' => 'ja_JP.UTF8', 'ctype' => 'ja_JP.UTF8') + end + def test_establishes_connection_to_new_database ActiveRecord::Base.expects(:establish_connection).with(@configuration) @@ -151,6 +159,24 @@ module ActiveRecord end end + class PostgreSQLDBCollationTest < ActiveRecord::TestCase + def setup + @connection = stub(:create_database => true) + @configuration = { + 'adapter' => 'postgresql', + 'database' => 'my-app-db' + } + + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_collation + @connection.expects(:collate) + ActiveRecord::Tasks::DatabaseTasks.collation @configuration + end + end + class PostgreSQLStructureDumpTest < ActiveRecord::TestCase def setup @connection = stub(:structure_dump => true) diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb index b5557fc953..06a1d0ffc2 100644 --- a/activerecord/test/cases/tasks/sqlite_rake_test.rb +++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb @@ -124,6 +124,27 @@ module ActiveRecord end end + class SqliteDBCollationTest < ActiveRecord::TestCase + def setup + @database = 'db_create.sqlite3' + @connection = stub :connection + @configuration = { + 'adapter' => 'sqlite3', + 'database' => @database + } + + File.stubs(:exist?).returns(false) + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_collation + assert_raise NoMethodError do + ActiveRecord::Tasks::DatabaseTasks.collation @configuration, '/rails/root' + end + end + end + class SqliteStructureDumpTest < ActiveRecord::TestCase def setup @database = "db_create.sqlite3" diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 9fefa9ad3a..43cde4ab73 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -64,7 +64,6 @@ class AuditLog < ActiveRecord::Base belongs_to :unvalidated_developer, :class_name => 'Developer' end -DeveloperSalary = Struct.new(:amount) class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base self.table_name = 'developers' has_and_belongs_to_many :projects, :join_table => 'developers_projects', :foreign_key => 'developer_id' diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index 65b6f9f227..24a43d7ece 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -32,4 +32,13 @@ CREATE TABLE collation_tests ( ) CHARACTER SET utf8 COLLATE utf8_general_ci SQL + ActiveRecord::Base.connection.execute <<-SQL +DROP TABLE IF EXISTS enum_tests; +SQL + + ActiveRecord::Base.connection.execute <<-SQL +CREATE TABLE enum_tests ( + enum_column ENUM('true','false') +) +SQL end diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb index 7d324f98c4..802c08b819 100644 --- a/activerecord/test/schema/mysql_specific_schema.rb +++ b/activerecord/test/schema/mysql_specific_schema.rb @@ -43,4 +43,14 @@ CREATE TABLE collation_tests ( ) CHARACTER SET utf8 COLLATE utf8_general_ci SQL + ActiveRecord::Base.connection.execute <<-SQL +DROP TABLE IF EXISTS enum_tests; +SQL + + ActiveRecord::Base.connection.execute <<-SQL +CREATE TABLE enum_tests ( + enum_column ENUM('true','false') +) +SQL + end diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb index 5a61906222..c82da3c6c2 100644 --- a/activesupport/lib/active_support/core_ext/hash/except.rb +++ b/activesupport/lib/active_support/core_ext/hash/except.rb @@ -4,13 +4,6 @@ class Hash # # @person.update_attributes(params[:person].except(:admin)) # - # If the receiver responds to +convert_key+, the method is called on each of the - # arguments. This allows +except+ to play nice with hashes with indifferent access - # for instance: - # - # {:a => 1}.with_indifferent_access.except(:a) # => {} - # {:a => 1}.with_indifferent_access.except('a') # => {} - # def except(*keys) dup.except!(*keys) end diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 8e728691c6..e753e36124 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -7,7 +7,7 @@ class Hash # # => { "NAME" => "Rob", "AGE" => "28" } def transform_keys result = {} - keys.each do |key| + each_key do |key| result[yield(key)] = self[key] end result diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index 48c39d9370..9decbaef32 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -101,7 +101,9 @@ module ActiveSupport end def updated_at(paths) - @updated_at || paths.map { |path| File.mtime(path) }.max || Time.at(0) + time_now = Time.now + @updated_at || paths.map { |path| File.mtime(path) }. + reject { |time| time > time_now }.max || Time.at(0) end def compile_glob(hash) diff --git a/activesupport/lib/active_support/inflections.rb b/activesupport/lib/active_support/inflections.rb index c04c2ed15b..ca2d8cb270 100644 --- a/activesupport/lib/active_support/inflections.rb +++ b/activesupport/lib/active_support/inflections.rb @@ -1,3 +1,5 @@ +require 'active_support/inflector/inflections' + module ActiveSupport Inflector.inflections do |inflect| inflect.plural(/$/, 's') diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index 600e353812..c9e50a9462 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -2,6 +2,8 @@ require 'active_support/core_ext/array/prepend_and_append' module ActiveSupport module Inflector + extend self + # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional # inflection rules. # diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index 2acc6ddee5..c14a43de0d 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'active_support/inflector/inflections' +require 'active_support/inflections' module ActiveSupport # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, diff --git a/activesupport/test/file_update_checker_test.rb b/activesupport/test/file_update_checker_test.rb index 8adff5de8d..bd1df0f858 100644 --- a/activesupport/test/file_update_checker_test.rb +++ b/activesupport/test/file_update_checker_test.rb @@ -48,6 +48,21 @@ class FileUpdateCheckerWithEnumerableTest < ActiveSupport::TestCase assert_equal 1, i end + def test_should_be_robust_to_handle_files_with_wrong_modified_time + i = 0 + now = Time.now + time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future + File.utime time, time, FILES[2] + + checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 } + + sleep(1) + FileUtils.touch(FILES[0..1]) + + assert checker.execute_if_updated + assert_equal 1, i + end + def test_should_cache_updated_result_until_execute i = 0 checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 } diff --git a/guides/source/debugging_rails_applications.textile b/guides/source/debugging_rails_applications.textile index 0802a2db26..cc172042e9 100644 --- a/guides/source/debugging_rails_applications.textile +++ b/guides/source/debugging_rails_applications.textile @@ -102,7 +102,7 @@ It can also be useful to save information to log files at runtime. Rails maintai h4. What is the Logger? -Rails makes use of Ruby's standard +logger+ to write log information. You can also substitute another logger such as +Log4r+ if you wish. +Rails makes use of the +ActiveSupport::BufferedLogger+ class to write log information. You can also substitute another logger such as +Log4r+ if you wish. You can specify an alternative logger in your +environment.rb+ or any environment file: diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 897327a888..07419d11b4 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -900,7 +900,7 @@ end </ruby> The new method, +update_attributes+, is used when you want to update a record -that already exists, and it accepts an hash containing the attributes +that already exists, and it accepts a hash containing the attributes that you want to update. As before, if there was an error updating the post we want to show the form back to the user. @@ -1185,7 +1185,7 @@ delete "posts/:id" => "posts#destroy" That's a lot to type for covering a single *resource*. Fortunately, Rails provides a +resources+ method which can be used to declare a -standard REST resource. Here's how +config/routes/rb+ looks after the +standard REST resource. Here's how +config/routes.rb+ looks after the cleanup: <ruby> diff --git a/guides/source/performance_testing.textile b/guides/source/performance_testing.textile index 958b13cd9e..982fd1b070 100644 --- a/guides/source/performance_testing.textile +++ b/guides/source/performance_testing.textile @@ -524,11 +524,11 @@ Please refer to the "API docs":http://api.rubyonrails.org/classes/ActiveRecord/B h4. Controller -Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715 +Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html <ruby> def process_projects - self.class.benchmark("Processing projects") do + benchmark("Processing projects") do Project.process(params[:project_ids]) Project.update_cached_projects end @@ -539,7 +539,7 @@ NOTE: +benchmark+ is a class method inside controllers h4. View -And in "views":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715: +And in "views":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html: <erb> <% benchmark("Showing projects partial") do %> diff --git a/guides/source/routing.textile b/guides/source/routing.textile index dae25853cd..cffbf9bec4 100644 --- a/guides/source/routing.textile +++ b/guides/source/routing.textile @@ -32,7 +32,13 @@ the request is dispatched to the +patients+ controller's +show+ action with <tt> h4. Generating Paths and URLs from Code -You can also generate paths and URLs. If your application contains this code: +You can also generate paths and URLs. If the route above is modified to be + +<ruby> +get "/patients/:id" => "patients#show", :as => "patient" +</ruby> + +If your application contains this code: <ruby> @patient = Patient.find(17) @@ -845,24 +851,6 @@ end This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+. -h3. Breaking Up a Large Route File - -If you have a large route file that you would like to break up into multiple files, you can use the +#draw+ method in your router: - -<ruby> -draw :admin -</ruby> - -Then, create a file called +config/routes/admin.rb+. Name the file the same as the symbol passed to the +draw+ method. You can then use the normal routing DSL inside that file: - -<ruby> -# in config/routes/admin.rb - -namespace :admin do - resources :posts -end -</ruby> - h3. Inspecting and Testing Routes Rails offers facilities for inspecting and testing your routes. diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 32797ee657..d5ec2cbfd9 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -53,7 +53,6 @@ module Rails autoload :Bootstrap, 'rails/application/bootstrap' autoload :Configuration, 'rails/application/configuration' autoload :Finisher, 'rails/application/finisher' - autoload :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' class << self @@ -80,9 +79,42 @@ module Rails @routes_reloader = nil @env_config = nil @ordered_railties = nil + @railties = nil @queue = nil end + # Returns true if the application is initialized. + def initialized? + @initialized + end + + # Implements call according to the Rack API. It simples + # dispatch the request to the underlying middleware stack. + def call(env) + env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) + super(env) + end + + # Reload application routes regardless if they changed or not. + def reload_routes! + routes_reloader.reload! + end + + # Stores some of the Rails initial environment parameters which + # will be used by middlewares and engines to configure themselves. + def env_config + @env_config ||= super.merge({ + "action_dispatch.parameter_filter" => config.filter_parameters, + "action_dispatch.secret_token" => config.secret_token, + "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions, + "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local, + "action_dispatch.logger" => Rails.logger, + "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner + }) + end + + ## Rails internal API + # This method is called just after an application inherits from Rails::Application, # allowing the developer to load classes in lib and use them during application # configuration. @@ -106,18 +138,14 @@ module Rails require environment if environment end - # Reload application routes regardless if they changed or not. - def reload_routes! - routes_reloader.reload! - end - def routes_reloader #:nodoc: @routes_reloader ||= RoutesReloader.new end - # Returns an array of file paths appended with a hash of directories-extensions - # suitable for ActiveSupport::FileUpdateChecker API. - def watchable_args + # Returns an array of file paths appended with a hash of + # directories-extensions suitable for ActiveSupport::FileUpdateChecker + # API. + def watchable_args #:nodoc: files, dirs = config.watchable_files.dup, config.watchable_dirs.dup ActiveSupport::Dependencies.autoload_paths.each do |path| @@ -138,45 +166,64 @@ module Rails self end - def initialized? - @initialized + def initializers #:nodoc: + Bootstrap.initializers_for(self) + + railties_initializers(super) + + Finisher.initializers_for(self) end - # Load the application and its railties tasks and invoke the registered hooks. - # Check <tt>Rails::Railtie.rake_tasks</tt> for more info. - def load_tasks(app=self) - initialize_tasks - super + def config #:nodoc: + @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd)) + end + + def queue #:nodoc: + @queue ||= build_queue + end + + def build_queue #:nodoc: + config.queue.new + end + + def to_app #:nodoc: self end - # Load the application console and invoke the registered hooks. - # Check <tt>Rails::Railtie.console</tt> for more info. - def load_console(app=self) - initialize_console + def helpers_paths #:nodoc: + config.helpers_paths + end + + def railties #:nodoc: + @railties ||= Rails::Railtie.subclasses.map(&:instance) + + Rails::Engine.subclasses.map(&:instance) + end + + protected + + alias :build_middleware_stack :app + + def run_tasks_blocks(app) #:nodoc: + railties.each { |r| r.run_tasks_blocks(app) } + super + require "rails/tasks" + task :environment do + $rails_rake_task = true + require_environment! + end + end + + def run_generators_blocks(app) #:nodoc: + railties.each { |r| r.run_generators_blocks(app) } super - self end - # Load the application runner and invoke the registered hooks. - # Check <tt>Rails::Railtie.runner</tt> for more info. - def load_runner(app=self) - initialize_runner + def run_runner_blocks(app) #:nodoc: + railties.each { |r| r.run_runner_blocks(app) } super - self end - # Stores some of the Rails initial environment parameters which - # will be used by middlewares and engines to configure themselves. - def env_config - @env_config ||= super.merge({ - "action_dispatch.parameter_filter" => config.filter_parameters, - "action_dispatch.secret_token" => config.secret_token, - "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions, - "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local, - "action_dispatch.logger" => Rails.logger, - "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner - }) + def run_console_blocks(app) #:nodoc: + railties.each { |r| r.run_console_blocks(app) } + super end # Returns the ordered railties for this application considering railties_order. @@ -192,7 +239,7 @@ module Rails end end - all = (railties.all - order) + all = (railties - order) all.push(self) unless (all + order).include?(self) order.push(:all) unless order.include?(:all) @@ -202,46 +249,23 @@ module Rails end end - def initializers #:nodoc: - Bootstrap.initializers_for(self) + - super + - Finisher.initializers_for(self) - end - - def config #:nodoc: - @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd)) - end - - def queue #:nodoc: - @queue ||= build_queue - end - - def build_queue # :nodoc: - config.queue.new - end - - def to_app - self - end - - def helpers_paths #:nodoc: - config.helpers_paths - end - - def call(env) - env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) - super(env) + def railties_initializers(current) #:nodoc: + initializers = [] + ordered_railties.each do |r| + if r == self + initializers += current + else + initializers += r.initializers + end + end + initializers end - protected - - alias :build_middleware_stack :app - - def reload_dependencies? + def reload_dependencies? #:nodoc: config.reload_classes_only_on_change != true || reloaders.map(&:updated?).any? end - def default_middleware_stack + def default_middleware_stack #:nodoc: ActionDispatch::MiddlewareStack.new.tap do |middleware| if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache require "action_dispatch/http/rack_cache" @@ -296,26 +320,7 @@ module Rails end end - def initialize_tasks #:nodoc: - self.class.rake_tasks do - require "rails/tasks" - task :environment do - $rails_rake_task = true - require_environment! - end - end - end - - def initialize_console #:nodoc: - require "pp" - require "rails/console/app" - require "rails/console/helpers" - end - - def initialize_runner #:nodoc: - end - - def build_original_fullpath(env) + def build_original_fullpath(env) #:nodoc: path_info = env["PATH_INFO"] query_string = env["QUERY_STRING"] script_name = env["SCRIPT_NAME"] diff --git a/railties/lib/rails/application/railties.rb b/railties/lib/rails/application/railties.rb deleted file mode 100644 index f20a9689de..0000000000 --- a/railties/lib/rails/application/railties.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'rails/engine/railties' - -module Rails - class Application < Engine - class Railties < Rails::Engine::Railties - def all(&block) - @all ||= railties + engines - @all.each(&block) if block - @all - end - end - end -end diff --git a/railties/lib/rails/application/route_inspector.rb b/railties/lib/rails/application/routes_inspector.rb index 942c4f4789..6b2caf8277 100644 --- a/railties/lib/rails/application/route_inspector.rb +++ b/railties/lib/rails/application/routes_inspector.rb @@ -62,7 +62,7 @@ module Rails ## # This class is just used for displaying route information when someone # executes `rake routes`. People should not use this class. - class RouteInspector # :nodoc: + class RoutesInspector # :nodoc: def initialize @engines = Hash.new end diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index 19f616ec50..6f9a200aa9 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -3,13 +3,12 @@ require "active_support/core_ext/module/delegation" module Rails class Application class RoutesReloader - attr_reader :route_sets, :paths, :external_routes + attr_reader :route_sets, :paths delegate :execute_if_updated, :execute, :updated?, :to => :updater def initialize - @paths = [] - @route_sets = [] - @external_routes = [] + @paths = [] + @route_sets = [] end def reload! @@ -24,11 +23,7 @@ module Rails def updater @updater ||= begin - dirs = @external_routes.each_with_object({}) do |dir, hash| - hash[dir.to_s] = %w(rb) - end - - updater = ActiveSupport::FileUpdateChecker.new(paths, dirs) { reload! } + updater = ActiveSupport::FileUpdateChecker.new(paths) { reload! } updater.execute updater end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 806b553b81..383c159d3d 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -2,7 +2,6 @@ require 'rails/railtie' require 'active_support/core_ext/module/delegation' require 'pathname' require 'rbconfig' -require 'rails/engine/railties' module Rails # <tt>Rails::Engine</tt> allows you to wrap a specific Rails application or subset of @@ -338,27 +337,6 @@ module Rails # config.railties_order = [Blog::Engine, :main_app, :all] class Engine < Railtie autoload :Configuration, "rails/engine/configuration" - autoload :Railties, "rails/engine/railties" - - def initialize - @_all_autoload_paths = nil - @_all_load_paths = nil - @app = nil - @config = nil - @env_config = nil - @helpers = nil - @railties = nil - @routes = nil - super - end - - def load_generators(app=self) - initialize_generators - railties.all { |r| r.load_generators(app) } - Rails::Generators.configure!(app.config.generators) - super - self - end class << self attr_accessor :called_from, :isolated @@ -408,7 +386,7 @@ module Rails end unless mod.respond_to?(:railtie_routes_url_helpers) - define_method(:railtie_routes_url_helpers) { railtie.routes_url_helpers } + define_method(:railtie_routes_url_helpers) { railtie.routes.url_helpers } end end end @@ -417,34 +395,65 @@ module Rails # Finds engine with given path def find(path) expanded_path = File.expand_path path - Rails::Engine::Railties.engines.find { |engine| - File.expand_path(engine.root) == expanded_path - } + Rails::Engine.subclasses.each do |klass| + engine = klass.instance + return engine if File.expand_path(engine.root) == expanded_path + end + nil end end delegate :middleware, :root, :paths, :to => :config delegate :engine_name, :isolated?, :to => "self.class" - def load_tasks(app=self) - railties.all { |r| r.load_tasks(app) } + def initialize + @_all_autoload_paths = nil + @_all_load_paths = nil + @app = nil + @config = nil + @env_config = nil + @helpers = nil + @routes = nil super - paths["lib/tasks"].existent.sort.each { |ext| load(ext) } end + # Load console and invoke the registered hooks. + # Check <tt>Rails::Railtie.console</tt> for more info. def load_console(app=self) - railties.all { |r| r.load_console(app) } - super + require "pp" + require "rails/console/app" + require "rails/console/helpers" + run_console_blocks(app) + self end + # Load Rails runner and invoke the registered hooks. + # Check <tt>Rails::Railtie.runner</tt> for more info. def load_runner(app=self) - railties.all { |r| r.load_runner(app) } - super + run_runner_blocks(app) + self end - def eager_load! - railties.all(&:eager_load!) + # Load Rake, railties tasks and invoke the registered hooks. + # Check <tt>Rails::Railtie.rake_tasks</tt> for more info. + def load_tasks(app=self) + require "rake" + run_tasks_blocks(app) + self + end + # Load rails generators and invoke the registered hooks. + # Check <tt>Rails::Railtie.generators</tt> for more info. + def load_generators(app=self) + require "rails/generators" + run_generators_blocks(app) + Rails::Generators.configure!(app.config.generators) + self + end + + # Eager load the application by loading all ruby + # files inside eager_load paths. + def eager_load! config.eager_load_paths.each do |load_path| matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| @@ -453,10 +462,7 @@ module Rails end end - def railties - @railties ||= self.class::Railties.new(config) - end - + # Returns a module with all the helpers defined for the engine. def helpers @helpers ||= begin helpers = Module.new @@ -468,14 +474,12 @@ module Rails end end + # Returns all registered helpers paths. def helpers_paths paths["app/helpers"].existent end - def routes_url_helpers - routes.url_helpers - end - + # Returns the underlying rack application for this engine. def app @app ||= begin config.middleware = config.middleware.merge_into(default_middleware_stack) @@ -483,45 +487,33 @@ module Rails end end + # Returns the endpoint for this engine. If none is registered, + # defaults to an ActionDispatch::Routing::RouteSet. def endpoint self.class.endpoint || routes end + # Define the Rack API for this engine. def call(env) app.call(env.merge!(env_config)) end + # Defines additional Rack env configuration that is added on each call. def env_config @env_config ||= { 'action_dispatch.routes' => routes } end + # Defines the routes for this engine. If a block is given to + # routes, it is appended to the engine. def routes - @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes| - routes.draw_paths.concat paths["config/routes"].paths - end - + @routes ||= ActionDispatch::Routing::RouteSet.new @routes.append(&Proc.new) if block_given? @routes end - def ordered_railties - railties.all + [self] - end - - def initializers - initializers = [] - ordered_railties.each do |r| - if r == self - initializers += super - else - initializers += r.initializers - end - end - initializers - end - + # Define the configuration object for the engine. def config @config ||= Engine::Configuration.new(find_root_with_flag("lib")) end @@ -560,12 +552,10 @@ module Rails initializer :add_routing_paths do |app| paths = self.paths["config/routes.rb"].existent - external_paths = self.paths["config/routes"].paths if routes? || paths.any? app.routes_reloader.paths.unshift(*paths) app.routes_reloader.route_sets << routes - app.routes_reloader.external_routes.unshift(*external_paths) end end @@ -626,7 +616,6 @@ module Rails else Rake::Task["app:railties:install:migrations"].invoke end - end end end @@ -634,19 +623,20 @@ module Rails protected - def initialize_generators - require "rails/generators" + def run_tasks_blocks(*) #:nodoc: + super + paths["lib/tasks"].existent.sort.each { |ext| load(ext) } end - def routes? + def routes? #:nodoc: @routes end - def has_migrations? + def has_migrations? #:nodoc: paths["db/migrate"].existent.any? end - def find_root_with_flag(flag, default=nil) + def find_root_with_flag(flag, default=nil) #:nodoc: root_path = self.class.called_from while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}") @@ -660,19 +650,19 @@ module Rails Pathname.new File.realpath root end - def default_middleware_stack + def default_middleware_stack #:nodoc: ActionDispatch::MiddlewareStack.new end - def _all_autoload_once_paths + def _all_autoload_once_paths #:nodoc: config.autoload_once_paths end - def _all_autoload_paths + def _all_autoload_paths #:nodoc: @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq end - def _all_load_paths + def _all_load_paths #:nodoc: @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq end end diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index e31df807a6..6b18b1e249 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -53,7 +53,6 @@ module Rails paths.add "config/initializers", :glob => "**/*.rb" paths.add "config/locales", :glob => "*.{rb,yml}" paths.add "config/routes.rb" - paths.add "config/routes", :glob => "**/*.rb" paths.add "db" paths.add "db/migrate" paths.add "db/seeds.rb" diff --git a/railties/lib/rails/engine/railties.rb b/railties/lib/rails/engine/railties.rb deleted file mode 100644 index 033d9c4180..0000000000 --- a/railties/lib/rails/engine/railties.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Rails - class Engine < Railtie - class Railties - # TODO Write tests for this behavior extracted from Application - def initialize(config) - @config = config - end - - def all(&block) - @all ||= [] - @all.each(&block) if block - @all - end - - def self.railties - @railties ||= ::Rails::Railtie.subclasses.map(&:instance) - end - - def self.engines - @engines ||= ::Rails::Engine.subclasses.map(&:instance) - end - - delegate :railties, :engines, :to => "self.class" - end - end -end diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb index 5081074395..bacdcbf3aa 100644 --- a/railties/lib/rails/info_controller.rb +++ b/railties/lib/rails/info_controller.rb @@ -1,4 +1,4 @@ -require 'rails/application/route_inspector' +require 'rails/application/routes_inspector' class Rails::InfoController < ActionController::Base self.view_paths = File.join(File.dirname(__FILE__), 'templates') @@ -15,7 +15,7 @@ class Rails::InfoController < ActionController::Base end def routes - inspector = Rails::Application::RouteInspector.new + inspector = Rails::Application::RoutesInspector.new @info = inspector.format(_routes.routes).join("\n") end diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index 6cd9c7bc95..9b4a69f19f 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -114,7 +114,7 @@ module Rails class Path include Enumerable - attr_reader :path, :root + attr_reader :path attr_accessor :glob def initialize(root, current, paths, options = {}) @@ -180,14 +180,6 @@ module Rails @paths end - def paths - raise "You need to set a path root" unless @root.path - - map do |p| - File.join @root.path, p - end - end - # Expands all paths against the root and return all unique values. def expanded raise "You need to set a path root" unless @root.path diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index a06be59759..1cb99463cc 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -181,33 +181,35 @@ module Rails def eager_load! end - def load_console(app=self) + def railtie_namespace + @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) } + end + + protected + + def run_console_blocks(app) #:nodoc: self.class.console.each { |block| block.call(app) } end - def load_runner(app=self) + def run_generators_blocks(app) #:nodoc: + self.class.generators.each { |block| block.call(app) } + end + + def run_runner_blocks(app) #:nodoc: self.class.runner.each { |block| block.call(app) } end - def load_tasks(app=self) - require 'rake' + def run_tasks_blocks(app) #:nodoc: extend Rake::DSL - self.class.rake_tasks.each { |block| self.instance_exec(app, &block) } + self.class.rake_tasks.each { |block| instance_exec(app, &block) } - # load also tasks from all superclasses + # Load also tasks from all superclasses klass = self.class.superclass + while klass.respond_to?(:rake_tasks) - klass.rake_tasks.each { |t| self.instance_exec(app, &t) } + klass.rake_tasks.each { |t| instance_exec(app, &t) } klass = klass.superclass end end - - def load_generators(app=self) - self.class.generators.each { |block| block.call(app) } - end - - def railtie_namespace - @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) } - end end end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 5778b22f18..4ade825616 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -1,7 +1,7 @@ desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' task :routes => :environment do all_routes = Rails.application.routes.routes - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new + require 'rails/application/routes_inspector' + inspector = Rails::Application::RoutesInspector.new puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n" end diff --git a/railties/railties.gemspec b/railties/railties.gemspec index 207739c4bc..6d28947e83 100644 --- a/railties/railties.gemspec +++ b/railties/railties.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |s| s.rdoc_options << '--exclude' << '.' s.add_dependency('rake', '>= 0.8.7') - s.add_dependency('thor', '>= 0.15.3', '< 2.0') + s.add_dependency('thor', '>= 0.15.4', '< 2.0') s.add_dependency('rdoc', '~> 3.4') s.add_dependency('activesupport', version) s.add_dependency('actionpack', version) diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index e0893f53be..4029984ce9 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -50,8 +50,6 @@ module ApplicationTests assert_path @paths["config/locales"], "config/locales/en.yml" assert_path @paths["config/environment"], "config/environment.rb" assert_path @paths["config/environments"], "config/environments/development.rb" - assert_path @paths["config/routes.rb"], "config/routes.rb" - assert_path @paths["config/routes"], "config/routes" assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first end diff --git a/railties/test/application/route_inspect_test.rb b/railties/test/application/routes_inspect_test.rb index 3b8c874b5b..eacbf3992e 100644 --- a/railties/test/application/route_inspect_test.rb +++ b/railties/test/application/routes_inspect_test.rb @@ -1,5 +1,5 @@ require 'minitest/autorun' -require 'rails/application/route_inspector' +require 'rails/application/routes_inspector' require 'action_controller' require 'rails/engine' @@ -7,7 +7,7 @@ module ApplicationTests class RouteInspectTest < ActiveSupport::TestCase def setup @set = ActionDispatch::Routing::RouteSet.new - @inspector = Rails::Application::RouteInspector.new + @inspector = Rails::Application::RoutesInspector.new app = ActiveSupport::OrderedOptions.new app.config = ActiveSupport::OrderedOptions.new app.config.assets = ActiveSupport::OrderedOptions.new diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index d1373ba202..396b1849d8 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -178,90 +178,7 @@ module ApplicationTests assert_equal 'WIN', last_response.body end - test "routes drawing from config/routes" do - app_file 'config/routes.rb', <<-RUBY - AppTemplate::Application.routes.draw do - draw :external - end - RUBY - - app_file 'config/routes/external.rb', <<-RUBY - get ':controller/:action' - RUBY - - controller :success, <<-RUBY - class SuccessController < ActionController::Base - def index - render :text => "success!" - end - end - RUBY - - app 'development' - get '/success/index' - assert_equal 'success!', last_response.body - end - {"development" => "baz", "production" => "bar"}.each do |mode, expected| - test "reloads routes when external configuration is changed in #{mode}" do - controller :foo, <<-RUBY - class FooController < ApplicationController - def bar - render :text => "bar" - end - - def baz - render :text => "baz" - end - end - RUBY - - app_file 'config/routes.rb', <<-RUBY - AppTemplate::Application.routes.draw do - draw :external - end - RUBY - - app_file 'config/routes/external.rb', <<-RUBY - get 'foo', :to => 'foo#bar' - RUBY - - app(mode) - - get '/foo' - assert_equal 'bar', last_response.body - - app_file 'config/routes/external.rb', <<-RUBY - get 'foo', :to => 'foo#baz' - RUBY - - sleep 0.1 - - get '/foo' - assert_equal expected, last_response.body - - app_file 'config/routes.rb', <<-RUBY - AppTemplate::Application.routes.draw do - draw :external - draw :other_external - end - RUBY - - app_file 'config/routes/other_external.rb', <<-RUBY - get 'win', :to => 'foo#baz' - RUBY - - sleep 0.1 - - get '/win' - - if mode == "development" - assert_equal expected, last_response.body - else - assert_equal 404, last_response.status - end - end - test "reloads routes when configuration is changed in #{mode}" do controller :foo, <<-RUBY class FooController < ApplicationController diff --git a/railties/test/engine_test.rb b/railties/test/engine_test.rb index 68406dce4c..addf49cdb6 100644 --- a/railties/test/engine_test.rb +++ b/railties/test/engine_test.rb @@ -11,14 +11,4 @@ class EngineTest < ActiveSupport::TestCase assert !engine.routes? end - - it "does not add more paths to routes on each call" do - engine = Class.new(Rails::Engine) - - engine.routes - length = engine.routes.draw_paths.length - - engine.routes - assert_equal length, engine.routes.draw_paths.length - end end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 800b1c90f0..6071cd3f39 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -273,24 +273,18 @@ end # Create a scope and build a fixture rails app Module.new do extend TestHelpers::Paths + # Build a rails app - if File.exist?(app_template_path) - FileUtils.rm_rf(app_template_path) - end + FileUtils.rm_rf(app_template_path) FileUtils.mkdir(app_template_path) environment = File.expand_path('../../../../load_paths', __FILE__) - if File.exist?("#{environment}.rb") - require_environment = "-r #{environment}" - end + require_environment = "-r #{environment}" `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails new #{app_template_path}` + File.open("#{app_template_path}/config/boot.rb", 'w') do |f| - if require_environment - f.puts "Dir.chdir('#{File.dirname(environment)}') do" - f.puts " require '#{environment}'" - f.puts "end" - end + f.puts "require '#{environment}'" f.puts "require 'rails/all'" end end unless defined?(RAILS_ISOLATED_ENGINE) diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 5d6b6f9f72..d334034e7d 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -29,7 +29,6 @@ class PathsTest < ActiveSupport::TestCase test "creating a root level path" do @root.add "app" assert_equal ["/foo/bar/app"], @root["app"].to_a - assert_equal ["/foo/bar/app"], @root["app"].paths end test "creating a root level path with options" do @@ -192,7 +191,6 @@ class PathsTest < ActiveSupport::TestCase @root["app"] = "/app" @root["app"].glob = "*.rb" assert_equal "*.rb", @root["app"].glob - assert_equal ["/foo/bar/app"], @root["app"].paths end test "it should be possible to override a path's default glob without assignment" do diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 52c7fae6c6..63814f7a04 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1098,7 +1098,7 @@ YAML assert_equal "// App's bar js\n;", last_response.body.strip # ensure that railties are not added twice - railties = Rails.application.ordered_railties.map(&:class) + railties = Rails.application.send(:ordered_railties).map(&:class) assert_equal railties, railties.uniq end |