diff options
-rw-r--r-- | activesupport/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/self.rb | 10 | ||||
-rw-r--r-- | activesupport/test/core_ext/object/self_test.rb | 9 |
4 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 61e9927a07..cb57ad0e0a 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,9 @@ +* Added Object#self which returns the object itself. Useful when dealing with a chaining scenario, like Active Record scopes: + + Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self).order(:created_at) + + *DHH* + * `Object#with_options` executes block in merging option context when explicit receiver in not passed. diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index f4f9152d6a..2646342b2e 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -2,6 +2,7 @@ require 'active_support/core_ext/object/acts_like' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/deep_dup' +require 'active_support/core_ext/object/self' require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/inclusion' diff --git a/activesupport/lib/active_support/core_ext/object/self.rb b/activesupport/lib/active_support/core_ext/object/self.rb new file mode 100644 index 0000000000..5c51bf56c9 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/self.rb @@ -0,0 +1,10 @@ +class Object + # Returns the object itself. Useful when dealing with a chaining scenario, like Active Record scopes: + # + # Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self).order(:created_at) + # + # @return Object + def self + self + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/object/self_test.rb b/activesupport/test/core_ext/object/self_test.rb new file mode 100644 index 0000000000..751199e41c --- /dev/null +++ b/activesupport/test/core_ext/object/self_test.rb @@ -0,0 +1,9 @@ +require 'abstract_unit' +require 'active_support/core_ext/object' + +class Object::SelfTest < ActiveSupport::TestCase + test 'self returns self' do + object = 'fun' + assert_equal object, object.self + end +end |