Permalink
Browse files

Added Object#self which returns the object itself

  • Loading branch information...
1 parent 20405e5 commit 702ad710b57bef45b081ebf42e6fa70820fdd810 @dhh dhh committed Aug 1, 2014
@@ -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.
@@ -2,6 +2,7 @@
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'
@@ -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
@@ -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

3 comments on commit 702ad71

@rubiii
Contributor
rubiii commented on 702ad71 Aug 2, 2014

The example for Object#self seems incorrect, since the ternary is missing an else (:).

>> Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self)
SyntaxError: (irb):1: syntax error, unexpected ')', expecting ':'
@dhh
Member
dhh commented on 702ad71 Aug 2, 2014
@rubiii
Contributor
rubiii commented on 702ad71 Aug 2, 2014

great. missed that one. thanks.

Please sign in to comment.