diff options
10 files changed, 85 insertions, 13 deletions
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index b94ad4bb9b..d48f2e8a1f 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -125,7 +125,7 @@ module ActiveModel # # class CommentObserver < ActiveModel::Observer # def after_save(comment) - # Notifications.deliver_comment("admin@do.com", "New comment was posted", comment) + # Notifications.comment("admin@do.com", "New comment was posted", comment).deliver # end # end # diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index 1ca448236e..198ad06360 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -19,7 +19,7 @@ module ActiveRecord def klass type = owner[reflection.foreign_type] - type && type.constantize + type.presence && type.constantize end def raise_on_type_mismatch(record) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 92e525a361..19a7965429 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -577,15 +577,25 @@ module ActiveRecord #:nodoc: # # ==== Examples # - # class Invoice < ActiveRecord::Base; end; + # class Invoice < ActiveRecord::Base + # end + # # file class table_name # invoice.rb Invoice invoices # - # class Invoice < ActiveRecord::Base; class Lineitem < ActiveRecord::Base; end; end; + # class Invoice < ActiveRecord::Base + # class Lineitem < ActiveRecord::Base + # end + # end + # # file class table_name # invoice.rb Invoice::Lineitem invoice_lineitems # - # module Invoice; class Lineitem < ActiveRecord::Base; end; end; + # module Invoice + # class Lineitem < ActiveRecord::Base + # end + # end + # # file class table_name # invoice/lineitem.rb Invoice::Lineitem lineitems # diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index c723436330..5a5351b517 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -11,7 +11,7 @@ module ActiveRecord # # class CommentObserver < ActiveRecord::Observer # def after_save(comment) - # Notifications.deliver_comment("admin@do.com", "New comment was posted", comment) + # Notifications.comment("admin@do.com", "New comment was posted", comment).deliver # end # end # diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 475e4338ca..4659563f33 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -264,6 +264,7 @@ module ActiveRecord end def find_or_instantiator_by_attributes(match, attributes, *args) + options = args.size > 1 && args.last(2).all?{ |a| a.is_a?(Hash) } ? args.extract_options! : {} protected_attributes_for_create, unprotected_attributes_for_create = {}, {} args.each_with_index do |arg, i| if arg.is_a?(Hash) @@ -278,8 +279,7 @@ module ActiveRecord record = where(conditions).first unless record - record = @klass.new do |r| - r.assign_attributes(protected_attributes_for_create) + record = @klass.new(protected_attributes_for_create, options) do |r| r.assign_attributes(unprotected_attributes_for_create, :without_protection => true) end yield(record) if block_given? diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index b993bf6e90..5a900e0605 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -158,6 +158,17 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm" end + def test_polymorphic_association_class + sponsor = Sponsor.new + assert_nil sponsor.association(:sponsorable).send(:klass) + + sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL + assert_nil sponsor.association(:sponsorable).send(:klass) + + sponsor.sponsorable = Member.new :name => "Bert" + assert_equal Member, sponsor.association(:sponsorable).send(:klass) + end + def test_with_polymorphic_and_condition sponsor = Sponsor.create member = Member.create :name => "Bert" diff --git a/activerecord/test/cases/mass_assignment_security_test.rb b/activerecord/test/cases/mass_assignment_security_test.rb index 765033852d..33737e12a8 100644 --- a/activerecord/test/cases/mass_assignment_security_test.rb +++ b/activerecord/test/cases/mass_assignment_security_test.rb @@ -239,6 +239,54 @@ class MassAssignmentSecurityTest < ActiveRecord::TestCase end end + def test_find_or_initialize_by_with_attr_accessible_attributes + p = TightPerson.find_or_initialize_by_first_name('Josh', attributes_hash) + + assert_default_attributes(p) + end + + def test_find_or_initialize_by_with_admin_role_with_attr_accessible_attributes + p = TightPerson.find_or_initialize_by_first_name('Josh', attributes_hash, :as => :admin) + + assert_admin_attributes(p) + end + + def test_find_or_initialize_by_with_attr_protected_attributes + p = LoosePerson.find_or_initialize_by_first_name('Josh', attributes_hash) + + assert_default_attributes(p) + end + + def test_find_or_initialize_by_with_admin_role_with_attr_protected_attributes + p = LoosePerson.find_or_initialize_by_first_name('Josh', attributes_hash, :as => :admin) + + assert_admin_attributes(p) + end + + def test_find_or_create_by_with_attr_accessible_attributes + p = TightPerson.find_or_create_by_first_name('Josh', attributes_hash) + + assert_default_attributes(p, true) + end + + def test_find_or_create_by_with_admin_role_with_attr_accessible_attributes + p = TightPerson.find_or_create_by_first_name('Josh', attributes_hash, :as => :admin) + + assert_admin_attributes(p, true) + end + + def test_find_or_create_by_with_attr_protected_attributes + p = LoosePerson.find_or_create_by_first_name('Josh', attributes_hash) + + assert_default_attributes(p, true) + end + + def test_find_or_create_by_with_admin_role_with_attr_protected_attributes + p = LoosePerson.find_or_create_by_first_name('Josh', attributes_hash, :as => :admin) + + assert_admin_attributes(p, true) + end + end diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 816bff29b7..a7405aae16 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -185,10 +185,12 @@ module Rails end def initialize_tasks - require "rails/tasks" - task :environment do - $rails_rake_task = true - require_environment! + self.class.rake_tasks do + require "rails/tasks" + task :environment do + $rails_rake_task = true + require_environment! + end end end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 20bd9db624..5e1b4b0098 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -9,7 +9,7 @@ source 'http://rubygems.org' <%= "gem 'json'\n" if RUBY_VERSION < "1.9.2" -%> gem 'sass' gem 'coffee-script' -# gem 'uglifier' +gem 'uglifier' <%= gem_for_javascript %> diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index b183eb8ddd..0a65840c2b 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -178,6 +178,7 @@ module Rails end def load_tasks + extend Rake::DSL if defined? Rake::DSL self.class.rake_tasks.each(&:call) # load also tasks from all superclasses |