From 702ad710b57bef45b081ebf42e6fa70820fdd810 Mon Sep 17 00:00:00 2001
From: David Heinemeier Hansson <david@loudthinking.com>
Date: Fri, 1 Aug 2014 10:07:38 -0700
Subject: Added Object#self which returns the object itself

---
 activesupport/CHANGELOG.md                               |  6 ++++++
 activesupport/lib/active_support/core_ext/object.rb      |  1 +
 activesupport/lib/active_support/core_ext/object/self.rb | 10 ++++++++++
 activesupport/test/core_ext/object/self_test.rb          |  9 +++++++++
 4 files changed, 26 insertions(+)
 create mode 100644 activesupport/lib/active_support/core_ext/object/self.rb
 create mode 100644 activesupport/test/core_ext/object/self_test.rb

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
-- 
cgit v1.2.3