Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: carlosantoniodasilva/rails
...
head fork: carlosantoniodasilva/rails
  • 7 commits
  • 8 files changed
  • 0 commit comments
  • 4 contributors
View
4 activemodel/lib/active_model/validations.rb
@@ -177,12 +177,12 @@ def inherited(base)
super
end
end
-
+
# Clean the +Errors+ object if instance is duped
def initialize_dup(other) # :nodoc:
@errors = nil
super
- end
+ end
# Returns the +Errors+ object that holds all information about attribute error messages.
def errors
View
2  activemodel/test/cases/validations_test.rb
@@ -344,7 +344,7 @@ def test_does_not_modify_options_argument
Topic.validates :title, options
assert_equal({ :presence => true }, options)
end
-
+
def test_dup_validity_is_independent
Topic.validates_presence_of :title
topic = Topic.new("title" => "Litterature")
View
36 activerecord/lib/active_record/relation/calculations.rb
@@ -108,15 +108,36 @@ def calculate(operation, column_name, options = {})
0
end
- # This method is designed to perform select by a single column as direct SQL query
- # Returns <tt>Array</tt> with values of the specified column name
- # The values has same data type as column.
+ # Use <tt>pluck</tt> as a shortcut to select a single attribute without
+ # loading a bunch of records just to grab one attribute you want.
+ #
+ # Person.pluck(:name)
+ #
+ # instead of
+ #
+ # Person.all.map(&:name)
+ #
+ # Pluck returns an <tt>Array</tt> of attribute values type-casted to match
+ # the plucked column name, if it can be deduced. Plucking a SQL fragment
+ # returns String values by default.
#
# Examples:
#
- # Person.pluck(:id) # SELECT people.id FROM people
- # Person.uniq.pluck(:role) # SELECT DISTINCT role FROM people
- # Person.where(:age => 21).limit(5).pluck(:id) # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
+ # Person.pluck(:id)
+ # # SELECT people.id FROM people
+ # # => [1, 2, 3]
+ #
+ # Person.uniq.pluck(:role)
+ # # SELECT DISTINCT role FROM people
+ # # => ['admin', 'member', 'guest']
+ #
+ # Person.where(:age => 21).limit(5).pluck(:id)
+ # # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
+ # # => [2, 3]
+ #
+ # Person.pluck('DATEDIFF(updated_at, created_at)')
+ # # SELECT DATEDIFF(updated_at, created_at) FROM people
+ # # => ['0', '27761', '173']
#
def pluck(column_name)
key = column_name.to_s.split('.', 2).last
@@ -130,7 +151,8 @@ def pluck(column_name)
column = types[key]
result.map do |attributes|
- value = klass.initialize_attributes(attributes)[key]
+ raise ArgumentError, "Pluck expects to select just one attribute: #{attributes.inspect}" unless attributes.one?
+ value = klass.initialize_attributes(attributes).first[1]
if column
column.type_cast value
else
View
6 activerecord/test/cases/base_test.rb
@@ -1514,11 +1514,7 @@ def test_clear_cash_when_setting_table_name
after_seq = Joke.sequence_name
assert_not_equal before_columns, after_columns
- unless before_seq.nil? && after_seq.nil?
- assert_not_equal before_seq, after_seq
- assert_equal "cold_jokes_id_seq", before_seq
- assert_equal "funny_jokes_id_seq", after_seq
- end
+ assert_not_equal before_seq, after_seq unless before_seq.nil? && after_seq.nil?
end
def test_dont_clear_sequence_name_when_setting_explicitly
View
8 activerecord/test/cases/calculations_test.rb
@@ -466,6 +466,14 @@ def test_pluck_not_auto_table_name_prefix_if_column_joined
assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
end
+ def test_pluck_with_selection_clause
+ assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT credit_limit').sort
+ end
+
+ def test_pluck_expects_a_single_selection
+ assert_raise(ArgumentError) { Account.pluck 'id, credit_limit' }
+ end
+
def test_plucks_with_ids
assert_equal Company.all.map(&:id).sort, Company.ids.sort
end
View
1  activesupport/lib/active_support/core_ext/string.rb
@@ -6,7 +6,6 @@
require 'active_support/core_ext/string/access'
require 'active_support/core_ext/string/xchar'
require 'active_support/core_ext/string/behavior'
-require 'active_support/core_ext/string/interpolation'
require 'active_support/core_ext/string/output_safety'
require 'active_support/core_ext/string/exclude'
require 'active_support/core_ext/string/strip'
View
2  activesupport/lib/active_support/core_ext/string/interpolation.rb
@@ -1,2 +0,0 @@
-require 'active_support/i18n'
-require 'i18n/core_ext/string/interpolate'
View
14 guides/source/active_support_core_extensions.textile
@@ -1277,20 +1277,6 @@ The <tt>inquiry</tt> method converts a string into a +StringInquirer+ object mak
"active".inquiry.inactive? # => false
</ruby>
-h4. Key-based Interpolation
-
-In Ruby 1.9 the <tt>%</tt> string operator supports key-based interpolation, both formatted and unformatted:
-
-<ruby>
-"Total is %<total>.02f" % {:total => 43.1} # => Total is 43.10
-"I say %{foo}" % {:foo => "wadus"} # => "I say wadus"
-"I say %{woo}" % {:foo => "wadus"} # => KeyError
-</ruby>
-
-Active Support adds that functionality to <tt>%</tt> in previous versions of Ruby.
-
-NOTE: Defined in +active_support/core_ext/string/interpolation.rb+.
-
h4. +starts_with?+ and +ends_with?+
Active Support defines 3rd person aliases of +String#start_with?+ and +String#end_with?+:

No commit comments for this range

Something went wrong with that request. Please try again.