Skip to content

Commit

Permalink
Behavior of AR::Base.new{} fixed (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
inossidabile committed Dec 2, 2013
1 parent 9fac72b commit cd44ff4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/protector/adapters/active_record/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ def new_with_protector(*args, &block)
Protector::ActiveRecord::StrongParameters.sanitize! args, true, protector_meta
end

new_without_protector(*args, &block).restrict!(protector_subject)
unless block_given?
new_without_protector(*args).restrict!(protector_subject)
else
new_without_protector(*args) do |instance|
block.call instance.restrict!(protector_subject)
end
end
end

def create_with_protector(*args, &block)
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/protector/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module ProtectionCase
describe Protector::Adapters::ActiveRecord::Base do
let(:dummy) do
Class.new(ActiveRecord::Base) do
def self.name; 'Dummy'; end
def self.model_name; ActiveModel::Name.new(self, nil, "dummy"); end
self.table_name = "dummies"
scope :none, where('1 = 0') unless respond_to?(:none)
Expand Down Expand Up @@ -93,6 +94,18 @@ def self.model_name; ActiveModel::Name.new(self, nil, "dummy"); end
expect { dummy.restrict!('!').create!(string: 'test').delete }.to raise_error
end

it "validates on new{}" do
dummy.instance_eval do
protect do; end
end

result = dummy.restrict!('!').new do |instance|
instance.protector_subject.should == '!'
end

result.protector_subject.should == '!'
end

it "finds with scope on id column" do
dummy.instance_eval do
protect do
Expand Down

0 comments on commit cd44ff4

Please sign in to comment.