ByBrian Underwood, Neo4j Advocate | October 30, 2015
For a while now, I’ve been building various Neo4j.rb educational resources using the example of an asset portal .
There have been:A screencast series (thefirst half and asecond half) A SitePoint article on building recommendations and access control A webinar on advanced access control Anintroductory course for Neo4j using Ruby
As part of this process, I’ve wanted to use what I’ve been building and allow anybody to easily create a UI for their own assets in Rails. I’m pleased to say that I’ve got a good start with the graph_starter gem .
The graph_starter gem is a Rails engine, which means that it can be placed within a Neo4j Rails application. The goal is to be able to quickly set up a basic UI for your entities, but to also be able to override it when you want to provide custom logic.
Setting up a graph_starter application is as simple as the following steps:Installation
Using graph_starter is easy. First, create a Rails application if you don’t already have one:rails new application_name
Include the graph_starter gem (it will include the Neo4j.rb gems for you):# Gemfilegem 'graph_starter'
Then, mount the engine:# config/routes.rbmount GraphStarter::Engine => '/'
Next, create some asset models:# app/models/product.rbclass Product < GraphStarter::Asset # `title` property is added automatically property :name property :description property :price, type: Integer has_images has_one :in, :vendor, type: :SELLS_PRODUCTend# app/models/vendor.rbclass Vendor < GraphStarter::Asset property :brand_name property :code name_property :brand_name has_many :out, :products, origin: :vendorend
These models are simply Neo4j.rb ActiveNode models so you can refer to the Neo4j.rb documentation to define them. They do have some special methods, however, which let you control how GraphStarter works.
In the above Product model, for example, has_images has been called to indicate that products have images which defines a separate Image model along with the necessary association.
See the graph_starter README for documentation on how to configure aspects of your models.Importing Data
Once that framework is in place, you can define a way to import data, if desired. In this case, I would suggest making a rake task:# lib/tasks/store.rakenamespace :store do task :import do CSV.open(File.read('vendors.csv')).each do |row| Vendor.create(name: row['brand_name'], code: row['code']) end CSV.open(File.read('products.csv')).each do |row| product = Product.create(name: row['name'], description: row['description'], price: row['price'].to_i) product.vendor = Vendor.find_by(code: row['vendor_code']) end endend
And that’s all!
When everything is in place, you can simply start up your Rails server (by running rails server ) and you get a UI which looks like this example site I made using data from the Natural History Museum in London:
You can browse the app on Heroku and checkout the repository on GitHub
I’ll be working on a new project to create aGraphGist portal based on the graph_starter gem so I plan to continue improving it!
Need more tips on how to effectively use Neo4j in your next Neo4j Rails app? Register for our online training class, Neo4j in Production , and learn how master the world’s leading graph database.
Sign Me Up