Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass class name to Sequel::MassAssignmentRestriction error #2079

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/sequel/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2038,12 +2038,12 @@ def set_restricted(hash, type)
# Avoid using respond_to? or creating symbols from user input
if public_methods.map(&:to_s).include?(m)
if Array(model.primary_key).map(&:to_s).member?(k.to_s) && model.restrict_primary_key?
raise MassAssignmentRestriction, "#{k} is a restricted primary key"
raise MassAssignmentRestriction.create("#{k} is a restricted primary key", self)
else
raise MassAssignmentRestriction, "#{k} is a restricted column"
raise MassAssignmentRestriction.create("#{k} is a restricted column", self)
end
else
raise MassAssignmentRestriction, "method #{m} doesn't exist"
raise MassAssignmentRestriction.create("method #{m} doesn't exist", self)
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions lib/sequel/model/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ def initialize(message=nil, model=nil)
UndefinedAssociation = Class.new(Error)
).name

(
# Raised when a mass assignment method is called in strict mode with either a restricted column
# or a column without a setter method.
MassAssignmentRestriction = Class.new(Error)
).name
class MassAssignmentRestriction < Error
# The Sequel::Model object related to this exception.
attr_reader :model

def self.create(msg, model)
msg += " for class #{model.class.name}" if model.class.name
new(msg).tap do |err|
err.instance_variable_set(:@model, model)
end
end
end

# Exception class raised when +raise_on_save_failure+ is set and validation fails
class ValidationFailed < Error
Expand Down
3 changes: 2 additions & 1 deletion spec/extensions/csv_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def self.name; 'Album' end

it "should raise an error if attempting to set a restricted column and :all_columns is not used" do
@Artist.restrict_primary_key
proc{@Artist.from_csv(@artist.to_csv)}.must_raise(Sequel::MassAssignmentRestriction)
err = proc{@Artist.from_csv(@artist.to_csv)}.must_raise(Sequel::MassAssignmentRestriction)
err.message.must_equal("id is a restricted primary key for class Artist")
end

it "should use a dataset's selected columns" do
Expand Down
17 changes: 16 additions & 1 deletion spec/model/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,13 +664,28 @@ class ModelTest2 < Sequel::Model(@db[:foo])
end

it "should raise an error if a missing/restricted column/method is accessed" do
proc{@c.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
err = proc{@c.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
err.message.must_equal("method a= doesn't exist")
proc{@c.create(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
c = @c.new
proc{c.set(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
proc{c.update(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
end

it "should add class name to error message" do
item = Class.new(Sequel::Model(:items)) do
columns :id
end

item.class_eval do
def self.name; 'Item' end
end

err = proc{item.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction)
err.message.must_equal("method a= doesn't exist for class Item")
err.model.class.must_equal(item)
end

it "should be disabled by strict_param_setting = false" do
@c.strict_param_setting = false
@c.strict_param_setting.must_equal false
Expand Down
15 changes: 15 additions & 0 deletions spec/model/exceptions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative "spec_helper"

describe Sequel::MassAssignmentRestriction, "#create" do
it "should set model attr" do
model_cls = Class.new
model_cls.class_eval do
def self.name; 'TestModel' end
end

model = model_cls.new
err = Sequel::MassAssignmentRestriction.create("method foo doesn't exist", model)
err.message.must_equal("method foo doesn't exist for class TestModel")
err.model.must_equal(model)
end
end
Loading