Skip to content

Commit

Permalink
Enhance method delete_property to work on defined property attributes…
Browse files Browse the repository at this point in the history
… and associations.
  • Loading branch information
lfu committed Sep 8, 2017
1 parent ded7d76 commit f944cd4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
11 changes: 9 additions & 2 deletions app/models/generic_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ def property_attributes
end

def delete_property(name)
properties.delete(name.to_s)
save!
if !property_attribute_defined?(name) && !property_association_defined?(name)
valid_property_names = generic_object_definition.property_attributes.keys + generic_object_definition.property_associations.keys
raise "Invalid property [#{name}]: must be one of #{valid_property_names.join(", ")}"
end

_property_getter(name).tap do
properties.delete(name.to_s)
save!
end
end

def add_to_property_association(name, objs)
Expand Down
8 changes: 4 additions & 4 deletions app/models/generic_object_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ def add_property_attribute(name, type)

def delete_property_attribute(name)
transaction do
generic_objects.find_each { |o| o.delete_property(name) }

properties[:attributes].delete(name.to_s)
save!

generic_objects.find_each { |o| o.delete_property(name) }
end
end

Expand All @@ -112,10 +112,10 @@ def add_property_association(name, type)

def delete_property_association(name)
transaction do
generic_objects.find_each { |o| o.delete_property(name) }

properties[:associations].delete(name.to_s)
save!

generic_objects.find_each { |o| o.delete_property(name) }
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/models/generic_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,27 @@
expect(go.my_host).to eq("some_return_value")
end
end

describe '#delete_property' do
it 'an attriute' do
max = go.max_number
expect(go.delete_property("max_number")).to eq(max)
expect(go.max_number).to be_nil
end

it 'an association' do
vm2 = FactoryGirl.create(:vm_vmware)
go.vms = [vm1, vm2]
expect(go.delete_property("vms")).to match_array([vm1, vm2])
expect(go.vms).to be_empty
end

it 'a method' do
expect { go.delete_property("my_host") }.to raise_error(RuntimeError)
end

it 'an invalid property name' do
expect { go.delete_property("some_attribute_not_defined") }.to raise_error(RuntimeError)
end
end
end

0 comments on commit f944cd4

Please sign in to comment.