Fixed #2236 by adding instance_method_already_implemented? check for invalid attribute names (fixes #2236)

This commit is contained in:
Billy Zhou 2018-09-14 22:32:02 +08:00
parent dd848ad135
commit dc8748896d
2 changed files with 54 additions and 0 deletions

View file

@ -898,6 +898,13 @@ is certain attribute used by triggers, overviews or schedulers
reserved_words = %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp]
raise "#{name} is a reserved word, please choose a different one" if name.match?(/^(#{reserved_words.join('|')})$/)
# fixes issue #2236 - Naming an attribute "attribute" causes ActiveRecord failure
begin
ObjectLookup.by_id(object_lookup_id).constantize.instance_method_already_implemented? name
rescue ActiveRecord::DangerousAttributeError => e
raise "#{name} is a reserved word, please choose a different one"
end
record = object_lookup.name.constantize.new
return true if !record.respond_to?(name.to_sym)
raise "#{name} already exists!" if record.attributes.key?(name) && new_record?

View file

@ -49,4 +49,51 @@ RSpec.describe ObjectManager::Attribute, type: :model do
end
end
end
describe 'check name' do
it 'rejects ActiveRecord reserved word "attribute"' do
expect do
ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: 'attribute'
end.to raise_error 'attribute is a reserved word, please choose a different one'
end
it 'rejects Zammad reserved word "table"' do
expect do
ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: 'table'
end.to raise_error 'table is a reserved word, please choose a different one'
end
it 'rejects duplicate attribute name of conflicting types' do
attribute = attributes_for :object_manager_attribute_text
ObjectManager::Attribute.add attribute
attribute[:data_type] = 'boolean'
expect do
ObjectManager::Attribute.add attribute
end.to raise_error ActiveRecord::RecordInvalid
end
it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
attribute = attributes_for :object_manager_attribute_text
ObjectManager::Attribute.add attribute
expect do
ObjectManager::Attribute.add attribute
end.to_not raise_error
end
it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
attribute = attributes_for :object_manager_attribute_text
ObjectManager::Attribute.add attribute
attribute[:data_type] = 'select'
attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
expect do
ObjectManager::Attribute.add attribute
end.to_not raise_error
end
it 'accepts valid attribute names' do
expect do
ObjectManager::Attribute.add attributes_for :object_manager_attribute_text
end.to_not raise_error
end
end
end