Skip to content

Commit

Permalink
Proxy classes should inherit from BasicObject. Closes #2434
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Oct 31, 2015
1 parent 60c277b commit 5daf6e9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/rails_admin/config/lazy_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

module RailsAdmin
module Config
class LazyModel
class LazyModel < BasicObject
def initialize(entity, &block)
@entity = entity
@deferred_block = block
end

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
Expand Down
6 changes: 2 additions & 4 deletions lib/rails_admin/config/proxyable/proxy.rb
Original file line number Diff line number Diff line change
@@ -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 = {})
Expand All @@ -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
Expand Down
38 changes: 27 additions & 11 deletions spec/rails_admin/config/lazy_model_spec.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions spec/rails_admin/config/proxyable/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class ProxyTest
def initialize
@bindings = {foo: 'bar'}
end

def qux
'foobar'
end
end

let(:proxy_test) { ProxyTest.new }
Expand All @@ -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

0 comments on commit 5daf6e9

Please sign in to comment.