From 25abe3b5d9aa8924b44d38f25020b063c5386eda Mon Sep 17 00:00:00 2001 From: Boris Staal Date: Sun, 18 Aug 2013 12:15:55 +0700 Subject: [PATCH] List of model fields should be lazy-loadable to avoid interferention with model until we actually try to protect it (fixes #21) --- lib/protector/adapters/active_record/base.rb | 6 ++---- lib/protector/adapters/sequel/model.rb | 6 ++---- lib/protector/dsl.rb | 14 +++++++++----- lib/protector/version.rb | 2 +- migrations/active_record.rb | 3 +++ migrations/sequel.rb | 3 +++ spec/lib/dsl_spec.rb | 6 +++--- spec/spec_helpers/examples/model.rb | 8 ++++++++ 8 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/protector/adapters/active_record/base.rb b/lib/protector/adapters/active_record/base.rb index 9a87a0c..e17297a 100644 --- a/lib/protector/adapters/active_record/base.rb +++ b/lib/protector/adapters/active_record/base.rb @@ -63,11 +63,9 @@ def [](name) module ClassMethods # Storage of {Protector::DSL::Meta} def protector_meta - @protector_meta ||= Protector::DSL::Meta.new( - Protector::Adapters::ActiveRecord, - self, + @protector_meta ||= Protector::DSL::Meta.new(Protector::Adapters::ActiveRecord, self) do self.column_names - ) + end end # Wraps every `.field` method with a check against {Protector::DSL::Meta::Box#readable?} diff --git a/lib/protector/adapters/sequel/model.rb b/lib/protector/adapters/sequel/model.rb index 561356a..a8bddb2 100644 --- a/lib/protector/adapters/sequel/model.rb +++ b/lib/protector/adapters/sequel/model.rb @@ -18,11 +18,9 @@ def restrict!(*args) module ClassMethods # Storage of {Protector::DSL::Meta} def protector_meta - @protector_meta ||= Protector::DSL::Meta.new( - Protector::Adapters::Sequel, - self, + @protector_meta ||= Protector::DSL::Meta.new(Protector::Adapters::Sequel, self) do self.columns - ) + end end # Gets default restricted `Dataset` diff --git a/lib/protector/dsl.rb b/lib/protector/dsl.rb index a82fd00..4c93a72 100644 --- a/lib/protector/dsl.rb +++ b/lib/protector/dsl.rb @@ -214,10 +214,14 @@ def modifiable?(part, fields) end end - def initialize(adapter, model, fields) - @adapter = adapter - @model = model - @fields = fields + def initialize(adapter, model, &fields_proc) + @adapter = adapter + @model = model + @fields_proc = fields_proc + end + + def fields + @fields ||= @fields_proc.call end # Storage for `protect` blocks @@ -235,7 +239,7 @@ def <<(block) # @param subject [Object] Restriction subject # @param entry [Object] An instance of the model def evaluate(subject, entry=nil) - Box.new(@adapter, @model, @fields, subject, entry, blocks) + Box.new(@adapter, @model, fields, subject, entry, blocks) end end diff --git a/lib/protector/version.rb b/lib/protector/version.rb index 8232f29..6d23d08 100644 --- a/lib/protector/version.rb +++ b/lib/protector/version.rb @@ -1,4 +1,4 @@ module Protector # Gem version - VERSION = "0.5.4" + VERSION = "0.5.5" end diff --git a/migrations/active_record.rb b/migrations/active_record.rb index 85d5a1d..0cae616 100644 --- a/migrations/active_record.rb +++ b/migrations/active_record.rb @@ -44,4 +44,7 @@ class Bobby < ActiveRecord::Base end class Loony < ActiveRecord::Base +end + +class Rumba < ActiveRecord::Base end \ No newline at end of file diff --git a/migrations/sequel.rb b/migrations/sequel.rb index a9df658..cf50ace 100644 --- a/migrations/sequel.rb +++ b/migrations/sequel.rb @@ -46,4 +46,7 @@ class Bobby < Sequel::Model end class Loony < Sequel::Model +end + +class Rumba < Sequel::Model end \ No newline at end of file diff --git a/spec/lib/dsl_spec.rb b/spec/lib/dsl_spec.rb index 2a0bc93..66015bd 100644 --- a/spec/lib/dsl_spec.rb +++ b/spec/lib/dsl_spec.rb @@ -42,7 +42,7 @@ include Protector::DSL::Entry def self.protector_meta - @protector_meta ||= Protector::DSL::Meta.new nil, nil, [] + @protector_meta ||= Protector::DSL::Meta.new(nil, nil){[]} end end end @@ -61,7 +61,7 @@ def self.protector_meta l = lambda {|x| x > 4 } before :each do - @meta = Protector::DSL::Meta.new nil, nil, %w(field1 field2 field3 field4 field5) + @meta = Protector::DSL::Meta.new(nil, nil){%w(field1 field2 field3 field4 field5)} @meta << lambda { can :view } @@ -159,7 +159,7 @@ def self.protector_meta context "custom methods" do before :each do - @meta = Protector::DSL::Meta.new nil, nil, %w(field1 field2) + @meta = Protector::DSL::Meta.new(nil, nil){%w(field1 field2)} @meta << lambda { can :drink, :field1 diff --git a/spec/spec_helpers/examples/model.rb b/spec/spec_helpers/examples/model.rb index 9d786e0..4b873b8 100644 --- a/spec/spec_helpers/examples/model.rb +++ b/spec/spec_helpers/examples/model.rb @@ -30,6 +30,14 @@ d.instance_variable_get('@protector_meta').should == nil end + it "doesn't get stuck with non-existing tables" do + Rumba.class_eval do + protect do + can + end + end + end + describe "visibility" do it "marks blocked" do Dummy.first.restrict!('-').visible?.should == false