diff --git a/lib/sequel/model/base.rb b/lib/sequel/model/base.rb index 1c1a6f1f3..b85169a8c 100644 --- a/lib/sequel/model/base.rb +++ b/lib/sequel/model/base.rb @@ -2035,15 +2035,20 @@ def set_restricted(hash, type) if meths.include?(m) set_column_value(m, v) elsif strict + err_msg = -> (msg) do + msg << " for class #{self.class.name}" if self.class.name + msg + end + # 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, err_msg.call("#{k} is a restricted primary key") else - raise MassAssignmentRestriction, "#{k} is a restricted column" + raise MassAssignmentRestriction, err_msg.call("#{k} is a restricted column") end else - raise MassAssignmentRestriction, "method #{m} doesn't exist" + raise MassAssignmentRestriction, err_msg.call("method #{m} doesn't exist") end end end diff --git a/spec/extensions/csv_serializer_spec.rb b/spec/extensions/csv_serializer_spec.rb index 968eaf3bc..08bccc74d 100644 --- a/spec/extensions/csv_serializer_spec.rb +++ b/spec/extensions/csv_serializer_spec.rb @@ -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 diff --git a/spec/model/base_spec.rb b/spec/model/base_spec.rb index 78c29bd49..9eec636c1 100644 --- a/spec/model/base_spec.rb +++ b/spec/model/base_spec.rb @@ -664,13 +664,27 @@ 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") + end + it "should be disabled by strict_param_setting = false" do @c.strict_param_setting = false @c.strict_param_setting.must_equal false