forked from indirect/rails3-generators
/
datamapper.rb
80 lines (67 loc) · 1.81 KB
/
datamapper.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
require 'rails/generators/named_base'
require 'rails/generators/migration'
require 'rails/generators/active_model'
module Datamapper
module Generators
class Base < Rails::Generators::NamedBase #:nodoc:
include Rails::Generators::Migration
def self.source_root
@_datamapper_source_root ||= File.expand_path(File.join(File.dirname(__FILE__),
'datamapper', generator_name, 'templates'))
end
protected
# Datamapper does not care if migrations have the same name as long as
# they have different ids.
#
def migration_exists?(dirname, file_name) #:nodoc:
false
end
# Implement the required interface for Rails::Generators::Migration.
#
def next_migration_number(dirname) #:nodoc:
if options[:id]
"%.3d" % options[:id]
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end
end
class ActiveModel < Rails::Generators::ActiveModel #:nodoc:
def self.all(klass)
"#{klass}.all"
end
def self.find(klass, params=nil)
"#{klass}.get(#{params})"
end
def self.build(klass, params=nil)
if params
"#{klass}.new(#{params})"
else
"#{klass}.new"
end
end
def save
"#{name}.save"
end
def update_attributes(params=nil)
"#{name}.update(#{params})"
end
def errors
"#{name}.errors"
end
def destroy
"#{name}.destroy"
end
end
end
end
module Rails
module Generators
class GeneratedAttribute #:nodoc:
def type_class
return 'DateTime' if type.to_s == 'datetime'
return type.to_s.camelcase
end
end
end
end