Skip to content

Commit

Permalink
Merge pull request maxdemarzi#69 from blizkreeg/models
Browse files Browse the repository at this point in the history
bringing active model dirty back to check for changed attributes
  • Loading branch information
blizkreeg committed Dec 5, 2013
2 parents 007ed84 + c59b491 commit 0da9c15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
14 changes: 9 additions & 5 deletions lib/deja/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,21 @@ def incoming_rel(type, cardinality="plural")
end

def create!
run_callbacks :create do
@id = Deja::Query.create_node(persisted_attributes)
raise Deja::Error::OperationFailed, "Failed to create node" unless @id
run_callbacks :save do
run_callbacks :create do
@id = Deja::Query.create_node(persisted_attributes)
raise Deja::Error::OperationFailed, "Failed to create node" unless @id
end
end
self
end

def update!(opts = {})
opts.each { |attribute, value| send("#{attribute}=", value) }
run_callbacks :update do
Deja::Query.update_node(@id, persisted_attributes)
run_callbacks :save do
run_callbacks :update do
Deja::Query.update_node(@id, persisted_attributes)
end
end
self
end
Expand Down
14 changes: 9 additions & 5 deletions lib/deja/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,21 @@ def initialize(*args)
end

def create!
run_callbacks :create do
@id = Deja::Query.create_relationship(@start_node.id, @end_node.id, self.class.label, persisted_attributes)
raise Deja::Error::OperationFailed, "Failed to create relationship" unless @id
run_callbacks :save do
run_callbacks :create do
@id = Deja::Query.create_relationship(@start_node.id, @end_node.id, self.class.label, persisted_attributes)
raise Deja::Error::OperationFailed, "Failed to create relationship" unless @id
end
end
self
end

def update!(opts = {})
opts.each { |attribute, value| send("#{attribute}=", value) }
run_callbacks :update do
Deja::Query.update_relationship(@id, persisted_attributes)
run_callbacks :save do
run_callbacks :update do
Deja::Query.update_relationship(@id, persisted_attributes)
end
end
self
end
Expand Down
11 changes: 10 additions & 1 deletion lib/deja/schema_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ def define_class_key
def attribute(name, type, opts = {})
self.define_class_key
@@all_attributes[self.name][name] = opts.merge(:type => type)
send(:attr_accessor, name)
attr_accessorize(name, opts)
add_property_to_index(name) if opts[:index]
end

def attr_accessorize(name, opts)
send(:attr_accessor, name)
define_attribute_methods name
define_method("#{name}=") do |new_value|
send("#{name}_will_change!") if (new_value != instance_variable_get("@#{name}") && !instance_variable_get("@#{name}").nil?)
instance_variable_set("@#{name}", new_value)
end
end

def indexed_attributes
@@indexed_attributes
end
Expand Down

0 comments on commit 0da9c15

Please sign in to comment.