Skip to content

Commit

Permalink
Added support for Ruby on Rails, Added some method that rails needs o…
Browse files Browse the repository at this point in the history
…n the Node. Added support for Value Objects, started to write a Rails with Neo4j.rb tutorial
  • Loading branch information
andreasronge committed Dec 9, 2008
1 parent 3d43d55 commit d9cfedc
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 13 deletions.
211 changes: 210 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ It uses two powerful java libraries:
* Neo4J (http://www.neo4j.org/) - for persistance and traversal of the graph
* Lucene (http://lucene.apache.org/java/docs/index.html) for quering and indexing.

This document contains:
* Installation guide
* Ten Minute Tutorial
* Lucene API Documentation
* Neo4j API Documentation

== Installation

Neo4j exists as a gem at rubyforge
Expand All @@ -30,7 +36,7 @@ To install from the source:
JRuby version 1.1.4 does not work with Neo4j.rb because of a JRuby bug.
This bug is fixed in JRuby 1.1.5.

== Two Minute Tutorial
== Ten Minute Tutorial

=== Creating a model

Expand Down Expand Up @@ -183,6 +189,209 @@ or

More information about neo4j can be found after the Lucene section below.

== Ruby on Rails

Neo4j.rb does work nicely with R&R.

It has been verified on rail 2.2.2, JRuby 1.1.6 RC1, Glassfish 0.9.1.

=== Configuration

==== Install Neo4j.rb
gem install neo4j

==== Install rails
gem install rails

==== Create a rails project, movies
rails movies

==== Config rails
Config rails to use Neo4j.rb instead of ActiveRecord, edit movies/config/environment.rb
environment.rb:
config.frameworks -= [ :active_record ] #, :active_resource, :action_mailer ]
config.gem "neo4j", :version => "0.0.7"

==== Create Models
Create model in movies/app/models
actor.rb:

class Role
include Neo4j::RelationMixin
properties :title, :character
end

class Actor
include Neo4j::NodeMixin
properties :name, :phone, :salary
has_n(:acted_in).to(Movie).relation(Role)
index :name
end

movie.rb:
class Movie

include Neo4j::NodeMixin
properties :title
properties :year

# defines a method for traversing incoming acted_in relationships from Actor
has_n(:actors).from(Actor, :acted_in)
end

==== Create RESTful routes
Edit the config/routes.rb file

ActionController::Routing::Routes.draw do |map|
map.resources :actors do |actor|
actor.resources :acted_in#, :controller => 'movies'
actor.resource :movies, :controller => 'acted_in'
end

==== Create Controllers

Add the following controllers in app/controllers

actors_controller.rb:

class ActorsController < ApplicationController
before_filter :find_actor, :only => [:show, :edit, :update, :destroy]

def index
@actors = Actor.all.nodes
end

def create
@actor = Actor.new
@actor.update(params[:actor])
flash[:notice] = 'Actor was successfully created.'
redirect_to(@actor)
end

def update
@actor.update(params[:actor])
flash[:notice] = 'Actor was successfully updated.'
redirect_to(@actor)
end

def destroy
@actor.delete
redirect_to(actors_url)
end


def edit
end

def show
end

def new
@actor = Actor.value_object.new
end

private
def find_actor
@actor = Neo4j.load(params[:id])
end
end


acted_in_controller.rb:

class ActedInController < ApplicationController
def index
@actor = Neo4j.load(params[:actor_id])
@movies = @actor.acted_in.nodes
end

def create
@actor = Neo4j.load(params[:actor_id])
@movie = Movie.new
@movie.update(params[:movie])
@actor.acted_in << @movie
flash[:notice] = 'Movie was successfully created.'
redirect_to(@actor)
end

def update
@actor = Neo4j.load(params[:actor_id])
@movie = Movie.new
@movie.update(params[:movie])
@actor.acted_in.new @movie
@movie.update(params[:movie])
flash[:notice] = 'Movie was successfully updated.'
redirect_to(@movie)
end

def show
@movie = Neo4j.load(params[:id])
end

def new
@actor = Neo4j.load(params[:actor_id])
@movie = Movie.value_object.new
end

def edit
@movie = Neo4j.load(params[:id])
end

end

==== Add views

Add the following views in app/views/actors
index.html.erb:

<h1>Listing actors</h1>

<table>
<tr>
<th>Name</th>
</tr>

<% for actor in @actors %>
<tr>
<td><%=h actor.name %></td>
<td><%= link_to 'Edit', edit_actor_path(actor) %></td>
<td><%= link_to 'Show', actor %></td>
<td><%= link_to 'Destroy', actor, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New actor', new_actor_path %>

new.html.erb:

<h1>New Actor</h1>

<% form_for(@actor) do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :salary%><br />
<%= f.text_field :salary %>
</p>
<p>
<%= f.submit "Update" %>
</p>

<% end %>



<%= link_to 'Back', actors_path %>

== The Lucene Module

You can use this module without using the Neo4j module.
Expand Down
3 changes: 1 addition & 2 deletions lib/neo4j.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
require 'neo4j/neo'
require 'neo4j/reference_node'
require 'neo4j/transaction'
require 'neo4j/version'
require 'neo4j/search_result'

require 'neo4j/version'



Expand Down
Loading

0 comments on commit d9cfedc

Please sign in to comment.