Skip to content

Commit

Permalink
List of model fields should be lazy-loadable to avoid interferention …
Browse files Browse the repository at this point in the history
…with model until we actually try to protect it (fixes #21)
  • Loading branch information
inossidabile committed Aug 18, 2013
1 parent 313f4a2 commit 25abe3b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 17 deletions.
6 changes: 2 additions & 4 deletions lib/protector/adapters/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?}
Expand Down
6 changes: 2 additions & 4 deletions lib/protector/adapters/sequel/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
14 changes: 9 additions & 5 deletions lib/protector/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/protector/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Protector
# Gem version
VERSION = "0.5.4"
VERSION = "0.5.5"
end
3 changes: 3 additions & 0 deletions migrations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ class Bobby < ActiveRecord::Base
end

class Loony < ActiveRecord::Base
end

class Rumba < ActiveRecord::Base
end
3 changes: 3 additions & 0 deletions migrations/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ class Bobby < Sequel::Model
end

class Loony < Sequel::Model
end

class Rumba < Sequel::Model
end
6 changes: 3 additions & 3 deletions spec/lib/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helpers/examples/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 25abe3b

Please sign in to comment.