aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-04-02 19:16:44 +0200
committerJosé Valim <jose.valim@gmail.com>2010-04-02 19:16:44 +0200
commit1cd3d296f527d7109f6e875b3b9c698ff19f8173 (patch)
treed79c8f57c5a00766ddaad3b57c8ccbf999123724
parenta0cdb0499eecd40ba18d33226767783e4a058847 (diff)
parent83ebe6224f546d2154ad4ed72bb3f2c9b07de678 (diff)
downloadrails-1cd3d296f527d7109f6e875b3b9c698ff19f8173.tar.gz
rails-1cd3d296f527d7109f6e875b3b9c698ff19f8173.tar.bz2
rails-1cd3d296f527d7109f6e875b3b9c698ff19f8173.zip
Merge branch 'master' of git://github.com/rails/rails
-rw-r--r--activemodel/lib/active_model/errors.rb2
-rw-r--r--activemodel/test/cases/validations/i18n_validation_test.rb10
-rw-r--r--activerecord/CHANGELOG10
-rw-r--r--activerecord/lib/active_record/named_scope.rb7
-rw-r--r--activerecord/lib/active_record/relation.rb6
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb25
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb15
-rw-r--r--activerecord/test/cases/relations_test.rb16
-rw-r--r--activerecord/test/models/post.rb6
9 files changed, 81 insertions, 16 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 8d28040c32..64b28f6def 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object/blank'
diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb
index 38844bb1fa..7dd82e711d 100644
--- a/activemodel/test/cases/validations/i18n_validation_test.rb
+++ b/activemodel/test/cases/validations/i18n_validation_test.rb
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
require "cases/helper"
require 'cases/tests_database'
require 'models/person'
@@ -41,6 +43,14 @@ class I18nValidationTest < ActiveModel::TestCase
@person.errors.add_on_blank :title, 'custom'
end
+ def test_full_message_encoding
+ I18n.backend.store_translations('en', :errors => {
+ :messages => { :too_short => '猫舌' }})
+ Person.validates_length_of :title, :within => 3..5
+ @person.valid?
+ assert_equal ['Title 猫舌'], @person.errors.full_messages
+ end
+
def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
@person.errors.add(:name, 'not found')
Person.expects(:human_attribute_name).with(:name, :default => 'Name').returns("Person's name")
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e379f4f967..e0625c3dbb 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,15 @@
*Rails 3.0.0 [Beta 2] (pending)*
+* Add Relation extensions. [Pratik Naik]
+
+ users = User.where(:admin => true).extending(User::AdminPowers)
+
+ latest_users = User.order('created_at DESC') do
+ def posts_count
+ Post.count(:user_id => to_a.map(&:id))
+ end
+ end
+
* To prefix the table names of all models in a module, define self.table_name_prefix on the module. #4032 [Andrew White]
* Silenced "SHOW FIELDS" and "SET SQL_AUTO_IS_NULL=0" statements from the MySQL driver to improve log signal to noise ration in development [DHH]
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 9abf979cd0..3456332227 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -133,19 +133,16 @@ module ActiveRecord
delegate :scopes, :with_scope, :with_exclusive_scope, :scoped_methods, :scoped, :to => :klass
def self.init(klass, options, &block)
- relation = new(klass, klass.arel_table)
+ relation = new(klass, klass.arel_table, &block)
scope = if options.is_a?(Hash)
- klass.scoped.apply_finder_options(options.except(:extend))
+ klass.scoped.apply_finder_options(options)
else
options ? klass.scoped.merge(options) : klass.scoped
end
relation = relation.merge(scope)
- Array.wrap(options[:extend]).each {|extension| relation.send(:extend, extension) } if options.is_a?(Hash)
- relation.send(:extend, Module.new(&block)) if block_given?
-
relation.current_scoped_methods_when_defined = klass.send(:current_scoped_methods)
relation
end
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 8577ec58f7..3ca27f06ea 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -13,8 +13,9 @@ module ActiveRecord
delegate :insert, :to => :arel
attr_reader :table, :klass
+ attr_accessor :extensions
- def initialize(klass, table)
+ def initialize(klass, table, &block)
@klass, @table = klass, table
@implicit_readonly = nil
@@ -22,6 +23,9 @@ module ActiveRecord
SINGLE_VALUE_METHODS.each {|v| instance_variable_set(:"@#{v}_value", nil)}
(ASSOCIATION_METHODS + MULTI_VALUE_METHODS).each {|v| instance_variable_set(:"@#{v}_values", [])}
+ @extensions = []
+
+ apply_modules(Module.new(&block)) if block_given?
end
def new(*args, &block)
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index e224781016..b5e8b7570a 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -10,8 +10,9 @@ module ActiveRecord
next if [:where, :having].include?(query_method)
class_eval <<-CEVAL
- def #{query_method}(*args)
+ def #{query_method}(*args, &block)
new_relation = clone
+ new_relation.send(:apply_modules, Module.new(&block)) if block_given?
value = Array.wrap(args.flatten).reject {|x| x.blank? }
new_relation.#{query_method}_values += value if value.present?
new_relation
@@ -21,8 +22,9 @@ module ActiveRecord
[:where, :having].each do |query_method|
class_eval <<-CEVAL
- def #{query_method}(*args)
+ def #{query_method}(*args, &block)
new_relation = clone
+ new_relation.send(:apply_modules, Module.new(&block)) if block_given?
value = build_where(*args)
new_relation.#{query_method}_values += [*value] if value.present?
new_relation
@@ -34,8 +36,9 @@ module ActiveRecord
attr_accessor :"#{query_method}_value"
class_eval <<-CEVAL
- def #{query_method}(value = true)
+ def #{query_method}(value = true, &block)
new_relation = clone
+ new_relation.send(:apply_modules, Module.new(&block)) if block_given?
new_relation.#{query_method}_value = value
new_relation
end
@@ -43,8 +46,16 @@ module ActiveRecord
end
end
- def lock(locks = true)
+ def extending(*modules)
+ new_relation = clone
+ new_relation.send :apply_modules, *modules
+ new_relation
+ end
+
+ def lock(locks = true, &block)
relation = clone
+ relation.send(:apply_modules, Module.new(&block)) if block_given?
+
case locks
when String, TrueClass, NilClass
clone.tap {|new_relation| new_relation.lock_value = locks || true }
@@ -191,6 +202,12 @@ module ActiveRecord
private
+ def apply_modules(modules)
+ values = Array.wrap(modules)
+ @extensions += values if values.present?
+ values.each {|extension| extend(extension) }
+ end
+
def reverse_sql_order(order_query)
order_query.to_s.split(/,/).each { |s|
if s.match(/\s(asc|ASC)$/)
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index a17de1bdbb..8fdd64afcc 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -6,10 +6,9 @@ module ActiveRecord
merged_relation = clone
return merged_relation unless r
- (ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS).reject {|m| [:joins, :where].include?(m)}.each do |method|
- unless (value = r.send(:"#{method}_values")).blank?
- merged_relation.send(:"#{method}_values=", value)
- end
+ (Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS).reject {|m| [:joins, :where].include?(m)}.each do |method|
+ value = r.send(:"#{method}_values")
+ merged_relation.send(:"#{method}_values=", value) if value.present?
end
merged_relation = merged_relation.joins(r.joins_values)
@@ -26,7 +25,7 @@ module ActiveRecord
merged_relation.where_values = merged_wheres
- ActiveRecord::Relation::SINGLE_VALUE_METHODS.reject {|m| m == :lock}.each do |method|
+ Relation::SINGLE_VALUE_METHODS.reject {|m| m == :lock}.each do |method|
unless (value = r.send(:"#{method}_value")).nil?
merged_relation.send(:"#{method}_value=", value)
end
@@ -34,6 +33,9 @@ module ActiveRecord
merged_relation.lock_value = r.lock_value unless merged_relation.lock_value
+ # Apply scope extension modules
+ merged_relation.send :apply_modules, r.extensions
+
merged_relation
end
@@ -69,7 +71,7 @@ module ActiveRecord
result
end
- VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset,
+ VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset, :extend,
:order, :select, :readonly, :group, :having, :from, :lock ]
def apply_finder_options(options)
@@ -84,6 +86,7 @@ module ActiveRecord
relation = relation.where(options[:conditions]) if options.has_key?(:conditions)
relation = relation.includes(options[:include]) if options.has_key?(:include)
+ relation = relation.extending(options[:extend]) if options.has_key?(:extend)
relation
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 1e345399f5..7b9e680c02 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -572,4 +572,20 @@ class RelationTest < ActiveRecord::TestCase
assert_equal Post.all, all_posts.all
end
+ def test_anonymous_extension
+ relation = Post.where(:author_id => 1).order('id ASC') do
+ def author
+ 'lifo'
+ end
+ end
+
+ assert_equal "lifo", relation.author
+ assert_equal "lifo", relation.limit(1).author
+ end
+
+ def test_named_extension
+ relation = Post.where(:author_id => 1).order('id ASC').extending(Post::NamedExtension)
+ assert_equal "lifo", relation.author
+ assert_equal "lifo", relation.limit(1).author
+ end
end
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 704313649a..d092c4bf09 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -1,4 +1,10 @@
class Post < ActiveRecord::Base
+ module NamedExtension
+ def author
+ 'lifo'
+ end
+ end
+
scope :containing_the_letter_a, where("body LIKE '%a%'")
scope :ranked_by_comments, order("comments_count DESC")
scope :limit_by, lambda {|l| limit(l) }