Skip to content

Commit

Permalink
implement on model update callback
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Apr 17, 2024
1 parent d6b6dff commit ddc4caf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
37 changes: 28 additions & 9 deletions lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,26 +298,45 @@ def add_aggregate(attribute,aggregate,value)
def save(*opts)

if self.kind_of?(Goo::Base::Enum)
unless opts[0] && opts[0][:init_enum]
raise ArgumentError, "Enums can only be created on initialization"
end
raise ArgumentError, "Enums can only be created on initialization" unless opts[0] && opts[0][:init_enum]
end
batch_file = nil
if opts && opts.length > 0
if opts.first.is_a?(Hash) && opts.first[:batch] && opts.first[:batch].is_a?(File)
callbacks = true
if opts && opts.length > 0 && opts.first.is_a?(Hash)
if opts.first[:batch] && opts.first[:batch].is_a?(File)
batch_file = opts.first[:batch]
end

callbacks = opts.first[:callbacks]
end

if !batch_file
if not modified?
return self
end
return self if not modified?
raise Goo::Base::NotValidException, "Object is not valid. Check errors." unless valid?
end

#set default values before saving
unless self.persistent?
self.class.attributes_with_defaults.each do |attr|
value = self.send("#{attr}")
if value.nil?
value = self.class.default(attr).call(self)
self.send("#{attr}=", value)
end
end
end

#call update callback before saving
if callbacks
self.class.attributes_with_update_callbacks.each do |attr|
Goo::Validators::Enforce.enforce_callbacks(self, attr)
end
end

graph_insert, graph_delete = Goo::SPARQL::Triples.model_update_triples(self)
graph = self.graph()
graph = self.graph


if graph_delete and graph_delete.size > 0
begin
Goo.sparql_update_client.delete_data(graph_delete, graph: graph)
Expand Down
10 changes: 10 additions & 0 deletions lib/goo/base/settings/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def attributes_with_defaults
select{ |attr,opts| opts[:default] }).keys()
end

def attributes_with_update_callbacks
(@model_settings[:attributes].
select{ |attr,opts| opts[:onUpdate] }).keys
end


def update_callbacks(attr)
@model_settings[:attributes][attr][:onUpdate]
end

def default(attr)
return @model_settings[:attributes][attr][:default]
end
Expand Down
53 changes: 53 additions & 0 deletions test/test_update_callbacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative 'test_case'


require_relative 'models'

class TestUpdateCallBack < Goo::Base::Resource
model :update_callback_model, name_with: :code
attribute :code, enforce: [:string, :existence]
attribute :name, enforce: [:string, :existence]
attribute :first_name, onUpdate: :update_name
attribute :last_name, onUpdate: :update_name


def update_name(inst, attr)
self.name = self.first_name + self.last_name
end
end

class TestUpdateCallBacks < MiniTest::Unit::TestCase

def self.before_suite
GooTestData.delete_all [TestUpdateCallBack]
end

def self.after_suite
GooTestData.delete_all [TestUpdateCallBack]
end


def test_update_callback
p = TestUpdateCallBack.new
p.code = "1"
p.name = "name"
p.first_name = "first_name"
p.last_name = "last_name"

assert p.valid?
p.save

p.bring_remaining

assert_equal p.first_name + p.last_name, p.name

p.last_name = "last_name2"
p.save

p.bring_remaining
assert_equal "last_name2", p.last_name
assert_equal p.first_name + p.last_name, p.name
end

end

0 comments on commit ddc4caf

Please sign in to comment.