/
model_generator.rb
82 lines (64 loc) · 1.8 KB
/
model_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require File.join(File.dirname(__FILE__), '..', '..', '..', '..','neo4j.rb')
class Neo4j::Generators::ModelGenerator < Neo4j::Generators::Base #:nodoc:
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
check_class_collision
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_many, :type => :array, :desc => "A list of has_many relationships"
def create_model_file
template "model.erb", File.join('app/models', "#{singular_name}.rb")
end
protected
def migration?
false
end
def timestamps?
options[:timestamps]
end
def has_many?
options[:has_many]
end
def has_many_statements
txt = ""
options[:has_many].each do |key|
txt << has_x('has_many', key)
end
txt
end
def has_one?
options[:has_one]
end
def has_x(method, key)
to, from = key.split(':')
(from ? "\n #{method}(:#{to}).from(:#{from})\n" : "\n #{method} :#{to}")
end
def has_one_statements
txt = ""
options[:has_one].each do |key|
txt << has_x('has_one', key)
end
txt
end
def indices?
options[:indices]
end
def index_fragment(property)
if options[:indices] && options[:indices].include?(property)
"index :#{property}"
end
end
def parent?
options[:parent]
end
def timestamp_statements
%q{
property :created_at, type: DateTime
# property :created_on, type: Date
property :updated_at, type: DateTime
# property :updated_on, type: Date
}
end
hook_for :test_framework
end