diff --git a/lib/rails_admin/config/lazy_model.rb b/lib/rails_admin/config/lazy_model.rb index e15e6ee662..efc60fa427 100644 --- a/lib/rails_admin/config/lazy_model.rb +++ b/lib/rails_admin/config/lazy_model.rb @@ -2,7 +2,7 @@ module RailsAdmin module Config - class LazyModel + class LazyModel < BasicObject def initialize(entity, &block) @entity = entity @deferred_block = block @@ -10,7 +10,7 @@ def initialize(entity, &block) def target unless @model - @model = RailsAdmin::Config::Model.new(@entity) + @model = ::RailsAdmin::Config::Model.new(@entity) @model.instance_eval(&@deferred_block) if @deferred_block end @model diff --git a/lib/rails_admin/config/proxyable/proxy.rb b/lib/rails_admin/config/proxyable/proxy.rb index 36b280dcdc..d4178d833d 100644 --- a/lib/rails_admin/config/proxyable/proxy.rb +++ b/lib/rails_admin/config/proxyable/proxy.rb @@ -1,9 +1,7 @@ module RailsAdmin module Config module Proxyable - class Proxy - instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_eval|object_id)/ } - + class Proxy < BasicObject attr_reader :bindings def initialize(object, bindings = {}) @@ -13,7 +11,7 @@ def initialize(object, bindings = {}) # Bind variables to be used by the configuration options def bind(key, value = nil) - if key.is_a?(Hash) + if key.is_a?(::Hash) @bindings = key else @bindings[key] = value diff --git a/spec/rails_admin/config/lazy_model_spec.rb b/spec/rails_admin/config/lazy_model_spec.rb index f90fd06c44..3eda2297ba 100644 --- a/spec/rails_admin/config/lazy_model_spec.rb +++ b/spec/rails_admin/config/lazy_model_spec.rb @@ -1,29 +1,45 @@ require 'spec_helper' describe RailsAdmin::Config::LazyModel do - describe '#store' do - let(:block) { proc { register_instance_option('parameter') } } # an arbitrary instance method we can spy on - let(:other_block) { proc { register_instance_option('other parameter') } } + subject { RailsAdmin::Config::LazyModel.new(:Team, &block) } + let(:block) { proc { register_instance_option('parameter') } } # an arbitrary instance method we can spy on + describe '#store' do it "doesn't evaluate the block immediately" do expect_any_instance_of(RailsAdmin::Config::Model).not_to receive(:register_instance_option) - - RailsAdmin::Config::LazyModel.new(:Team, &block) + subject end it 'evaluates block when reading' do expect_any_instance_of(RailsAdmin::Config::Model).to receive(:register_instance_option).with('parameter') - - lazy_model = RailsAdmin::Config::LazyModel.new(:Team, &block) - lazy_model.groups # an arbitrary instance method on RailsAdmin::Config::Model to wake up lazy_model + subject.groups # an arbitrary instance method on RailsAdmin::Config::Model to wake up lazy_model end it 'evaluates config block only once' do expect_any_instance_of(RailsAdmin::Config::Model).to receive(:register_instance_option).once.with('parameter') - lazy_model = RailsAdmin::Config::LazyModel.new(:Team, &block) - lazy_model.groups - lazy_model.groups + subject.groups + subject.groups + end + end + + context 'when a method is defined in Kernel' do + before do + Kernel.module_eval do + def weight + 42 + end + end + end + + after do + Kernel.module_eval do + undef weight + end + end + + it 'proxies calls for the method to @object' do + expect(subject.weight).to eq 0 end end end diff --git a/spec/rails_admin/config/proxyable/proxy_spec.rb b/spec/rails_admin/config/proxyable/proxy_spec.rb index 1c9ae06f8f..8318039175 100644 --- a/spec/rails_admin/config/proxyable/proxy_spec.rb +++ b/spec/rails_admin/config/proxyable/proxy_spec.rb @@ -6,6 +6,10 @@ class ProxyTest def initialize @bindings = {foo: 'bar'} end + + def qux + 'foobar' + end end let(:proxy_test) { ProxyTest.new } @@ -19,4 +23,24 @@ def initialize subject.bindings expect(proxy_test.bindings).to eq foo: 'bar' end + + context 'when a method is defined in Kernel' do + before do + Kernel.module_eval do + def qux + 'quux' + end + end + end + + after do + Kernel.module_eval do + undef qux + end + end + + it 'proxies calls for the method to @object' do + expect(subject.qux).to eq 'foobar' + end + end end