Skip to content

Commit df9e70c

Browse files
committed
[MODEL] Use ActiveSupport#delegate instead of stdlib's Forwardable
Prevent failures when `ActiveSupport#delegate` is used, e.g. in an ActiveRecord model: class Article < ActiveRecord::Base delegate :size, to: :comments, prefix: true end See: http://api.rubyonrails.org/classes/Module.html#method-i-delegate Closes elastic#6
1 parent 0e7acd8 commit df9e70c

File tree

8 files changed

+23
-63
lines changed

8 files changed

+23
-63
lines changed

elasticsearch-model/elasticsearch-model.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
2222
s.rdoc_options = [ "--charset=UTF-8" ]
2323

2424
s.add_dependency "elasticsearch", '> 0.4'
25+
s.add_dependency "activesupport", '> 3'
2526
s.add_dependency "hashie"
2627

2728
s.add_development_dependency "bundler", "~> 1.3"

elasticsearch-model/examples/activerecord_associations.rb

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#
44
# https://github.com/rails/rails/tree/master/activerecord
55
# http://guides.rubyonrails.org/association_basics.html
6+
#
7+
# Run me with:
8+
#
9+
# ruby -I lib examples/activerecord_associations.rb
10+
#
611

712
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
813

@@ -81,6 +86,8 @@ class Article < ActiveRecord::Base
8186
has_many :comments
8287
end
8388

89+
class Article < ActiveRecord::Base; delegate :size, to: :comments, prefix: true; end
90+
8491
class Comment < ActiveRecord::Base
8592
belongs_to :article, touch: true
8693
end

elasticsearch-model/lib/elasticsearch/model.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
require 'forwardable'
2-
31
require 'elasticsearch'
42

53
require 'hashie'
64

7-
require 'elasticsearch/model/support/forwardable'
5+
require 'active_support/core_ext/module/delegation'
86

97
require 'elasticsearch/model/version'
108

@@ -105,14 +103,15 @@ def as_indexed_json(options={})
105103

106104
# Delegate important methods to the `__elasticsearch__` proxy, unless they are defined already
107105
#
108-
extend Support::Forwardable
109-
forward :'self.__elasticsearch__', :search unless respond_to?(:search)
110-
forward :'self.__elasticsearch__', :mapping unless respond_to?(:mapping)
111-
forward :'self.__elasticsearch__', :mappings unless respond_to?(:mappings)
112-
forward :'self.__elasticsearch__', :settings unless respond_to?(:settings)
113-
forward :'self.__elasticsearch__', :index_name unless respond_to?(:index_name)
114-
forward :'self.__elasticsearch__', :document_type unless respond_to?(:document_type)
115-
forward :'self.__elasticsearch__', :import unless respond_to?(:import)
106+
class << self
107+
delegate :search, to: :__elasticsearch__ unless respond_to?(:search)
108+
delegate :mapping, to: :__elasticsearch__ unless respond_to?(:mapping)
109+
delegate :mappings, to: :__elasticsearch__ unless respond_to?(:mappings)
110+
delegate :settings, to: :__elasticsearch__ unless respond_to?(:settings)
111+
delegate :index_name, to: :__elasticsearch__ unless respond_to?(:index_name)
112+
delegate :document_type, to: :__elasticsearch__ unless respond_to?(:document_type)
113+
delegate :import, to: :__elasticsearch__ unless respond_to?(:import)
114+
end
116115

117116
# Mix the importing module into the proxy
118117
#

elasticsearch-model/lib/elasticsearch/model/response.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ class Response
1414
:took, :timed_out, :shards
1515

1616
include Enumerable
17-
extend Support::Forwardable
1817

19-
forward :results, :each, :empty?, :size, :slice, :[], :to_ary
18+
delegate :each, :empty?, :size, :slice, :[], :to_ary, to: :results
2019

2120
def initialize(klass, search, options={})
2221
@klass = klass

elasticsearch-model/lib/elasticsearch/model/response/pagination.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def self.included(base)
2020
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods
2121
Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods
2222

23-
Elasticsearch::Model::Response::Results.__send__ :forward, :response, :limit_value, :offset_value, :total_count
24-
Elasticsearch::Model::Response::Records.__send__ :forward, :response, :limit_value, :offset_value, :total_count
23+
Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, to: :response
24+
Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, to: :response
2525

2626
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
2727
# Define the `page` Kaminari method

elasticsearch-model/lib/elasticsearch/model/response/records.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ module Response
1010
class Records
1111
include Enumerable
1212

13-
extend Support::Forwardable
14-
forward :records, :each, :empty?, :size, :slice, :[], :to_a, :to_ary
13+
delegate :each, :empty?, :size, :slice, :[], :to_a, :to_ary, to: :records
1514

1615
include Base
1716

elasticsearch-model/lib/elasticsearch/model/response/results.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class Results
1010
include Base
1111
include Enumerable
1212

13-
extend Support::Forwardable
14-
forward :results, :each, :empty?, :size, :slice, :[], :to_a, :to_ary
13+
delegate :each, :empty?, :size, :slice, :[], :to_a, :to_ary, to: :results
1514

1615
# @see Base#initialize
1716
#

elasticsearch-model/lib/elasticsearch/model/support/forwardable.rb

-44
This file was deleted.

0 commit comments

Comments
 (0)