Skip to content
Permalink
Browse files

Generators can now generate relationships as well [#195 state:resolved]

  • Loading branch information...
andreasronge committed Nov 1, 2011
1 parent b498bf5 commit b1f9ae9ba9ae8c3c54f082580f0125bae06d2058
@@ -2,63 +2,71 @@
require 'rails/generators/active_model'

module Neo4j
module Generators #:nodoc:
end
module Generators #:nodoc:
end
end

class Neo4j::Generators::Base < Rails::Generators::NamedBase #:nodoc:
def self.source_root
@_neo4j_source_root ||= File.expand_path(File.join(File.dirname(__FILE__),
'neo4j', generator_name, 'templates'))
end
def self.source_root
@_neo4j_source_root ||= File.expand_path(File.join(File.dirname(__FILE__),
'neo4j', generator_name, 'templates'))
end
end

class Neo4j::Generators::ActiveModel < Rails::Generators::ActiveModel #:nodoc:
def self.all(klass)
"#{klass}.all"
end
def self.all(klass)
"#{klass}.all"
end

def self.find(klass, params=nil)
"#{klass}.find(#{params})"
end
def self.find(klass, params=nil)
"#{klass}.find(#{params})"
end

def self.build(klass, params=nil)
if params
"#{klass}.new(#{params})"
else
"#{klass}.new"
end
end
def self.build(klass, params=nil)
if params
"#{klass}.new(#{params})"
else
"#{klass}.new"
end
end

def save
"#{name}.save"
end
def save
"#{name}.save"
end

def update_attributes(params=nil)
"#{name}.update_attributes(#{params})"
end
def update_attributes(params=nil)
"#{name}.update_attributes(#{params})"
end

def errors
"#{name}.errors"
end
def errors
"#{name}.errors"
end

def destroy
"#{name}.destroy"
end
def destroy
"#{name}.destroy"
end
end

module Rails
module Generators
class GeneratedAttribute #:nodoc:
def type_class
case type.to_s.downcase
when 'datetime' then 'DateTime'
when 'date' then 'Date'
when 'text' then 'String'
when 'integer', 'number', 'fixnum' then 'Fixnum'
when 'float' then 'Float'
else 'String'
end
case type.to_s.downcase
when 'any' then
'any'
when 'datetime' then
'DateTime'
when 'date' then
'Date'
when 'text' then
'String'
when 'integer', 'number', 'fixnum' then
'Fixnum'
when 'float' then
'Float'
else
'String'
end
end
end
end
@@ -7,7 +7,10 @@ class Neo4j::Generators::ModelGenerator < Neo4j::Generators::Base #:nodoc:

class_option :timestamps, :type => :boolean
class_option :parent, :type => :string, :desc => "The parent class for the generated model"

class_option :indices, :type => :array, :desc => "The properties which should be indexed"
class_option :has_one, :type => :array, :desc => "A list of has_one relationships"
class_option :has_n, :type => :array, :desc => "A list of has_n relationships"

def create_model_file
template "model.erb", File.join('app/models', "#{singular_name}.rb")
end
@@ -20,7 +23,47 @@ def migration?
def timestamps?
options[:timestamps]
end


def has_n?
options[:has_n]
end

def has_n_statements
txt = ""
options[:has_n].each do |key|
to, from = key.split(':')
txt << (from ? "\n has_n(:#{to}).from(:#{from})\n" : "\n has_n :#{to}")
end
txt
end

def has_one?
options[:has_one]
end

def has_one_statements
txt = ""
options[:has_one].each do |key|
to, from = key.split(':')
txt << (from ? "\n has_one(:#{to}).from(:#{from})\n" : "\n has_one :#{to}")
end
txt
end

def indices?
options[:indices]
end

def indices_statements
puts "indices_statements #{options[:indices].inspect}"
txt = ""
options[:indices].each do |key|
txt << %Q{
index :#{key}}
end
txt
end

def parent?
options[:parent]
end
@@ -1,7 +1,11 @@
class <%= class_name %> < <%= parent? ? options[:parent].classify : "Neo4j::Rails::Model" %>
<% attributes.each do |attribute| -%>
property :<%= attribute.name %><%= ", :type => #{attribute.type_class}" %>
property :<%= attribute.name %><%= ", :type => #{attribute.type_class}" unless attribute.type_class == 'any' %>
<% end -%>
<%= indices_statements if indices? -%>

<%= has_n_statements if has_n? -%>
<%= has_one_statements if has_one? -%>

<%= timestamp_statements if timestamps? -%>
end

0 comments on commit b1f9ae9

Please sign in to comment.
You can’t perform that action at this time.