Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark/neoid with json field support #28

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

[![Build Status](https://secure.travis-ci.org/elado/neoid.png)](http://travis-ci.org/elado/neoid)

## This fork only

This fork was made so that neoid supports a JSON type field in the database.
This 's based on PostgreSQL 9.2 and Rails 4.0.

You need to have a PostgreSQL db availale (as PG can't be run in memory like SQLite3).
Currently, I'm manually dropping/creating the db everytime I run the test, which is time-consuming.

# Reame


Make your ActiveRecords stored and searchable on Neo4j graph database, in order to make fast graph queries that MySQL would crawl while doing them.

Expand Down Expand Up @@ -85,7 +95,7 @@ Then, you can customize what fields will be saved on the node in Neo4j, inside `
```ruby
class User < ActiveRecord::Base
include Neoid::Node

neoidable do |c|
c.field :slug
c.field :display_name
Expand Down Expand Up @@ -182,7 +192,7 @@ If you'd like to save nodes manually rather than after_save, use `auto_index: fa
```ruby
class User < ActiveRecord::Base
include Neoid::Node

neoidable auto_index: false do |c|
end
end
Expand Down
17 changes: 13 additions & 4 deletions lib/neoid/model_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Neoid
module ModelAdditions
module ClassMethods
attr_reader :neoid_config

def neoid_config
@neoid_config ||= Neoid::ModelConfig.new(self)
end

def neoidable(options = {})
# defaults
neoid_config.auto_index = true
Expand All @@ -25,7 +25,7 @@ def neo_model_index_name
@index_name ||= "#{self.name.tableize}_index"
end
end

module InstanceMethods
def to_neo
if self.class.neoid_config.stored_fields
Expand All @@ -35,10 +35,19 @@ def to_neo
else
self.send(field) rescue (raise "No field #{field} for #{self.class.name}")
end

all
end

# optional: if there are fields declared with json_field, merge their hash content to result
if self.class.neoid_config.stored_json_fields
json_fields_content = self.class.neoid_config.stored_json_fields.inject({}) do |all, field|
json_data = self.send(field)
all.merge(json_data)
end
hash.merge!(json_fields_content)
end

hash.reject { |k, v| v.nil? }
else
{}
Expand Down
28 changes: 18 additions & 10 deletions lib/neoid/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,42 @@ class ModelConfig
attr_reader :relationship_options
attr_accessor :enable_model_index
attr_accessor :auto_index

def initialize(klass)
@klass = klass
end

def stored_fields
@stored_fields ||= {}
end

def field(name, &block)
self.stored_fields[name] = block
end


def stored_json_fields
@stored_json_fields ||= []
end

def json_field(name)
self.stored_json_fields << name
end

def relationship(options)
@relationship_options = options
end

def search(&block)
raise "search needs a block" unless block_given?
@search_options = SearchConfig.new
block.(@search_options)
end

def inspect
"#<Neoid::ModelConfig @properties=#{properties.inspect} @search_options=#{@search_options.inspect}>"
end
end

class SearchConfig
def index_fields
@index_fields ||= {}
Expand All @@ -41,15 +49,15 @@ def index_fields
def fulltext_fields
@fulltext_fields ||= {}
end

def index(field, options = {}, &block)
index_fields[field] = options.merge(block: block)
end

def fulltext(field, options = {}, &block)
fulltext_fields[field] = options.merge(block: block)
end

def inspect
"#<Neoid::SearchConfig @index_fields=#{index_fields.inspect} @fulltext_fields=#{fulltext_fields.inspect}>"
end
Expand Down
10 changes: 5 additions & 5 deletions lib/neoid/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ def neo_search(term, options = {})
Neoid.search(self, term, options)
end
end

module InstanceMethods
def neo_find_by_id
# Neoid::logger.info "Node#neo_find_by_id #{self.class.neo_index_name} #{self.id}"
node = Neoid.db.get_node_auto_index(Neoid::UNIQUE_ID_KEY, self.neo_unique_id)
node.present? ? Neoid::Node.from_hash(node[0]) : nil
end

def _neo_save
return unless Neoid.enabled?

Expand Down Expand Up @@ -174,11 +174,11 @@ def neo_helper_get_field_value(field, options = {})
self.send(field) rescue (raise "No field #{field} for #{self.class.name}")
end
end

def neo_load(hash)
Neoid::Node.from_hash(hash)
end

def neo_node
_neo_representation
end
Expand All @@ -199,7 +199,7 @@ def neo_after_relationship_through_remove(record)
@__neo_temp_rels.delete(record)
end
end

def self.included(receiver)
receiver.send :include, Neoid::ModelAdditions
receiver.extend ClassMethods
Expand Down
7 changes: 6 additions & 1 deletion spec/neoid/model_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
Article.neoid_config.stored_fields[:title_length].should be_a(Proc)
end

it "should store stored json fields" do
NodeWithJson.neoid_config.stored_json_fields.should_not be_nil
NodeWithJson.neoid_config.stored_json_fields.should include(:data)
end

it "should store stored fields based on blocks" do
article = Article.create! title: "Hello", year: 2012

article.neo_node.title_length.should == article.title.length
end
end
Expand Down
20 changes: 15 additions & 5 deletions spec/neoid/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
user = User.create!(name: "Elad Ossadon", slug: "elado")

user.neo_node.should_not be_nil

user.neo_node.ar_id.should == user.id
user.neo_node.name.should == user.name
user.neo_node.slug.should == user.slug
Expand All @@ -22,11 +22,21 @@
movie = Movie.create!(name: "Memento", slug: "memento-1999", year: 1999)

movie.neo_node.should_not be_nil

movie.neo_node.ar_id.should == movie.id
movie.neo_node.name.should == movie.name
movie.neo_node.year.should == movie.year
end

it "should create a neo_node for node with json field" do
node = NodeWithJson.create!(data: {key1: "value1", key2: 2}, node_type: "value 3")

node.neo_node.should_not be_nil
node.neo_node.ar_id.should == node.id
node.neo_node.node_type.should == "value 3"
node.neo_node.key1.should == "value1"
node.neo_node.key2.should == 2
end
end

context "update graph nodes" do
Expand All @@ -51,7 +61,7 @@
context "find by id" do
it "should find a neo_node for user" do
user = User.create!(name: "Elad Ossadon", slug: "elado")

user.neo_node.should_not be_nil
user.neo_find_by_id.should_not be_nil
end
Expand All @@ -70,7 +80,7 @@
old, Neoid.config.enable_subrefs = Neoid.config.enable_subrefs, true

Neoid.send(:initialize_subrefs)

begin
Neoid.ref_node.rel(:outgoing, :users_subref).should_not be_nil
ensure
Expand All @@ -82,7 +92,7 @@
old, Neoid.config.enable_subrefs = Neoid.config.enable_subrefs, true

Neoid.send(:initialize_subrefs)

begin
user = User.create!(name: "Elad")
user.neo_node.rel(:incoming, :users).should_not be_nil
Expand Down
18 changes: 15 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'neoid'
require 'pg'
require 'active_record'
require 'neography'
require 'rest-client'
require 'pg'

# ENV['NEOID_LOG'] = 'true'

Expand All @@ -21,9 +23,19 @@

Neoid.db = $neo

# establish a connection to sqlite3 (in memory)
# logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, Logger.new('/dev/null')
# ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'support/database.yml')))
# ActiveRecord::Base.establish_connection('sqlite3')

# establish connection to a PostgreSQL db (persistent)
logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, Logger.new('/dev/null')
ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'support/database.yml')))
ActiveRecord::Base.establish_connection('sqlite3')
adapter = 'postgresql'
db_config = YAML.load(IO.read(File.join(File.dirname(__FILE__), 'support/database.yml')))[adapter]
ActiveRecord::Base.establish_connection(db_config)
# config = ActiveRecord::Base.connection.pool.spec.config



require 'support/schema'
require 'support/models'
Expand All @@ -35,7 +47,7 @@

config.before(:all) do
end

config.before(:each) do
Neoid.node_models.each(&:destroy_all)
Neoid.clean_db(:yes_i_am_sure)
Expand Down
13 changes: 12 additions & 1 deletion spec/support/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@ sqlite3:
database: ":memory:"
encoding: utf8
charset: utf8
timeout: 5000
timeout: 5000

postgresql:
adapter: postgresql
database: neoid_in_development
encoding: utf8
charset: utf8
timeout: 5000
pool: 5
username: postgres
password: password
host: localhost
Loading