diff options
32 files changed, 254 insertions, 525 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 9888be07a9..3b323b3899 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -2270,7 +2270,7 @@ superclass' view_paths. [Rick Olson] * Update documentation for erb trim syntax. #5651 [matt@mattmargolis.net] -* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 [olivier_ansaldi@yahoo.com, sebastien@goetzilla.info] +* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 [olivier_ansaldi@yahoo.com] * Reset @html_document between requests so assert_tag works. #4810 [Jarkko Laine, easleydp@gmail.com] @@ -2867,7 +2867,7 @@ superclass' view_paths. [Rick Olson] * Provide support for decimal columns to form helpers. Closes #5672. [Dave Thomas] -* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 [olivier_ansaldi@yahoo.com, sebastien@goetzilla.info] +* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 [olivier_ansaldi@yahoo.com] * Reset @html_document between requests so assert_tag works. #4810 [Jarkko Laine, easleydp@gmail.com] diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 003bc1dc2c..53374949ae 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -51,7 +51,7 @@ module ActionDispatch IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix] ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} SHORTHAND_REGEX = %r{^/[\w/]+$} - WILDCARD_PATH = %r{\*([^/]+)$} + WILDCARD_PATH = %r{\*([^/\)]+)\)?$} def initialize(set, scope, path, options) @set, @scope = set, scope @@ -105,13 +105,13 @@ module ActionDispatch # controllers with default routes like :controller/:action/:id(.:format), e.g: # GET /admin/products/show/1 # => { :controller => 'admin/products', :action => 'show', :id => '1' } - @options.reverse_merge!(:controller => /.+?/) + @options[:controller] ||= /.+?/ end # Add a constraint for wildcard route to make it non-greedy and match the # optional format part of the route by default if path.match(WILDCARD_PATH) && @options[:format] != false - @options.reverse_merge!(:"#{$1}" => /.+?/) + @options[$1.to_sym] ||= /.+?/ end if @options[:format] == false @@ -224,19 +224,11 @@ module ActionDispatch end def default_controller - if @options[:controller] - @options[:controller] - elsif @scope[:controller] - @scope[:controller] - end + @options[:controller] || @scope[:controller] end def default_action - if @options[:action] - @options[:action] - elsif @scope[:action] - @scope[:action] - end + @options[:action] || @scope[:action] end end @@ -264,7 +256,7 @@ module ActionDispatch # because this means it will be matched first. As this is the most popular route # of most Rails applications, this is beneficial. def root(options = {}) - match '/', options.reverse_merge(:as => :root) + match '/', { :as => :root }.merge(options) end # Matches a url pattern to one or more routes. Any symbols in a pattern diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index a2d639cd56..7381617dd7 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -55,16 +55,14 @@ module ActionDispatch # assert_redirected_to @customer # def assert_redirected_to(options = {}, message=nil) - validate_request! - assert_response(:redirect, message) return true if options == @response.location - redirected_to_after_normalization = normalize_argument_to_redirection(@response.location) - options_after_normalization = normalize_argument_to_redirection(options) + redirect_is = normalize_argument_to_redirection(@response.location) + redirect_expected = normalize_argument_to_redirection(options) - if redirected_to_after_normalization != options_after_normalization - flunk "Expected response to be a redirect to <#{options_after_normalization}> but was a redirect to <#{redirected_to_after_normalization}>" + if redirect_is != redirect_expected + flunk "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>" end end diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 974c963d44..85dea96bbb 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -365,7 +365,7 @@ module ActionView apply_form_for_options!(record, options) end - options[:html][:remote] = options.delete(:remote) + options[:html][:remote] = options.delete(:remote) if options.has_key?(:remote) options[:html][:method] = options.delete(:method) if options.has_key?(:method) options[:html][:authenticity_token] = options.delete(:authenticity_token) @@ -1227,8 +1227,12 @@ module ActionView parent_builder.multipart = multipart if parent_builder end - def self.model_name - @model_name ||= Struct.new(:partial_path).new(name.demodulize.underscore.sub!(/_builder$/, '')) + def self._to_path + @_to_path ||= name.demodulize.underscore.sub!(/_builder$/, '') + end + + def to_path + self.class._to_path end def to_model diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 7c43dc04e0..c677257d60 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -323,12 +323,12 @@ module ActionView return container if String === container selected, disabled = extract_selected_and_disabled(selected).map do | r | - Array.wrap(r).map(&:to_s) + Array.wrap(r).map { |item| item.to_s } end container.map do |element| html_attributes = option_html_attributes(element) - text, value = option_text_and_value(element).map(&:to_s) + text, value = option_text_and_value(element).map { |item| item.to_s } selected_attribute = ' selected="selected"' if option_value_selected?(value, selected) disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled) %(<option value="#{ERB::Util.html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{ERB::Util.html_escape(text)}</option>) diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb index c0ac332c4e..f67388b8cf 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -206,11 +206,12 @@ module ActionView # <%- end -%> # <% end %> class PartialRenderer < AbstractRenderer #:nodoc: - PARTIAL_NAMES = Hash.new {|h,k| h[k] = {} } + PARTIAL_NAMES = Hash.new { |h,k| h[k] = {} } def initialize(*) super - @partial_names = PARTIAL_NAMES[@lookup_context.prefixes.first] + @context_prefix = @lookup_context.prefixes.first + @partial_names = PARTIAL_NAMES[@context_prefix] end def render(context, options, block) @@ -291,6 +292,7 @@ module ActionView else paths.map! { |path| retrieve_variable(path).unshift(path) } end + if String === partial && @variable.to_s !~ /^[a-z_][a-zA-Z_0-9]*$/ raise ArgumentError.new("The partial name (#{partial}) is not a valid Ruby identifier; " + "make sure your partial name starts with a letter or underscore, " + @@ -360,27 +362,32 @@ module ActionView end def partial_path(object = @object) - @partial_names[object.class.name] ||= begin - object = object.to_model if object.respond_to?(:to_model) - object.class.model_name.partial_path.dup.tap do |partial| - path = @lookup_context.prefixes.first - merge_path_into_partial(path, partial) - end + object = object.to_model if object.respond_to?(:to_model) + + path = if object.respond_to?(:to_path) + object.to_path + else + ActiveSupport::Deprecation.warn "ActiveModel-compatible objects whose classes return a #model_name that responds to #partial_path are deprecated. Please respond to #to_path directly instead." + object.class.model_name.partial_path + end + + @partial_names[path] ||= path.dup.tap do |object_path| + merge_prefix_into_object_path(@context_prefix, object_path) end end - def merge_path_into_partial(path, partial) - if path.include?(?/) && partial.include?(?/) + def merge_prefix_into_object_path(prefix, object_path) + if prefix.include?(?/) && object_path.include?(?/) overlap = [] - path_array = File.dirname(path).split('/') - partial_array = partial.split('/')[0..-3] # skip model dir & partial + prefix_array = File.dirname(prefix).split('/') + object_path_array = object_path.split('/')[0..-3] # skip model dir & partial - path_array.each_with_index do |dir, index| - overlap << dir if dir == partial_array[index] + prefix_array.each_with_index do |dir, index| + overlap << dir if dir == object_path_array[index] end - partial.gsub!(/^#{overlap.join('/')}\//,'') - partial.insert(0, "#{File.dirname(path)}/") + object_path.gsub!(/^#{overlap.join('/')}\//,'') + object_path.insert(0, "#{File.dirname(prefix)}/") end end diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb index 83799d2b4d..c8438e6043 100644 --- a/actionpack/lib/sprockets/railtie.rb +++ b/actionpack/lib/sprockets/railtie.rb @@ -11,15 +11,20 @@ module Sprockets load "sprockets/assets.rake" end - # We need to configure this after initialization to ensure we collect - # paths from all engines. This hook is invoked exactly before routes - # are compiled, and so that other Railties have an opportunity to - # register compressors. - config.after_initialize do |app| - assets = app.config.assets - next unless assets.enabled + initializer "sprockets.environment" do |app| + config = app.config + next unless config.assets.enabled - app.assets = asset_environment(app) + require 'sprockets' + + app.assets = Sprockets::Environment.new(app.root.to_s) do |env| + env.static_root = File.join(app.root.join('public'), config.assets.prefix) + env.logger = ::Rails.logger + + if config.assets.cache_store != false + env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache + end + end ActiveSupport.on_load(:action_view) do include ::Sprockets::Helpers::RailsHelper @@ -28,53 +33,40 @@ module Sprockets include ::Sprockets::Helpers::RailsHelper end end - - app.routes.prepend do - mount app.assets => assets.prefix - end - - if config.action_controller.perform_caching - app.assets = app.assets.index - end end - protected - def asset_environment(app) - require "sprockets" - - assets = app.config.assets - - env = Sprockets::Environment.new(app.root.to_s) + # We need to configure this after initialization to ensure we collect + # paths from all engines. This hook is invoked exactly before routes + # are compiled, and so that other Railties have an opportunity to + # register compressors. + config.after_initialize do |app| + next unless app.assets + config = app.config - env.static_root = File.join(app.root.join("public"), assets.prefix) + config.assets.paths.each { |path| app.assets.append_path(path) } - if env.respond_to?(:append_path) - assets.paths.each { |path| env.append_path(path) } - else - env.paths.concat assets.paths + if config.assets.compress + # temporarily hardcode default JS compressor to uglify. Soon, it will work + # the same as SCSS, where a default plugin sets the default. + unless config.assets.js_compressor == false + app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) } end - env.logger = ::Rails.logger - - if env.respond_to?(:cache) && assets.cache_store != false - env.cache = ActiveSupport::Cache.lookup_store(assets.cache_store) || ::Rails.cache + unless config.assets.css_compressor == false + app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) } end + end - if assets.compress - # temporarily hardcode default JS compressor to uglify. Soon, it will work - # the same as SCSS, where a default plugin sets the default. - unless assets.js_compressor == false - env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) } - end - - unless assets.css_compressor == false - env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) } - end - end + app.routes.prepend do + mount app.assets => config.assets.prefix + end - env + if config.action_controller.perform_caching + app.assets = app.assets.index end + end + protected def expand_js_compressor(sym) case sym when :closure diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb index 81f0efb76e..3316dd03aa 100644 --- a/actionpack/test/dispatch/mapper_test.rb +++ b/actionpack/test/dispatch/mapper_test.rb @@ -35,6 +35,13 @@ module ActionDispatch Mapper.new FakeSet.new end + def test_mapping_requirements + options = { :controller => 'foo', :action => 'bar' } + m = Mapper::Mapping.new FakeSet.new, {}, '/store/:name(*rest)', options + _, _, requirements, _ = m.to_route + assert_equal(/.+?/, requirements[:rest]) + end + def test_map_slash fakeset = FakeSet.new mapper = Mapper.new fakeset diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index cc3d2cddf7..71a2c46d92 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -791,6 +791,23 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_with_remote_in_html + form_for(@post, :url => '/', :html => { :remote => true, :id => 'create-post', :method => :put }) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end + + expected = whole_form("/", "create-post", "edit_post", :method => "put", :remote => true) do + "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[secret]' type='hidden' value='0' />" + + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_remote_without_html @post.persisted = false form_for(@post, :remote => true) do |f| @@ -1874,6 +1891,17 @@ class FormHelperTest < ActionView::TestCase assert_equal LabelledFormBuilder, klass end + def test_form_for_with_labelled_builder_path + path = nil + + form_for(@post, :builder => LabelledFormBuilder) do |f| + path = f.to_path + '' + end + + assert_equal 'labelled_form', path + end + class LabelledFormBuilderSubclass < LabelledFormBuilder; end def test_form_for_with_labelled_builder_with_nested_fields_for_with_custom_builder diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 68b2ed45d1..0b91e55091 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -201,6 +201,36 @@ module RenderTestCases @controller_view.render(customers, :greeting => "Hello") end + class CustomerWithDeprecatedPartialPath + attr_reader :name + + def self.model_name + Struct.new(:partial_path).new("customers/customer") + end + + def initialize(name) + @name = name + end + end + + def test_render_partial_using_object_with_deprecated_partial_path + assert_deprecated(/#model_name.*#partial_path.*#to_path/) do + assert_equal "Hello: nertzy", + @controller_view.render(CustomerWithDeprecatedPartialPath.new("nertzy"), :greeting => "Hello") + end + end + + def test_render_partial_using_collection_with_deprecated_partial_path + assert_deprecated(/#model_name.*#partial_path.*#to_path/) do + customers = [ + CustomerWithDeprecatedPartialPath.new("nertzy"), + CustomerWithDeprecatedPartialPath.new("peeja") + ] + assert_equal "Hello: nertzyHello: peeja", + @controller_view.render(customers, :greeting => "Hello") + end + end + # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" }) diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG index c38349b95e..81386667c2 100644 --- a/activemodel/CHANGELOG +++ b/activemodel/CHANGELOG @@ -1,3 +1,5 @@ +* Deprecate "Model.model_name.partial_path" in favor of "model.to_path" [Grant Hutchins] + * Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior [Bogdan Gusiev] *Rails 3.1.0 (unreleased)* diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb index 1405b1bfe3..39977f12c3 100644 --- a/activemodel/lib/active_model/conversion.rb +++ b/activemodel/lib/active_model/conversion.rb @@ -1,9 +1,12 @@ +require 'active_support/concern' +require 'active_support/inflector' + module ActiveModel # == Active Model Conversions # - # Handles default conversions: to_model, to_key and to_param. + # Handles default conversions: to_model, to_key, to_param, and to_path. # - # Let's take for example this non persisted object. + # Let's take for example this non-persisted object. # # class ContactMessage # include ActiveModel::Conversion @@ -18,8 +21,11 @@ module ActiveModel # cm.to_model == self # => true # cm.to_key # => nil # cm.to_param # => nil + # cm.to_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. @@ -45,5 +51,23 @@ module ActiveModel 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. + def to_path + self.class._to_path + end + + module ClassMethods #:nodoc: + # Provide a class level cache for the to_path. This is an + # internal method and should not be accessed directly. + def _to_path #:nodoc: + @_to_path ||= begin + element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)) + collection = ActiveSupport::Inflector.tableize(self) + "#{collection}/#{element}".freeze + end + end + end end end diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 71ece15b71..1cf8144e98 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -86,7 +86,7 @@ module ActiveModel # Do the error messages include an error with key +error+? def include?(error) - messages.include? error + (v = messages[error]) && v.any? end # Get messages for +key+ diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index b71ef4b22e..08c2e5fcf3 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -43,6 +43,16 @@ module ActiveModel assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false" end + # == Responds to <tt>to_path</tt> + # + # 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_path + assert model.respond_to?(:to_path), "The model should respond to to_path" + assert_kind_of String, model.to_path + end + # == Responds to <tt>valid?</tt> # # Returns a boolean that specifies whether the object is in a valid or invalid @@ -66,15 +76,14 @@ module ActiveModel # == Naming # - # Model.model_name must return a string with some convenience methods as - # :human and :partial_path. Check ActiveModel::Naming for more information. + # Model.model_name must return a string with some convenience methods: + # :human, :singular, and :plural. 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 assert_kind_of String, model_name assert_kind_of String, model_name.human - assert_kind_of String, model_name.partial_path assert_kind_of String, model_name.singular assert_kind_of String, model_name.plural end diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 4c1a82f413..26fa3062eb 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -1,12 +1,15 @@ require 'active_support/inflector' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/module/introspection' +require 'active_support/core_ext/module/deprecation' module ActiveModel class Name < String attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key alias_method :cache_key, :collection + deprecate :partial_path => "ActiveModel::Name#partial_path is deprecated. Call #to_path on model instances directly instead." + def initialize(klass, namespace = nil, name = nil) name ||= klass.name super(name) diff --git a/activemodel/test/cases/conversion_test.rb b/activemodel/test/cases/conversion_test.rb index 7669bf5f65..2eccc4e56d 100644 --- a/activemodel/test/cases/conversion_test.rb +++ b/activemodel/test/cases/conversion_test.rb @@ -1,5 +1,6 @@ require 'cases/helper' require 'models/contact' +require 'models/helicopter' class ConversionTest < ActiveModel::TestCase test "to_model default implementation returns self" do @@ -22,4 +23,10 @@ class ConversionTest < ActiveModel::TestCase test "to_param default implementation returns a string of ids for persisted records" do assert_equal "1", Contact.new(:id => 1).to_param end -end
\ No newline at end of file + + test "to_path default implementation returns a string giving a relative path" do + assert_equal "contacts/contact", Contact.new.to_path + assert_equal "helicopters/helicopter", Helicopter.new.to_path, + "ActiveModel::Conversion#to_path caching should be class-specific" + end +end diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index a24cac40ad..85ca8ca835 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -38,6 +38,7 @@ class ErrorsTest < ActiveModel::TestCase person.errors[:foo] assert person.errors.empty? assert person.errors.blank? + assert !person.errors.include?(:foo) end test "method validate! should work" do diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index f814fcc56c..bafe4f3c0c 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -26,7 +26,9 @@ class NamingTest < ActiveModel::TestCase end def test_partial_path - assert_equal 'post/track_backs/track_back', @model_name.partial_path + assert_deprecated(/#partial_path.*#to_path/) do + assert_equal 'post/track_backs/track_back', @model_name.partial_path + end end def test_human @@ -56,7 +58,9 @@ class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase end def test_partial_path - assert_equal 'blog/posts/post', @model_name.partial_path + assert_deprecated(/#partial_path.*#to_path/) do + assert_equal 'blog/posts/post', @model_name.partial_path + end end def test_human @@ -98,7 +102,9 @@ class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase end def test_partial_path - assert_equal 'blog/posts/post', @model_name.partial_path + assert_deprecated(/#partial_path.*#to_path/) do + assert_equal 'blog/posts/post', @model_name.partial_path + end end def test_human @@ -136,7 +142,9 @@ class NamingWithSuppliedModelNameTest < ActiveModel::TestCase end def test_partial_path - assert_equal 'articles/article', @model_name.partial_path + assert_deprecated(/#partial_path.*#to_path/) do + assert_equal 'articles/article', @model_name.partial_path + end end def test_human diff --git a/activemodel/test/models/helicopter.rb b/activemodel/test/models/helicopter.rb new file mode 100644 index 0000000000..a52b6fb4dd --- /dev/null +++ b/activemodel/test/models/helicopter.rb @@ -0,0 +1,3 @@ +class Helicopter + include ActiveModel::Conversion +end diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e448609cf4..e8d4b9c04e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -24,6 +24,12 @@ a URI that specifies the connection configuration. For example: *Rails 3.1.0 (unreleased)* +* Add a proxy_association method to association proxies, which can be called by association + extensions to access information about the association. This replaces proxy_owner etc with + proxy_association.owner. + + [Jon Leighton] + * ActiveRecord::MacroReflection::AssociationReflection#build_record has a new method signature. Before: def build_association(*options) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 029d7a9b15..2605a54cb6 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -460,6 +460,12 @@ module ActiveRecord # * <tt>record.association(:items).target</tt> - Returns the associated object for +belongs_to+ and +has_one+, or # the collection of associated objects for +has_many+ and +has_and_belongs_to_many+. # + # However, inside the actual extension code, you will not have access to the <tt>record</tt> as + # above. In this case, you can access <tt>proxy_association</tt>. For example, + # <tt>record.association(:items)</tt> and <tt>record.items.proxy_association</tt> will return + # the same object, allowing you to make calls like <tt>proxy_association.owner</tt> inside + # association extensions. + # # === Association Join Models # # Has Many associations can be configured with the <tt>:through</tt> option to use an diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 827dfb7ccb..6ba3d45aff 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -58,23 +58,27 @@ module ActiveRecord alias_method :new, :build + def proxy_association + @association + end + def respond_to?(name, include_private = false) super || (load_target && target.respond_to?(name, include_private)) || - @association.klass.respond_to?(name, include_private) + proxy_association.klass.respond_to?(name, include_private) end def method_missing(method, *args, &block) match = DynamicFinderMatch.match(method) if match && match.instantiator? send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |r| - @association.send :set_owner_attributes, r - @association.send :add_to_target, r + proxy_association.send :set_owner_attributes, r + proxy_association.send :add_to_target, r yield(r) if block_given? end end - if target.respond_to?(method) || (!@association.klass.respond_to?(method) && Class.respond_to?(method)) + if target.respond_to?(method) || (!proxy_association.klass.respond_to?(method) && Class.respond_to?(method)) if load_target if target.respond_to?(method) target.send(method, *args, &block) @@ -104,7 +108,7 @@ module ActiveRecord alias_method :to_a, :to_ary def <<(*records) - @association.concat(records) && self + proxy_association.concat(records) && self end alias_method :push, :<< @@ -114,7 +118,7 @@ module ActiveRecord end def reload - @association.reload + proxy_association.reload self end end diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 0ac821b2d7..9a7ff87e88 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -250,6 +250,7 @@ module ActiveRecord operation, distinct).as(aggregate_alias) ] + select_values += @select_values unless @having_values.empty? select_values.concat group_fields.zip(group_aliases).map { |field,aliaz| "#{field} AS #{aliaz}" diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 49d82ba2df..ffe2993e0f 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -203,6 +203,11 @@ class AssociationProxyTest < ActiveRecord::TestCase assert_equal david.projects, david.projects.reload.reload end end + + def test_proxy_association_accessor + david = developers(:david) + assert_equal david.association(:projects), david.projects.proxy_association + end end class OverridingAssociationsTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 224b3f3d1f..42f98b3d42 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -170,6 +170,13 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 60, c[2] end + def test_should_group_by_summed_field_having_condition_from_select + c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit) + assert_nil c[1] + assert_equal 60, c[2] + assert_equal 53, c[9] + end + def test_should_group_by_summed_association c = Account.sum(:credit_limit, :group => :firm) assert_equal 50, c[companies(:first_firm)] diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index 48fdeb13df..d29eaf5fb6 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -139,7 +139,7 @@ class SchemaTest < ActiveModel::TestCase assert_nothing_raised { Person.schema = new_schema assert_equal new_schema, Person.schema, "should have saved the schema on the class" - assert_equal new_schema, Person.new.schema, "should have mde the schema available to every instance" + assert_equal new_schema, Person.new.schema, "should have made the schema available to every instance" } end @@ -283,8 +283,8 @@ class SchemaTest < ActiveModel::TestCase new_attr_name_two = :another_new_schema_attribute assert Person.schema.blank?, "sanity check - should have a blank class schema" - assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" assert_nothing_raised do Person.schema = {new_attr_name.to_s => 'string'} @@ -301,8 +301,8 @@ class SchemaTest < ActiveModel::TestCase assert Person.schema.blank?, "sanity check - should have a blank class schema" - assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" assert_nothing_raised do Person.schema { string new_attr_name_two } diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index d1543c4c58..8cd4d15e4c 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -478,7 +478,7 @@ module ActiveSupport #:nodoc: qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore - trace = caller.reject {|l| l =~ %r{#{Regexp.escape(__FILE__)}}} + trace = caller.reject {|l| l.starts_with? __FILE__ } name_error = NameError.new("uninitialized constant #{qualified_name}") name_error.set_backtrace(trace) diff --git a/ci/ci_build.rb b/ci/ci_build.rb deleted file mode 100755 index 50f7f410fa..0000000000 --- a/ci/ci_build.rb +++ /dev/null @@ -1,181 +0,0 @@ -#!/usr/bin/env ruby -require 'fileutils' -include FileUtils - -def root_dir - @root_dir ||= File.expand_path('../..', __FILE__) -end - -def rake(*tasks) - tasks.each do |task| - cmd = "bundle exec rake #{task}" - puts "Running command: #{cmd}" - return false unless system(cmd) - end - true -end - -puts "[CruiseControl] Rails build" -build_results = {} - -# Install required version of bundler. -bundler_install_cmd = "sudo gem install bundler --no-ri --no-rdoc" -puts "Running command: #{bundler_install_cmd}" -build_results[:install_bundler] = system bundler_install_cmd - -cd root_dir do - puts - puts "[CruiseControl] Bundling gems" - puts - build_results[:bundle] = system 'bundle update' -end - -cd "#{root_dir}/activesupport" do - puts - puts "[CruiseControl] Building Active Support" - puts - build_results[:activesupport] = rake 'test' - build_results[:activesupport_isolated] = rake 'test:isolated' -end - -system "sudo rm -R #{root_dir}/railties/tmp" -cd "#{root_dir}/railties" do - puts - puts "[CruiseControl] Building Railties" - puts - build_results[:railties] = rake 'test' -end - -cd "#{root_dir}/actionpack" do - puts - puts "[CruiseControl] Building Action Pack" - puts - build_results[:actionpack] = rake 'test' - build_results[:actionpack_isolated] = rake 'test:isolated' -end - -cd "#{root_dir}/actionmailer" do - puts - puts "[CruiseControl] Building Action Mailer" - puts - build_results[:actionmailer] = rake 'test' - build_results[:actionmailer_isolated] = rake 'test:isolated' -end - -cd "#{root_dir}/activemodel" do - puts - puts "[CruiseControl] Building Active Model" - puts - build_results[:activemodel] = rake 'test' - build_results[:activemodel_isolated] = rake 'test:isolated' -end - -rm_f "#{root_dir}/activeresource/debug.log" -cd "#{root_dir}/activeresource" do - puts - puts "[CruiseControl] Building Active Resource" - puts - build_results[:activeresource] = rake 'test' - build_results[:activeresource_isolated] = rake 'test:isolated' -end - -rm_f "#{root_dir}/activerecord/debug.log" -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with MySQL IM enabled" - puts - ENV['IM'] = 'true' - build_results[:activerecord_mysql_IM] = rake 'mysql:rebuild_databases', 'mysql:test' - build_results[:activerecord_mysql_isolated_IM] = rake 'mysql:rebuild_databases', 'mysql:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with MySQL IM disabled" - puts - ENV['IM'] = 'false' - build_results[:activerecord_mysql] = rake 'mysql:rebuild_databases', 'mysql:test' - build_results[:activerecord_mysql_isolated] = rake 'mysql:rebuild_databases', 'mysql:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with MySQL2 IM enabled" - puts - ENV['IM'] = 'true' - build_results[:activerecord_mysql2_IM] = rake 'mysql:rebuild_databases', 'mysql2:test' - build_results[:activerecord_mysql2_isolated_IM] = rake 'mysql:rebuild_databases', 'mysql2:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with MySQL2 IM disabled" - puts - ENV['IM'] = 'false' - build_results[:activerecord_mysql2] = rake 'mysql:rebuild_databases', 'mysql2:test' - build_results[:activerecord_mysql2_isolated] = rake 'mysql:rebuild_databases', 'mysql2:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with PostgreSQL IM enabled" - puts - ENV['IM'] = 'true' - build_results[:activerecord_postgresql8_IM] = rake 'postgresql:rebuild_databases', 'postgresql:test' - build_results[:activerecord_postgresql8_isolated_IM] = rake 'postgresql:rebuild_databases', 'postgresql:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with PostgreSQL IM disabled" - puts - ENV['IM'] = 'false' - build_results[:activerecord_postgresql8] = rake 'postgresql:rebuild_databases', 'postgresql:test' - build_results[:activerecord_postgresql8_isolated] = rake 'postgresql:rebuild_databases', 'postgresql:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with SQLite 3 IM enabled" - puts - ENV['IM'] = 'true' - build_results[:activerecord_sqlite3_IM] = rake 'sqlite3:test' - build_results[:activerecord_sqlite3_isolated_IM] = rake 'sqlite3:isolated_test' -end - -cd "#{root_dir}/activerecord" do - puts - puts "[CruiseControl] Building Active Record with SQLite 3 IM disabled" - puts - ENV['IM'] = 'false' - build_results[:activerecord_sqlite3] = rake 'sqlite3:test' - build_results[:activerecord_sqlite3_isolated] = rake 'sqlite3:isolated_test' -end - - -puts -puts "[CruiseControl] Build environment:" -puts "[CruiseControl] #{`cat /etc/issue`}" -puts "[CruiseControl] #{`uname -a`}" -puts "[CruiseControl] #{`ruby -v`}" -puts "[CruiseControl] #{`mysql --version`}" -puts "[CruiseControl] #{`pg_config --version`}" -puts "[CruiseControl] SQLite3: #{`sqlite3 -version`}" -`gem env`.each_line {|line| print "[CruiseControl] #{line}"} -puts "[CruiseControl] Bundled gems:" -`bundle show`.each_line {|line| print "[CruiseControl] #{line}"} -puts "[CruiseControl] Local gems:" -`gem list`.each_line {|line| print "[CruiseControl] #{line}"} - -failures = build_results.select { |key, value| value == false } - -if failures.empty? - puts - puts "[CruiseControl] Rails build finished sucessfully" - exit(0) -else - puts - puts "[CruiseControl] Rails build FAILED" - puts "[CruiseControl] Failed components: #{failures.map { |component| component.first }.join(', ')}" - exit(-1) -end diff --git a/ci/ci_setup_notes.txt b/ci/ci_setup_notes.txt deleted file mode 100644 index 890f9e8ef6..0000000000 --- a/ci/ci_setup_notes.txt +++ /dev/null @@ -1,140 +0,0 @@ -# Rails Continuous Integration Server Setup Notes -# This procedure was used to set up http://ci.rubyonrails.org on Ubuntu 8.04 -# It can be used as a guideline for setting up your own CI server against your local rails branches - -* Set up ci user: -# log in as root -$ adduser ci -enter user info and password -$ visudo -# give ci user same sudo rights as root - -* Disable root login: -# log in as ci -$ sudo vi /etc/shadow -# overwrite and disable encrypted root password to disable root login: -root:*:14001:0:99999:7::: - -* Change Hostname: -$ sudo vi /etc/hostname -change to correct hostname -$ sudo vi /etc/hosts -replace old hostname with the correct hostname -# reboot to use new hostname (and test reboot) -$ sudo shutdown -r now - -* Update aptitude: -$ sudo aptitude update - -* Use cinabox to perform rest of ruby/ccrb setup: -* https://github.com/thewoolleyman/cinabox/tree/master/README.txt - -# This is not yet properly supported by RubyGems... -# * Configure RubyGems to not require root access for gem installation -# $ vi ~/.profile -# # add this line at bottom: -# PATH="$HOME/.gem/ruby/1.8/bin:$PATH" -# $ sudo vi /etc/init.d/cruise -# # edit the start_cruise line to source CRUISE_USER/.profile: -# start_cruise "cd #{CRUISE_HOME} && source /home/#{CRUISE_USER}/.profile && ./cruise start -d" -# $ vi ~/.gemrc -# # add these lines: -# --- -# gemhome: /home/ci/.gem/ruby/1.8 -# gempath: -# - /home/ci/.gem/ruby/1.8 - -* If you did not configure no-root-gem installation via ~/.gemrc as shown above, then allow no-password sudo for gem installation: -$ sudo visudo -# add this line to bottom: -ci ALL=(ALL) NOPASSWD: ALL - -* Start ccrb via init script and check for default homepage at port 3333 - -* Install/setup nginx: -$ sudo aptitude install nginx -$ sudo vi /etc/nginx/sites-available/default -# Add the following entry at the top of the file above the 'server {' line: -upstream mongrel { - server 127.0.0.1:3333; -} - -# Change server_name entry to match server name - -# replace the contents of the root 'location / {}' block with the following entries: - proxy_pass http://mongrel; - proxy_redirect off; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Client-Verify SUCCESS; - proxy_read_timeout 65; - -# also comment default locations for /doc and /images -$ sudo /etc/init.d/nginx start - -* Add project to cruise (It will still fail until everything is set up): -$ cd ~/ccrb -$ ./cruise add rails -s git -r git://github.com/rails/rails.git # or the URI of your branch - -* Copy and configure cruise site config file: -$ cp ~/.cruise/projects/rails/work/ci/site_config.rb ~/.cruise/site_config.rb -# Edit ~/.cruise/site_config.rb as desired, for example: -ActionMailer::Base.smtp_settings = { - :address => "localhost", - :domain => "ci.yourdomain.com", -} -Configuration.dashboard_refresh_interval = 60.seconds -Configuration.dashboard_url = 'http://ci.yourdomain.com/' -Configuration.serialize_builds = true -Configuration.serialized_build_timeout = 1.hours -BuildReaper.number_of_builds_to_keep = 100 - -* Copy and configure cruise project config file -$ cp ~/.cruise/projects/rails/work/ci/cruise_config.rb ~/.cruise/projects/rails -$ vi ~/.cruise/projects/rails/cruise_config.rb: -# Edit ~/.cruise/projects/rails/cruise_config.rb as desired, for example: -Project.configure do |project| - project.build_command = 'ruby ci/ci_build.rb' - project.email_notifier.emails = ['recipient@yourdomain.com'] - project.email_notifier.from = 'sender@yourdomain.com' -end - -* Set up mysql -$ sudo aptitude install mysql-server-5.0 libmysqlclient-dev -# no password for mysql root user - -* setup sqlite 3 -$ sudo aptitude install sqlite3 libsqlite3-dev -# Note: there's some installation bugs with sqlite3-ruby 1.2.2 gem file permissions: -# http://www.icoretech.org/2008/07/06/no-such-file-to-load-sqlite3-database -# cd /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.2 && sudo find . -perm 0662 -exec chmod 664 {} \; - -* setup postgres -$ sudo aptitude install postgresql postgresql-server-dev-8.3 -$ sudo su - postgres -c 'createuser -s ci' - -* Install fcgi libraries -$ sudo apt-get install libfcgi-dev - -* Install memcached and start for first time (should start on reboot automatically) -$ sudo aptitude install memcached -$ sudo /etc/init.d/memcached start - -* Install and run GemInstaller to get all dependency gems -$ sudo gem install geminstaller -$ cd ~/.cruise/projects/rails/work -$ sudo geminstaller --config=ci/geminstaller.yml # turn up debugging with these options: --geminstaller-output=all --rubygems-output=all - -* Create ActiveRecord test databases for mysql -$ mysql -uroot -e 'grant all on *.* to rails@localhost;' -$ mysql -urails -e 'create database activerecord_unittest;' -$ mysql -urails -e 'create database activerecord_unittest2;' - -* Create ActiveRecord test databases for postgres -# cd to rails activerecord dir -$ rake postgresql:build_databases - -* Reboot and make sure everything is working -$ sudo shutdown -r now -$ http://ci.yourdomain.com diff --git a/ci/cruise_config.rb b/ci/cruise_config.rb deleted file mode 100644 index b64cd8aaea..0000000000 --- a/ci/cruise_config.rb +++ /dev/null @@ -1,9 +0,0 @@ -Project.configure do |project| - project.build_command = 'sudo gem update --system && ruby ci/ci_build.rb' - project.email_notifier.from = 'rails-ci@wyeworks.com' - - # project.campfire_notifier.account = 'rails' - # project.campfire_notifier.token = '' - # project.campfire_notifier.room = 'Rails 3' - # project.campfire_notifier.ssl = true -end diff --git a/ci/site.css b/ci/site.css deleted file mode 100644 index e771c5d1fd..0000000000 --- a/ci/site.css +++ /dev/null @@ -1,13 +0,0 @@ -/* this is a copy of /home/ci/.cruise/site.css, please make any changes to it there */ - -/* this is a copy of /home/ci/.cruise/site.css, please make any changes to it there */ - -/* if you'd like to add custom styles to cruise, add them here */ -/* the following will make successful builds green */ -a.success, a.success:visited { - color: #0A0; -} - -.build_success { - background-image: url(/images/green_gradient.png); -} diff --git a/ci/site_config.rb b/ci/site_config.rb deleted file mode 100644 index f9db39ed57..0000000000 --- a/ci/site_config.rb +++ /dev/null @@ -1,72 +0,0 @@ -# site_config.rb contains examples of various configuration options for the local installation -# of CruiseControl.rb. - -# YOU MUST RESTART YOUR CRUISE CONTROL SERVER FOR ANY CHANGES MADE HERE TO TAKE EFFECT!!! - -# EMAIL NOTIFICATION -# ------------------ - -# CruiseControl.rb can notify you about build status via email. It uses the Action Mailer component of Ruby on Rails -# framework. Obviously, Action Mailer needs to know how to send out email messages. -# If you have an SMTP server on your network, and it needs no authentication, write this in your site_config.rb: -# -ActionMailer::Base.smtp_settings = { - :address => "localhost", - :domain => "ci.rubyonrails.org", -} -# -# If you have no SMTP server at hand, you can configure email notification to use GMail SMTP server, as follows -# (of course, you'll need to create a GMail account): -# -# ActionMailer::Base.smtp_settings = { -# :address => "smtp.gmail.com", -# :port => 587, -# :domain => "yourdomain.com", -# :authentication => :plain, -# :user_name => "yourgmailaccount", -# :password => "yourgmailpassword" -# } -# -# The same approach works for other SMTP servers thet require authentication. Note that GMail's SMTP server runs on a -# non-standard port 587 (standard port for SMTP is 25). -# -# For further details about configuration of outgoing email, see Ruby On Rails documentation for ActionMailer::Base. - -# Other site-wide options are available through Configuration class: - -# Change how often CC.rb pings Subversion for new requests. Default is 10.seconds, which should be OK for a local -# SVN repository, but probably isn't very polite for a public repository, such as RubyForge. This can also be set for -# each project individually, through project.scheduler.polling_interval option: -# Configuration.default_polling_interval = 1.minute - -# How often the dashboard page refreshes itself. If you have more than 10-20 dashboards open, -# it is advisable to set it to something higher than the default 5 seconds: -Configuration.dashboard_refresh_interval = 60.seconds - -# Site-wide setting for the email "from" field. This can also be set on per-project basis, -# through project.email.notifier.from attribute -Configuration.email_from = 'rails-ci@wyeworks.com' - -# Root URL of the dashboard application. Setting this attribute allows various notifiers to include a link to the -# build page in the notification message. -Configuration.dashboard_url = 'http://rails-ci.wyeworks.com/' - -# If you don't want to allow triggering builds through dashboard Build Now button. Useful when you host CC.rb as a -# public web site (such as http://cruisecontrolrb.thoughtworks.com/projects - try clicking on Build Now button there -# and see what happens): -Configuration.disable_build_now = true - -# If you want to only allow one project to build at a time, uncomment this line -# by default, cruise allows multiple projects to build at a time -Configuration.serialize_builds = true - -# Amount of time a project will wait to build before failing when build serialization is on -Configuration.serialized_build_timeout = 3.hours - -# To delete build when there are more than a certain number present, uncomment this line - it will make the dashboard -# perform better -BuildReaper.number_of_builds_to_keep = 100 - -# any files that you'd like to override in cruise, keep in ~/.cruise, and copy over when this file is loaded like this -site_css = CRUISE_DATA_ROOT + "/site.css" -FileUtils.cp site_css, Rails.root + "/public/stylesheets/site.css" if File.exists? site_css |