-
Notifications
You must be signed in to change notification settings - Fork 510
/
activerecord.rb
185 lines (158 loc) · 5.6 KB
/
activerecord.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
AR = (<<-AR) unless defined?(AR)
##
# You can use other adapters like:
#
# ActiveRecord::Base.configurations[:development] = {
# :adapter => 'mysql2',
# :encoding => 'utf8',
# :reconnect => true,
# :database => 'your_database',
# :pool => 5,
# :username => 'root',
# :password => '',
# :host => 'localhost',
# :socket => '/tmp/mysql.sock'
# }
#
ActiveRecord::Base.configurations[:development] = {
!DB_DEVELOPMENT!
}
ActiveRecord::Base.configurations[:production] = {
!DB_PRODUCTION!
}
ActiveRecord::Base.configurations[:test] = {
!DB_TEST!
}
# Setup our logger
ActiveRecord::Base.logger = logger
# Raise exception on mass assignment protection for Active Record models.
ActiveRecord::Base.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
ActiveRecord::Base.auto_explain_threshold_in_seconds = 0.5
# Include Active Record class name as root for JSON serialized output.
ActiveRecord::Base.include_root_in_json = false
# Store the full class name (including module namespace) in STI type column.
ActiveRecord::Base.store_full_sti_class = true
# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true
# Don't escape HTML entities in JSON, leave that for the #json_escape helper
# if you're including raw JSON in an HTML page.
ActiveSupport.escape_html_entities_in_json = false
# Now we can establish connection with our db.
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
AR
MYSQL = (<<-MYSQL) unless defined?(MYSQL)
:adapter => 'mysql',
:encoding => 'utf8',
:reconnect => true,
:database => !DB_NAME!,
:pool => 5,
:username => 'root',
:password => '',
:host => 'localhost',
:socket => '/tmp/mysql.sock'
MYSQL
MYSQL2 = (<<-MYSQL2) unless defined?(MYSQL2)
:adapter => 'mysql2',
:encoding => 'utf8',
:reconnect => true,
:database => !DB_NAME!,
:pool => 5,
:username => 'root',
:password => '',
:host => 'localhost',
:socket => '/tmp/mysql.sock'
MYSQL2
POSTGRES = (<<-POSTGRES) unless defined?(POSTGRES)
:adapter => 'postgresql',
:database => !DB_NAME!,
:username => 'root',
:password => '',
:host => 'localhost',
:port => 5432
POSTGRES
SQLITE = (<<-SQLITE) unless defined?(SQLITE)
:adapter => 'sqlite3',
:database => !DB_NAME!
SQLITE
def setup_orm
ar = AR
db = @project_name.underscore
# We're now defaulting to mysql2 since mysql is deprecated
case options[:adapter]
when 'mysql-gem'
ar.gsub! /!DB_DEVELOPMENT!/, MYSQL.gsub(/!DB_NAME!/,"'#{db}_development'")
ar.gsub! /!DB_PRODUCTION!/, MYSQL.gsub(/!DB_NAME!/,"'#{db}_production'")
ar.gsub! /!DB_TEST!/, MYSQL.gsub(/!DB_NAME!/,"'#{db}_test'")
require_dependencies 'mysql', :version => "~> 2.8.1"
when 'mysql', 'mysql2'
ar.gsub! /!DB_DEVELOPMENT!/, MYSQL2.gsub(/!DB_NAME!/,"'#{db}_development'")
ar.gsub! /!DB_PRODUCTION!/, MYSQL2.gsub(/!DB_NAME!/,"'#{db}_production'")
ar.gsub! /!DB_TEST!/, MYSQL2.gsub(/!DB_NAME!/,"'#{db}_test'")
require_dependencies 'mysql2'
when 'postgres'
ar.gsub! /!DB_DEVELOPMENT!/, POSTGRES.gsub(/!DB_NAME!/,"'#{db}_development'")
ar.gsub! /!DB_PRODUCTION!/, POSTGRES.gsub(/!DB_NAME!/,"'#{db}_production'")
ar.gsub! /!DB_TEST!/, POSTGRES.gsub(/!DB_NAME!/,"'#{db}_test'")
require_dependencies 'pg'
else
ar.gsub! /!DB_DEVELOPMENT!/, SQLITE.gsub(/!DB_NAME!/,"Padrino.root('db', '#{db}_development.db')")
ar.gsub! /!DB_PRODUCTION!/, SQLITE.gsub(/!DB_NAME!/,"Padrino.root('db', '#{db}_production.db')")
ar.gsub! /!DB_TEST!/, SQLITE.gsub(/!DB_NAME!/,"Padrino.root('db', '#{db}_test.db')")
require_dependencies 'sqlite3'
end
require_dependencies 'activerecord', :require => 'active_record', :version => ">= 3.1"
insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement'
create_file("config/database.rb", ar)
end
AR_MODEL = (<<-MODEL) unless defined?(AR_MODEL)
class !NAME! < ActiveRecord::Base
end
MODEL
# options => { :fields => ["title:string", "body:string"], :app => 'app' }
def create_model_file(name, options={})
model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
model_contents = AR_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
create_file(model_path, model_contents,:skip => true)
end
AR_MIGRATION = (<<-MIGRATION) unless defined?(AR_MIGRATION)
class !FILECLASS! < ActiveRecord::Migration
def self.up
!UP!
end
def self.down
!DOWN!
end
end
MIGRATION
AR_MODEL_UP_MG = (<<-MIGRATION).gsub(/^/,' ') unless defined?(AR_MODEL_UP_MG)
create_table :!TABLE! do |t|
!FIELDS!
t.timestamps
end
MIGRATION
AR_MODEL_DOWN_MG = (<<-MIGRATION) unless defined?(AR_MODEL_DOWN_MG)
drop_table :!TABLE!
MIGRATION
def create_model_migration(migration_name, name, columns)
output_model_migration(migration_name, name, columns,
:base => AR_MIGRATION,
:column_format => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
:up => AR_MODEL_UP_MG,
:down => AR_MODEL_DOWN_MG
)
end
AR_CHANGE_MG = (<<-MIGRATION).gsub(/^/, ' ') unless defined?(AR_CHANGE_MG)
change_table :!TABLE! do |t|
!COLUMNS!
end
MIGRATION
def create_migration_file(migration_name, name, columns)
output_migration_file(migration_name, name, columns,
:base => AR_MIGRATION,
:change_format => AR_CHANGE_MG,
:add => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
:remove => Proc.new { |field, kind| "t.remove :#{field}" }
)
end