padrino-gen 0.16.0.pre4 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/bin/padrino-gen +4 -4
- data/lib/padrino-gen/command.rb +2 -2
- data/lib/padrino-gen/generators/actions.rb +68 -64
- data/lib/padrino-gen/generators/app/app.rb.tt +6 -6
- data/lib/padrino-gen/generators/app.rb +19 -19
- data/lib/padrino-gen/generators/cli.rb +16 -19
- data/lib/padrino-gen/generators/component.rb +18 -19
- data/lib/padrino-gen/generators/components/actions.rb +42 -42
- data/lib/padrino-gen/generators/components/mocks/mocha.rb +7 -7
- data/lib/padrino-gen/generators/components/mocks/rr.rb +7 -7
- data/lib/padrino-gen/generators/components/orms/activerecord.rb +158 -156
- data/lib/padrino-gen/generators/components/orms/couchrest.rb +33 -33
- data/lib/padrino-gen/generators/components/orms/datamapper.rb +108 -103
- data/lib/padrino-gen/generators/components/orms/dynamoid.rb +40 -40
- data/lib/padrino-gen/generators/components/orms/minirecord.rb +114 -114
- data/lib/padrino-gen/generators/components/orms/mongoid.rb +65 -65
- data/lib/padrino-gen/generators/components/orms/mongomapper.rb +20 -20
- data/lib/padrino-gen/generators/components/orms/mongomatic.rb +63 -59
- data/lib/padrino-gen/generators/components/orms/ohm.rb +49 -47
- data/lib/padrino-gen/generators/components/orms/ripple.rb +45 -45
- data/lib/padrino-gen/generators/components/orms/sequel.rb +51 -46
- data/lib/padrino-gen/generators/components/renderers/erb.rb +1 -1
- data/lib/padrino-gen/generators/components/renderers/liquid.rb +1 -1
- data/lib/padrino-gen/generators/components/scripts/dojo.rb +6 -6
- data/lib/padrino-gen/generators/components/scripts/extcore.rb +6 -6
- data/lib/padrino-gen/generators/components/scripts/jquery.rb +6 -6
- data/lib/padrino-gen/generators/components/scripts/mootools.rb +6 -6
- data/lib/padrino-gen/generators/components/scripts/prototype.rb +8 -8
- data/lib/padrino-gen/generators/components/stylesheets/compass.rb +1 -1
- data/lib/padrino-gen/generators/components/stylesheets/less.rb +4 -4
- data/lib/padrino-gen/generators/components/tests/bacon.rb +66 -66
- data/lib/padrino-gen/generators/components/tests/cucumber.rb +61 -61
- data/lib/padrino-gen/generators/components/tests/minitest.rb +70 -70
- data/lib/padrino-gen/generators/components/tests/rspec.rb +74 -74
- data/lib/padrino-gen/generators/components/tests/shoulda.rb +72 -72
- data/lib/padrino-gen/generators/components/tests/testunit.rb +69 -69
- data/lib/padrino-gen/generators/controller.rb +16 -17
- data/lib/padrino-gen/generators/helper.rb +8 -9
- data/lib/padrino-gen/generators/mailer.rb +8 -9
- data/lib/padrino-gen/generators/migration.rb +6 -6
- data/lib/padrino-gen/generators/model.rb +20 -21
- data/lib/padrino-gen/generators/plugin.rb +9 -9
- data/lib/padrino-gen/generators/project/config/apps.rb.tt +4 -4
- data/lib/padrino-gen/generators/project/config/boot.rb +2 -2
- data/lib/padrino-gen/generators/project/config.ru +1 -1
- data/lib/padrino-gen/generators/project.rb +49 -51
- data/lib/padrino-gen/generators/runner.rb +25 -24
- data/lib/padrino-gen/generators/task.rb +7 -8
- data/lib/padrino-gen/generators/templates/Gemfile.tt +5 -5
- data/lib/padrino-gen/generators/templates/controller.rb.tt +3 -3
- data/lib/padrino-gen/generators/templates/mailer.rb.tt +15 -15
- data/lib/padrino-gen/generators/templates/project_bin.tt +1 -1
- data/lib/padrino-gen/generators/templates/task.rb.tt +1 -1
- data/lib/padrino-gen/padrino-tasks/activerecord.rb +152 -160
- data/lib/padrino-gen/padrino-tasks/database.rb +2 -2
- data/lib/padrino-gen/padrino-tasks/datamapper.rb +31 -31
- data/lib/padrino-gen/padrino-tasks/minirecord.rb +3 -3
- data/lib/padrino-gen/padrino-tasks/mongoid.rb +39 -41
- data/lib/padrino-gen/padrino-tasks/mongomapper.rb +18 -14
- data/lib/padrino-gen/padrino-tasks/sequel.rb +28 -28
- data/lib/padrino-gen/padrino-tasks/sql-helpers.rb +45 -45
- data/lib/padrino-gen.rb +4 -4
- data/padrino-gen.gemspec +19 -19
- data/test/fixtures/admin_template.rb +5 -5
- data/test/fixtures/database_template.rb +19 -22
- data/test/fixtures/example_template.rb +8 -8
- data/test/fixtures/git_template.rb +2 -2
- data/test/fixtures/plugin_template.rb +3 -3
- data/test/fixtures/rake_template.rb +5 -5
- data/test/helper.rb +23 -21
- data/test/test_app_generator.rb +13 -13
- data/test/test_cli.rb +4 -5
- data/test/test_component_generator.rb +17 -17
- data/test/test_controller_generator.rb +62 -63
- data/test/test_generator.rb +6 -6
- data/test/test_helper_generator.rb +23 -24
- data/test/test_mailer_generator.rb +7 -7
- data/test/test_migration_generator.rb +31 -31
- data/test/test_model_generator.rb +80 -80
- data/test/test_plugin_generator.rb +35 -36
- data/test/test_project_generator.rb +171 -169
- data/test/test_sql_helpers.rb +49 -50
- data/test/test_task_generator.rb +13 -13
- metadata +10 -10
|
@@ -13,21 +13,21 @@ module Padrino
|
|
|
13
13
|
# Array of column names and property type.
|
|
14
14
|
# @param [Hash] options
|
|
15
15
|
# Additional migration options, e.g
|
|
16
|
-
# { :
|
|
17
|
-
# :
|
|
16
|
+
# { base: '....text...', up: '..text...',
|
|
17
|
+
# down: '..text...', column_format: "t.column :#{field}, :#{kind}" }
|
|
18
18
|
# @example
|
|
19
|
-
# output_model_migration(
|
|
20
|
-
# :
|
|
21
|
-
# :
|
|
22
|
-
# :
|
|
19
|
+
# output_model_migration('AddPerson', 'person', ["name:string", "age:integer"],
|
|
20
|
+
# base: AR_MIGRATION,
|
|
21
|
+
# column_format: Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
|
|
22
|
+
# up: AR_MODEL_UP_MG, down: AR_MODEL_DOWN_MG)
|
|
23
23
|
#
|
|
24
|
-
def output_model_migration(filename, name, columns, options={})
|
|
24
|
+
def output_model_migration(filename, name, columns, options = {})
|
|
25
25
|
if behavior == :revoke
|
|
26
26
|
remove_migration(filename)
|
|
27
27
|
else
|
|
28
28
|
return if migration_exist?(filename)
|
|
29
29
|
model_name = name.to_s.pluralize
|
|
30
|
-
field_tuples = columns.map { |value| value.split(
|
|
30
|
+
field_tuples = columns.map { |value| value.split(':') }
|
|
31
31
|
field_tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
|
|
32
32
|
column_declarations = field_tuples.map(&options[:column_format]).join("\n ")
|
|
33
33
|
contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, options[:up]).gsub(/!DOWN!\n/m, options[:down])
|
|
@@ -35,8 +35,8 @@ module Padrino
|
|
|
35
35
|
contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.underscore.camelize)
|
|
36
36
|
migration_number = current_migration_number
|
|
37
37
|
contents = contents.gsub(/!FIELDS!/, column_declarations).gsub(/!VERSION!/, migration_number)
|
|
38
|
-
migration_filename = "#{format(
|
|
39
|
-
create_file(destination_root('db/migrate/', migration_filename), contents, :
|
|
38
|
+
migration_filename = "#{format('%03d', migration_number)}_#{filename.underscore}.rb"
|
|
39
|
+
create_file(destination_root('db/migrate/', migration_filename), contents, skip: true)
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
|
|
@@ -51,17 +51,17 @@ module Padrino
|
|
|
51
51
|
# Array of column names and property type.
|
|
52
52
|
# @param [Hash] options
|
|
53
53
|
# Additional migration options, e.g
|
|
54
|
-
# {
|
|
55
|
-
# :
|
|
56
|
-
# :
|
|
54
|
+
# { base:..text...", change_format: "...text...",
|
|
55
|
+
# add: proc { |field, kind| "add_column :#{table_name}, :#{field}, :#{kind}" },
|
|
56
|
+
# remove: proc { |field, kind| "remove_column :#{table_name}, :#{field}" }
|
|
57
57
|
# @example
|
|
58
58
|
# output_migration_file(migration_name, name, columns,
|
|
59
|
-
# :
|
|
60
|
-
# :
|
|
61
|
-
# :
|
|
59
|
+
# base: AR_MIGRATION, change_format: AR_CHANGE_MG,
|
|
60
|
+
# add: Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
|
|
61
|
+
# remove: Proc.new { |field, kind| "t.remove :#{field}" }
|
|
62
62
|
# )
|
|
63
63
|
#
|
|
64
|
-
def output_migration_file(filename, name, columns, options={})
|
|
64
|
+
def output_migration_file(filename, name, columns, options = {})
|
|
65
65
|
if behavior == :revoke
|
|
66
66
|
remove_migration(name)
|
|
67
67
|
else
|
|
@@ -69,7 +69,7 @@ module Padrino
|
|
|
69
69
|
change_format = options[:change_format]
|
|
70
70
|
migration_scan = filename.underscore.camelize.scan(/(Add|Remove).*?(?:(?:To|From).+?)*(?:To|From)((?:To|From)?.*?)$/).flatten
|
|
71
71
|
direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any?
|
|
72
|
-
tuples = direction ? columns.map { |value| value.split(
|
|
72
|
+
tuples = direction ? columns.map { |value| value.split(':') } : []
|
|
73
73
|
tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
|
|
74
74
|
add_columns = tuples.map(&options[:add]).join("\n ")
|
|
75
75
|
remove_columns = tuples.map(&options[:remove]).join("\n ")
|
|
@@ -80,8 +80,8 @@ module Padrino
|
|
|
80
80
|
contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.underscore.camelize)
|
|
81
81
|
migration_number = current_migration_number
|
|
82
82
|
contents.gsub!(/!VERSION!/, migration_number)
|
|
83
|
-
migration_filename = "#{format(
|
|
84
|
-
create_file(destination_root('db/migrate/', migration_filename), contents, :
|
|
83
|
+
migration_filename = "#{format('%03d', migration_number)}_#{filename.underscore}.rb"
|
|
84
|
+
create_file(destination_root('db/migrate/', migration_filename), contents, skip: true)
|
|
85
85
|
end
|
|
86
86
|
end
|
|
87
87
|
|
|
@@ -89,9 +89,9 @@ module Padrino
|
|
|
89
89
|
# Returns the number of the latest(most current) migration file.
|
|
90
90
|
#
|
|
91
91
|
def return_last_migration_number
|
|
92
|
-
Dir[destination_root('db/migrate/*.rb')].map
|
|
92
|
+
Dir[destination_root('db/migrate/*.rb')].map do |f|
|
|
93
93
|
File.basename(f).match(/^(\d+)/)[0].to_i
|
|
94
|
-
|
|
94
|
+
end.max.to_i || 0
|
|
95
95
|
end
|
|
96
96
|
|
|
97
97
|
##
|
|
@@ -99,7 +99,7 @@ module Padrino
|
|
|
99
99
|
#
|
|
100
100
|
def current_migration_number
|
|
101
101
|
if fetch_component_choice(:migration_format).to_s == 'timestamp'
|
|
102
|
-
Time.now.utc.strftime(
|
|
102
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
|
103
103
|
else
|
|
104
104
|
return_last_migration_number + 1
|
|
105
105
|
end.to_s
|
|
@@ -112,7 +112,7 @@ module Padrino
|
|
|
112
112
|
# File name of the migration file.
|
|
113
113
|
#
|
|
114
114
|
def migration_exist?(filename)
|
|
115
|
-
Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].
|
|
115
|
+
Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].any?
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
##
|
|
@@ -122,13 +122,13 @@ module Padrino
|
|
|
122
122
|
# File name of the migration.
|
|
123
123
|
#
|
|
124
124
|
def remove_migration(name)
|
|
125
|
-
migration_path =
|
|
125
|
+
migration_path = Dir[destination_root('db/migrate/*.rb')].find do |f|
|
|
126
126
|
File.basename(f) =~ /#{name.to_s.underscore}/
|
|
127
127
|
end
|
|
128
128
|
return unless migration_path
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
|
|
130
|
+
# we use create to reverse the operation of a revoke
|
|
131
|
+
create_file(migration_path) if behavior == :revoke
|
|
132
132
|
end
|
|
133
133
|
|
|
134
134
|
##
|
|
@@ -141,10 +141,10 @@ module Padrino
|
|
|
141
141
|
#
|
|
142
142
|
# @example
|
|
143
143
|
# insert_test_suite_setup('...CLASS_NAME...')
|
|
144
|
-
# => inject_into_file(
|
|
144
|
+
# => inject_into_file('test/test_config.rb', TEST.gsub(/CLASS_NAME/, @app_name), after: 'set :environment, :test')
|
|
145
145
|
#
|
|
146
|
-
def insert_test_suite_setup(suite_text, options={})
|
|
147
|
-
options = { :
|
|
146
|
+
def insert_test_suite_setup(suite_text, options = {})
|
|
147
|
+
options = { path: 'test/test_config.rb' }.update(options)
|
|
148
148
|
create_file(options[:path], suite_text.gsub(/CLASS_NAME/, "#{@project_name}::#{@app_name}"))
|
|
149
149
|
end
|
|
150
150
|
|
|
@@ -158,13 +158,13 @@ module Padrino
|
|
|
158
158
|
#
|
|
159
159
|
# @example
|
|
160
160
|
# insert_mocking_include('Mocha::API'):
|
|
161
|
-
# => inject_into_file(
|
|
161
|
+
# => inject_into_file('test/test_config.rb', " include Mocha::API\n", after: /class.*?\n/)
|
|
162
162
|
#
|
|
163
|
-
def insert_mocking_include(library_name, options={})
|
|
164
|
-
options = { :
|
|
163
|
+
def insert_mocking_include(library_name, options = {})
|
|
164
|
+
options = { indent: 2, after: /class.*?\n/, path: 'test/test_config.rb' }.update(options)
|
|
165
165
|
return unless File.exist?(destination_root(options[:path]))
|
|
166
166
|
include_text = indent_spaces(2) + "include #{library_name}\n"
|
|
167
|
-
inject_into_file(options[:path], include_text, :
|
|
167
|
+
inject_into_file(options[:path], include_text, after: options[:after])
|
|
168
168
|
end
|
|
169
169
|
|
|
170
170
|
##
|
|
@@ -187,20 +187,20 @@ module Padrino
|
|
|
187
187
|
# controller_actions("get:index", "post:test")
|
|
188
188
|
#
|
|
189
189
|
def controller_actions(fields)
|
|
190
|
-
field_tuples = fields.map { |value| value.split(
|
|
190
|
+
field_tuples = fields.map { |value| value.split(':') }
|
|
191
191
|
action_declarations = field_tuples.map do |request, name|
|
|
192
192
|
"#{request} :#{name} do\n\nend\n"
|
|
193
193
|
end
|
|
194
|
-
action_declarations.join("\n").gsub(/^/,
|
|
194
|
+
action_declarations.join("\n").gsub(/^/, ' ' * 2).gsub(/^\s*$/, '')
|
|
195
195
|
end
|
|
196
196
|
|
|
197
197
|
def create_helper_files(app, name)
|
|
198
|
-
@helper_name
|
|
198
|
+
@helper_name = "#{name.to_s.underscore.camelize}Helper"
|
|
199
199
|
template 'templates/helper.rb.tt', destination_root(app, 'helpers', "#{name.to_s.underscore}_helper.rb")
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
200
|
+
return unless test?
|
|
201
|
+
|
|
202
|
+
include_component_module_for(:test)
|
|
203
|
+
generate_helper_test(@helper_name, @project_name, @app_name)
|
|
204
204
|
end
|
|
205
205
|
end
|
|
206
206
|
end
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
def setup_mock
|
|
2
|
-
require_dependencies 'mocha', :
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
require_dependencies 'mocha', group: 'test', require: false
|
|
3
|
+
|
|
4
|
+
if options[:test].to_s == 'rspec'
|
|
5
|
+
inject_into_file 'spec/spec_helper.rb', " conf.mock_with :mocha\n", after: "RSpec.configure do |conf|\n"
|
|
6
|
+
else
|
|
7
|
+
inject_into_file 'test/test_config.rb', "require 'mocha/api'\n", after: "require_relative '../config/boot')\n"
|
|
8
|
+
insert_mocking_include 'Mocha::API'
|
|
9
9
|
end
|
|
10
10
|
end
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
def setup_mock
|
|
2
|
-
require_dependencies 'rr', :
|
|
2
|
+
require_dependencies 'rr', require: false, group: 'test'
|
|
3
3
|
case options[:test].to_s
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
when 'rspec'
|
|
5
|
+
inject_into_file 'spec/spec_helper.rb', "require 'rr'\n", after: "'../config/boot'\n"
|
|
6
|
+
when 'minitest'
|
|
7
|
+
insert_mocking_include 'RR::Adapters::MiniTest', path: 'test/test_config.rb'
|
|
8
|
+
else
|
|
9
|
+
insert_mocking_include 'RR::Adapters::TestUnit', path: 'test/test_config.rb'
|
|
10
10
|
end
|
|
11
11
|
end
|
|
@@ -1,114 +1,114 @@
|
|
|
1
|
-
AR =
|
|
2
|
-
##
|
|
3
|
-
# You can use other adapters like:
|
|
4
|
-
#
|
|
5
|
-
# ActiveRecord::Base.configurations = {
|
|
6
|
-
# :
|
|
7
|
-
# :
|
|
8
|
-
# :
|
|
9
|
-
# :
|
|
10
|
-
# :
|
|
11
|
-
# :
|
|
12
|
-
# :
|
|
13
|
-
# :
|
|
14
|
-
# :
|
|
15
|
-
# :
|
|
16
|
-
# }
|
|
17
|
-
# }
|
|
18
|
-
#
|
|
19
|
-
ActiveRecord::Base.configurations = {
|
|
20
|
-
|
|
21
|
-
!DB_DEVELOPMENT!
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
!DB_PRODUCTION!
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
!DB_TEST!
|
|
1
|
+
AR = <<~AR unless defined?(AR)
|
|
2
|
+
##
|
|
3
|
+
# You can use other adapters like:
|
|
4
|
+
#
|
|
5
|
+
# ActiveRecord::Base.configurations = {
|
|
6
|
+
# development: {
|
|
7
|
+
# adapter: 'mysql2',
|
|
8
|
+
# encoding: 'utf8',
|
|
9
|
+
# reconnect: true,
|
|
10
|
+
# database: 'your_database',
|
|
11
|
+
# pool: 5,
|
|
12
|
+
# username: 'root',
|
|
13
|
+
# password: '',
|
|
14
|
+
# host: 'localhost',
|
|
15
|
+
# socket: '/tmp/mysql.sock'
|
|
16
|
+
# }
|
|
17
|
+
# }
|
|
18
|
+
#
|
|
19
|
+
ActiveRecord::Base.configurations = {
|
|
20
|
+
development: {
|
|
21
|
+
!DB_DEVELOPMENT!
|
|
22
|
+
},
|
|
23
|
+
production: {
|
|
24
|
+
!DB_PRODUCTION!
|
|
25
|
+
},
|
|
26
|
+
test: {
|
|
27
|
+
!DB_TEST!
|
|
28
|
+
}
|
|
28
29
|
}
|
|
29
|
-
}
|
|
30
30
|
|
|
31
|
-
# Setup our logger
|
|
32
|
-
ActiveRecord::Base.logger = logger
|
|
31
|
+
# Setup our logger
|
|
32
|
+
ActiveRecord::Base.logger = logger
|
|
33
33
|
|
|
34
|
-
if ActiveRecord::VERSION::MAJOR.to_i < 4
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
if ActiveRecord::VERSION::MAJOR.to_i < 4
|
|
35
|
+
# Raise exception on mass assignment protection for Active Record models.
|
|
36
|
+
ActiveRecord::Base.mass_assignment_sanitizer = :strict
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
end
|
|
38
|
+
# Log the query plan for queries taking more than this (works
|
|
39
|
+
# with SQLite, MySQL, and PostgreSQL).
|
|
40
|
+
ActiveRecord::Base.auto_explain_threshold_in_seconds = 0.5
|
|
41
|
+
end
|
|
42
42
|
|
|
43
|
-
# Doesn't include Active Record class name as root for JSON serialized output.
|
|
44
|
-
ActiveRecord::Base.include_root_in_json = false
|
|
43
|
+
# Doesn't include Active Record class name as root for JSON serialized output.
|
|
44
|
+
ActiveRecord::Base.include_root_in_json = false
|
|
45
45
|
|
|
46
|
-
# Store the full class name (including module namespace) in STI type column.
|
|
47
|
-
ActiveRecord::Base.store_full_sti_class = true
|
|
46
|
+
# Store the full class name (including module namespace) in STI type column.
|
|
47
|
+
ActiveRecord::Base.store_full_sti_class = true
|
|
48
48
|
|
|
49
|
-
# Use ISO 8601 format for JSON serialized times and dates.
|
|
50
|
-
ActiveSupport.use_standard_json_time_format = true
|
|
49
|
+
# Use ISO 8601 format for JSON serialized times and dates.
|
|
50
|
+
ActiveSupport.use_standard_json_time_format = true
|
|
51
51
|
|
|
52
|
-
# Don't escape HTML entities in JSON, leave that for the #json_escape helper
|
|
53
|
-
# if you're including raw JSON in an HTML page.
|
|
54
|
-
ActiveSupport.escape_html_entities_in_json = false
|
|
52
|
+
# Don't escape HTML entities in JSON, leave that for the #json_escape helper
|
|
53
|
+
# if you're including raw JSON in an HTML page.
|
|
54
|
+
ActiveSupport.escape_html_entities_in_json = false
|
|
55
55
|
|
|
56
|
-
# Now we can establish connection with our db.
|
|
57
|
-
if ActiveRecord::VERSION::MAJOR.to_i < 6
|
|
58
|
-
|
|
59
|
-
else
|
|
60
|
-
|
|
61
|
-
end
|
|
56
|
+
# Now we can establish connection with our db.
|
|
57
|
+
if ActiveRecord::VERSION::MAJOR.to_i < 6
|
|
58
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
|
|
59
|
+
else
|
|
60
|
+
ActiveRecord::Base.establish_connection(Padrino.env)
|
|
61
|
+
end
|
|
62
62
|
AR
|
|
63
63
|
|
|
64
|
-
MYSQL =
|
|
65
|
-
:
|
|
66
|
-
:
|
|
67
|
-
:
|
|
68
|
-
:
|
|
69
|
-
:
|
|
70
|
-
:
|
|
71
|
-
:
|
|
72
|
-
:
|
|
73
|
-
:
|
|
64
|
+
MYSQL = <<-MYSQL.chomp unless defined?(MYSQL)
|
|
65
|
+
adapter: 'mysql',
|
|
66
|
+
encoding: 'utf8',
|
|
67
|
+
reconnect: true,
|
|
68
|
+
database: !DB_NAME!,
|
|
69
|
+
pool: 5,
|
|
70
|
+
username: 'root',
|
|
71
|
+
password: '',
|
|
72
|
+
host: 'localhost',
|
|
73
|
+
socket: '/tmp/mysql.sock'
|
|
74
74
|
MYSQL
|
|
75
75
|
|
|
76
|
-
MYSQL2 =
|
|
77
|
-
:
|
|
78
|
-
:
|
|
79
|
-
:
|
|
80
|
-
:
|
|
81
|
-
:
|
|
82
|
-
:
|
|
83
|
-
:
|
|
84
|
-
:
|
|
85
|
-
:
|
|
76
|
+
MYSQL2 = <<-MYSQL2.chomp unless defined?(MYSQL2)
|
|
77
|
+
adapter: 'mysql2',
|
|
78
|
+
encoding: 'utf8',
|
|
79
|
+
reconnect: true,
|
|
80
|
+
database: !DB_NAME!,
|
|
81
|
+
pool: 5,
|
|
82
|
+
username: 'root',
|
|
83
|
+
password: '',
|
|
84
|
+
host: 'localhost',
|
|
85
|
+
socket: '/tmp/mysql.sock'
|
|
86
86
|
MYSQL2
|
|
87
87
|
|
|
88
|
-
POSTGRES =
|
|
89
|
-
:
|
|
90
|
-
:
|
|
91
|
-
:
|
|
92
|
-
:
|
|
93
|
-
:
|
|
94
|
-
:
|
|
88
|
+
POSTGRES = <<-POSTGRES.chomp unless defined?(POSTGRES)
|
|
89
|
+
adapter: 'postgresql',
|
|
90
|
+
database: !DB_NAME!,
|
|
91
|
+
username: 'root',
|
|
92
|
+
password: '',
|
|
93
|
+
host: 'localhost',
|
|
94
|
+
port: 5432
|
|
95
95
|
POSTGRES
|
|
96
96
|
|
|
97
|
-
SQLITE =
|
|
98
|
-
:
|
|
99
|
-
:
|
|
97
|
+
SQLITE = <<-SQLITE.chomp unless defined?(SQLITE)
|
|
98
|
+
adapter: 'sqlite3',
|
|
99
|
+
database: !DB_NAME!
|
|
100
100
|
SQLITE
|
|
101
101
|
|
|
102
|
-
CONNECTION_POOL_MIDDLEWARE =
|
|
103
|
-
class ConnectionPoolManagement
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
CONNECTION_POOL_MIDDLEWARE = <<~MIDDLEWARE
|
|
103
|
+
class ConnectionPoolManagement
|
|
104
|
+
def initialize(app)
|
|
105
|
+
@app = app
|
|
106
|
+
end
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
108
|
+
def call(env)
|
|
109
|
+
ActiveRecord::Base.connection_pool.with_connection { @app.call(env) }
|
|
110
|
+
end
|
|
110
111
|
end
|
|
111
|
-
end
|
|
112
112
|
MIDDLEWARE
|
|
113
113
|
|
|
114
114
|
def setup_orm
|
|
@@ -118,109 +118,111 @@ def setup_orm
|
|
|
118
118
|
begin
|
|
119
119
|
case adapter ||= options[:adapter]
|
|
120
120
|
when 'mysql-gem'
|
|
121
|
-
ar.sub!
|
|
122
|
-
ar.sub!
|
|
123
|
-
ar.sub!
|
|
124
|
-
require_dependencies 'mysql', :
|
|
121
|
+
ar.sub!(/!DB_DEVELOPMENT!/, MYSQL.sub(/!DB_NAME!/, "'#{db}_development'"))
|
|
122
|
+
ar.sub!(/!DB_PRODUCTION!/, MYSQL.sub(/!DB_NAME!/, "'#{db}_production'"))
|
|
123
|
+
ar.sub!(/!DB_TEST!/, MYSQL.sub(/!DB_NAME!/, "'#{db}_test'"))
|
|
124
|
+
require_dependencies 'mysql', version: '~> 2.8.1'
|
|
125
125
|
when 'mysql', 'mysql2'
|
|
126
|
-
ar.sub!
|
|
127
|
-
ar.sub!
|
|
128
|
-
ar.sub!
|
|
126
|
+
ar.sub!(/!DB_DEVELOPMENT!/, MYSQL2.sub(/!DB_NAME!/, "'#{db}_development'"))
|
|
127
|
+
ar.sub!(/!DB_PRODUCTION!/, MYSQL2.sub(/!DB_NAME!/, "'#{db}_production'"))
|
|
128
|
+
ar.sub!(/!DB_TEST!/, MYSQL2.sub(/!DB_NAME!/, "'#{db}_test'"))
|
|
129
129
|
require_dependencies 'mysql2'
|
|
130
130
|
when 'postgres'
|
|
131
|
-
ar.sub!
|
|
132
|
-
ar.sub!
|
|
133
|
-
ar.sub!
|
|
131
|
+
ar.sub!(/!DB_DEVELOPMENT!/, POSTGRES.sub(/!DB_NAME!/, "'#{db}_development'"))
|
|
132
|
+
ar.sub!(/!DB_PRODUCTION!/, POSTGRES.sub(/!DB_NAME!/, "'#{db}_production'"))
|
|
133
|
+
ar.sub!(/!DB_TEST!/, POSTGRES.sub(/!DB_NAME!/, "'#{db}_test'"))
|
|
134
134
|
require_dependencies 'pg'
|
|
135
135
|
when 'sqlite'
|
|
136
|
-
ar.sub!
|
|
137
|
-
ar.sub!
|
|
138
|
-
ar.sub!
|
|
136
|
+
ar.sub!(/!DB_DEVELOPMENT!/, SQLITE.sub(/!DB_NAME!/, "Padrino.root('db', '#{db}_development.db')"))
|
|
137
|
+
ar.sub!(/!DB_PRODUCTION!/, SQLITE.sub(/!DB_NAME!/, "Padrino.root('db', '#{db}_production.db')"))
|
|
138
|
+
ar.sub!(/!DB_TEST!/, SQLITE.sub(/!DB_NAME!/, "Padrino.root('db', '#{db}_test.db')"))
|
|
139
139
|
require_dependencies 'sqlite3'
|
|
140
140
|
else
|
|
141
141
|
say "Failed to generate `config/database.rb` for ORM adapter `#{options[:adapter]}`", :red
|
|
142
|
-
|
|
142
|
+
raise ArgumentError
|
|
143
143
|
end
|
|
144
144
|
rescue ArgumentError
|
|
145
|
-
adapter = ask(
|
|
145
|
+
adapter = ask('Please, choose a proper adapter:', limited_to: %w[mysql mysql2 mysql-gem postgres sqlite])
|
|
146
146
|
retry
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
-
require_dependencies 'activerecord', :
|
|
150
|
-
create_file(
|
|
149
|
+
require_dependencies 'activerecord', require: 'active_record', version: '>= 3.1'
|
|
150
|
+
create_file('config/database.rb', ar)
|
|
151
151
|
middleware :connection_pool_management, CONNECTION_POOL_MIDDLEWARE
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
-
AR_MODEL =
|
|
155
|
-
class !NAME! < ActiveRecord::Base
|
|
154
|
+
AR_MODEL = <<~MODEL unless defined?(AR_MODEL)
|
|
155
|
+
class !NAME! < ActiveRecord::Base
|
|
156
156
|
|
|
157
|
-
end
|
|
157
|
+
end
|
|
158
158
|
MODEL
|
|
159
159
|
|
|
160
|
-
# options => { :
|
|
161
|
-
def create_model_file(name, options={})
|
|
160
|
+
# options => { fields: ['title:string', 'body:string'], app: 'app' }
|
|
161
|
+
def create_model_file(name, options = {})
|
|
162
162
|
model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
|
|
163
163
|
model_contents = AR_MODEL.sub(/!NAME!/, name.to_s.underscore.camelize)
|
|
164
|
-
create_file(model_path, model_contents
|
|
164
|
+
create_file(model_path, model_contents, skip: true)
|
|
165
165
|
end
|
|
166
166
|
|
|
167
167
|
if defined?(ActiveRecord::Migration) && ActiveRecord::Migration.respond_to?(:[])
|
|
168
|
-
AR_MIGRATION =
|
|
169
|
-
class !FILECLASS! < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
end
|
|
178
|
-
MIGRATION
|
|
168
|
+
AR_MIGRATION = <<~MIGRATION unless defined?(AR_MIGRATION)
|
|
169
|
+
class !FILECLASS! < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]
|
|
170
|
+
def self.up
|
|
171
|
+
!UP!
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def self.down
|
|
175
|
+
!DOWN!
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
MIGRATION
|
|
179
179
|
else
|
|
180
|
-
AR_MIGRATION =
|
|
181
|
-
class !FILECLASS! < ActiveRecord::Migration
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
end
|
|
190
|
-
MIGRATION
|
|
180
|
+
AR_MIGRATION = <<~MIGRATION unless defined?(AR_MIGRATION)
|
|
181
|
+
class !FILECLASS! < ActiveRecord::Migration
|
|
182
|
+
def self.up
|
|
183
|
+
!UP!
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def self.down
|
|
187
|
+
!DOWN!
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
MIGRATION
|
|
191
191
|
end
|
|
192
192
|
|
|
193
|
-
AR_MODEL_UP_MG =
|
|
194
|
-
create_table :!TABLE! do |t|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
end
|
|
193
|
+
AR_MODEL_UP_MG = <<~MIGRATION.gsub(/^/, ' ') unless defined?(AR_MODEL_UP_MG)
|
|
194
|
+
create_table :!TABLE! do |t|
|
|
195
|
+
!FIELDS!
|
|
196
|
+
t.timestamps null: false
|
|
197
|
+
end
|
|
198
198
|
MIGRATION
|
|
199
199
|
|
|
200
|
-
AR_MODEL_DOWN_MG =
|
|
201
|
-
drop_table :!TABLE!
|
|
200
|
+
AR_MODEL_DOWN_MG = <<~MIGRATION unless defined?(AR_MODEL_DOWN_MG)
|
|
201
|
+
drop_table :!TABLE!
|
|
202
202
|
MIGRATION
|
|
203
203
|
|
|
204
204
|
def create_model_migration(migration_name, name, columns)
|
|
205
|
-
output_model_migration(
|
|
206
|
-
|
|
207
|
-
:
|
|
208
|
-
:
|
|
209
|
-
:
|
|
205
|
+
output_model_migration(
|
|
206
|
+
migration_name, name, columns,
|
|
207
|
+
base: AR_MIGRATION,
|
|
208
|
+
column_format: proc { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
|
|
209
|
+
up: AR_MODEL_UP_MG,
|
|
210
|
+
down: AR_MODEL_DOWN_MG
|
|
210
211
|
)
|
|
211
212
|
end
|
|
212
213
|
|
|
213
|
-
AR_CHANGE_MG =
|
|
214
|
-
change_table :!TABLE! do |t|
|
|
215
|
-
|
|
216
|
-
end
|
|
214
|
+
AR_CHANGE_MG = <<~MIGRATION.gsub(/^/, ' ') unless defined?(AR_CHANGE_MG)
|
|
215
|
+
change_table :!TABLE! do |t|
|
|
216
|
+
!COLUMNS!
|
|
217
|
+
end
|
|
217
218
|
MIGRATION
|
|
218
219
|
|
|
219
220
|
def create_migration_file(migration_name, name, columns)
|
|
220
|
-
output_migration_file(
|
|
221
|
-
|
|
222
|
-
:
|
|
223
|
-
:
|
|
224
|
-
:
|
|
221
|
+
output_migration_file(
|
|
222
|
+
migration_name, name, columns,
|
|
223
|
+
base: AR_MIGRATION,
|
|
224
|
+
change_format: AR_CHANGE_MG,
|
|
225
|
+
add: proc { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
|
|
226
|
+
remove: proc { |field, _kind| "t.remove :#{field}" }
|
|
225
227
|
)
|
|
226
228
|
end
|