Skip to content

Commit

Permalink
Added examples for Rails3 ActiveModel validations and serialization [#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Mar 20, 2010
1 parent 02eebb5 commit df5d745
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/active_model/serializers.rb
@@ -0,0 +1,25 @@
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../lib")

require 'rubygems'
require 'neo4j'
require 'neo4j/extensions/activemodel'

class Person
include Neo4j::NodeMixin
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml

property :first_name
property :last_name

end


Neo4j::Transaction.run do
person = Person.new :first_name => 'jimmy', :last_name => 'smith'

puts "HASH: #{person.serializable_hash}"
puts "JSON: #{person.to_json}"
puts "XML : #{person.to_xml}"

end
26 changes: 26 additions & 0 deletions examples/active_model/validation.rb
@@ -0,0 +1,26 @@
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../lib")

require 'rubygems'
require 'neo4j'
require 'neo4j/extensions/activemodel'

class Person
include Neo4j::NodeMixin
include ActiveModel::Validations

property :first_name
property :last_name

validates_presence_of :first_name, :last_name
end


Neo4j::Transaction.run do
person = Person.new

puts person.valid?
puts person.errors.inspect
person.first_name = "Hej"
person.last_name = "hop"
puts person.valid?
end
8 changes: 8 additions & 0 deletions lib/neo4j/extensions/activemodel.rb
Expand Up @@ -3,10 +3,18 @@
require 'neo4j'
require 'active_model'


module Neo4j::NodeMixin
def to_model
Neo4j::ActiveModel::ActiveModelFactory.instance.to_model(self)
end

def attributes
attr = props
attr.keys.each {|k| attr.delete k if k[0] == ?_}
attr
end

end

module Neo4j::ActiveModel
Expand Down

0 comments on commit df5d745

Please sign in to comment.