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
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
MR =
|
|
2
|
-
##
|
|
3
|
-
# You can use other adapters like:
|
|
4
|
-
#
|
|
5
|
-
# ActiveRecord::Base.configurations[:development] = {
|
|
6
|
-
# :
|
|
7
|
-
# :
|
|
8
|
-
# :
|
|
9
|
-
# :
|
|
10
|
-
# :
|
|
11
|
-
# :
|
|
12
|
-
# :
|
|
13
|
-
# :
|
|
14
|
-
# :
|
|
15
|
-
# }
|
|
16
|
-
#
|
|
17
|
-
ActiveRecord::Base.configurations[:development] = {
|
|
18
|
-
!DB_DEVELOPMENT!
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
ActiveRecord::Base.configurations[:production] = {
|
|
22
|
-
!DB_PRODUCTION!
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
ActiveRecord::Base.configurations[:test] = {
|
|
26
|
-
!DB_TEST!
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
# Setup our logger.
|
|
30
|
-
ActiveRecord::Base.logger = logger
|
|
31
|
-
|
|
32
|
-
if ActiveRecord::VERSION::MAJOR.to_i < 4
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
1
|
+
MR = <<~MR unless defined?(MR)
|
|
2
|
+
##
|
|
3
|
+
# You can use other adapters like:
|
|
4
|
+
#
|
|
5
|
+
# ActiveRecord::Base.configurations[:development] = {
|
|
6
|
+
# adapter: 'mysql2',
|
|
7
|
+
# encoding: 'utf8',
|
|
8
|
+
# reconnect: true,
|
|
9
|
+
# database: 'your_database',
|
|
10
|
+
# pool: 5,
|
|
11
|
+
# username: 'root',
|
|
12
|
+
# password: '',
|
|
13
|
+
# host: 'localhost',
|
|
14
|
+
# socket: '/tmp/mysql.sock'
|
|
15
|
+
# }
|
|
16
|
+
#
|
|
17
|
+
ActiveRecord::Base.configurations[:development] = {
|
|
18
|
+
!DB_DEVELOPMENT!
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
ActiveRecord::Base.configurations[:production] = {
|
|
22
|
+
!DB_PRODUCTION!
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
ActiveRecord::Base.configurations[:test] = {
|
|
26
|
+
!DB_TEST!
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# Setup our logger.
|
|
30
|
+
ActiveRecord::Base.logger = logger
|
|
31
|
+
|
|
32
|
+
if ActiveRecord::VERSION::MAJOR.to_i < 4
|
|
33
|
+
# Raise exception on mass assignment protection for Active Record models.
|
|
34
|
+
ActiveRecord::Base.mass_assignment_sanitizer = :strict
|
|
35
|
+
|
|
36
|
+
# Log the query plan for queries taking more than this (works
|
|
37
|
+
# with SQLite, MySQL, and PostgreSQL).
|
|
38
|
+
ActiveRecord::Base.auto_explain_threshold_in_seconds = 0.5
|
|
39
|
+
end
|
|
40
40
|
|
|
41
|
-
# Doesn't include Active Record class name as root for JSON serialized output.
|
|
42
|
-
ActiveRecord::Base.include_root_in_json = false
|
|
41
|
+
# Doesn't include Active Record class name as root for JSON serialized output.
|
|
42
|
+
ActiveRecord::Base.include_root_in_json = false
|
|
43
43
|
|
|
44
|
-
# Store the full class name (including module namespace) in STI type column.
|
|
45
|
-
ActiveRecord::Base.store_full_sti_class = true
|
|
44
|
+
# Store the full class name (including module namespace) in STI type column.
|
|
45
|
+
ActiveRecord::Base.store_full_sti_class = true
|
|
46
46
|
|
|
47
|
-
# Use ISO 8601 format for JSON serialized times and dates.
|
|
48
|
-
ActiveSupport.use_standard_json_time_format = true
|
|
47
|
+
# Use ISO 8601 format for JSON serialized times and dates.
|
|
48
|
+
ActiveSupport.use_standard_json_time_format = true
|
|
49
49
|
|
|
50
|
-
# Don't escape HTML entities in JSON, leave that for the #json_escape helper
|
|
51
|
-
# if you're including raw JSON in an HTML page.
|
|
52
|
-
ActiveSupport.escape_html_entities_in_json = false
|
|
50
|
+
# Don't escape HTML entities in JSON, leave that for the #json_escape helper
|
|
51
|
+
# if you're including raw JSON in an HTML page.
|
|
52
|
+
ActiveSupport.escape_html_entities_in_json = false
|
|
53
53
|
|
|
54
|
-
# Now we can establish connection with our db.
|
|
55
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
|
|
54
|
+
# Now we can establish connection with our db.
|
|
55
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
|
|
56
56
|
|
|
57
|
-
# Timestamps are in the utc by default.
|
|
58
|
-
ActiveRecord::Base.default_timezone = :utc
|
|
57
|
+
# Timestamps are in the utc by default.
|
|
58
|
+
ActiveRecord::Base.default_timezone = :utc
|
|
59
59
|
MR
|
|
60
60
|
|
|
61
|
-
MYSQL =
|
|
62
|
-
:
|
|
63
|
-
:
|
|
64
|
-
:
|
|
65
|
-
:
|
|
66
|
-
:
|
|
67
|
-
:
|
|
68
|
-
:
|
|
69
|
-
:
|
|
70
|
-
:
|
|
61
|
+
MYSQL = <<-MYSQL unless defined?(MYSQL)
|
|
62
|
+
adapter: 'mysql',
|
|
63
|
+
encoding: 'utf8',
|
|
64
|
+
reconnect: true,
|
|
65
|
+
database: !DB_NAME!,
|
|
66
|
+
pool: 5,
|
|
67
|
+
username: 'root',
|
|
68
|
+
password: '',
|
|
69
|
+
host: 'localhost',
|
|
70
|
+
socket: '/tmp/mysql.sock'
|
|
71
71
|
MYSQL
|
|
72
72
|
|
|
73
|
-
MYSQL2 =
|
|
74
|
-
:
|
|
75
|
-
:
|
|
76
|
-
:
|
|
77
|
-
:
|
|
78
|
-
:
|
|
79
|
-
:
|
|
80
|
-
:
|
|
81
|
-
:
|
|
82
|
-
:
|
|
73
|
+
MYSQL2 = <<-MYSQL2 unless defined?(MYSQL2)
|
|
74
|
+
adapter: 'mysql2',
|
|
75
|
+
encoding: 'utf8',
|
|
76
|
+
reconnect: true,
|
|
77
|
+
database: !DB_NAME!,
|
|
78
|
+
pool: 5,
|
|
79
|
+
username: 'root',
|
|
80
|
+
password: '',
|
|
81
|
+
host: 'localhost',
|
|
82
|
+
socket: '/tmp/mysql.sock'
|
|
83
83
|
MYSQL2
|
|
84
84
|
|
|
85
|
-
POSTGRES =
|
|
86
|
-
:
|
|
87
|
-
:
|
|
88
|
-
:
|
|
89
|
-
:
|
|
90
|
-
:
|
|
91
|
-
:
|
|
85
|
+
POSTGRES = <<-POSTGRES unless defined?(POSTGRES)
|
|
86
|
+
adapter: 'postgresql',
|
|
87
|
+
database: !DB_NAME!,
|
|
88
|
+
username: 'root',
|
|
89
|
+
password: '',
|
|
90
|
+
host: 'localhost',
|
|
91
|
+
port: 5432
|
|
92
92
|
POSTGRES
|
|
93
93
|
|
|
94
|
-
SQLITE =
|
|
95
|
-
:
|
|
96
|
-
:
|
|
94
|
+
SQLITE = <<-SQLITE unless defined?(SQLITE)
|
|
95
|
+
adapter: 'sqlite3',
|
|
96
|
+
database: !DB_NAME!
|
|
97
97
|
SQLITE
|
|
98
98
|
|
|
99
|
-
CONNECTION_POOL_MIDDLEWARE =
|
|
100
|
-
class ConnectionPoolManagement
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
99
|
+
CONNECTION_POOL_MIDDLEWARE = <<~MIDDLEWARE
|
|
100
|
+
class ConnectionPoolManagement
|
|
101
|
+
def initialize(app)
|
|
102
|
+
@app = app
|
|
103
|
+
end
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
def call(env)
|
|
106
|
+
ActiveRecord::Base.connection_pool.with_connection { @app.call(env) }
|
|
107
|
+
end
|
|
107
108
|
end
|
|
108
|
-
end
|
|
109
109
|
MIDDLEWARE
|
|
110
110
|
|
|
111
111
|
def setup_orm
|
|
@@ -115,31 +115,31 @@ def setup_orm
|
|
|
115
115
|
begin
|
|
116
116
|
case adapter ||= options[:adapter]
|
|
117
117
|
when 'mysql-gem'
|
|
118
|
-
ar.gsub!
|
|
119
|
-
ar.gsub!
|
|
120
|
-
ar.gsub!
|
|
121
|
-
require_dependencies 'mysql', :
|
|
118
|
+
ar.gsub!(/!DB_DEVELOPMENT!/, MYSQL.gsub(/!DB_NAME!/, "'#{db}_development'"))
|
|
119
|
+
ar.gsub!(/!DB_PRODUCTION!/, MYSQL.gsub(/!DB_NAME!/, "'#{db}_production'"))
|
|
120
|
+
ar.gsub!(/!DB_TEST!/, MYSQL.gsub(/!DB_NAME!/, "'#{db}_test'"))
|
|
121
|
+
require_dependencies 'mysql', version: '~> 2.8.1'
|
|
122
122
|
when 'mysql', 'mysql2'
|
|
123
|
-
ar.gsub!
|
|
124
|
-
ar.gsub!
|
|
125
|
-
ar.gsub!
|
|
123
|
+
ar.gsub!(/!DB_DEVELOPMENT!/, MYSQL2.gsub(/!DB_NAME!/, "'#{db}_development'"))
|
|
124
|
+
ar.gsub!(/!DB_PRODUCTION!/, MYSQL2.gsub(/!DB_NAME!/, "'#{db}_production'"))
|
|
125
|
+
ar.gsub!(/!DB_TEST!/, MYSQL2.gsub(/!DB_NAME!/, "'#{db}_test'"))
|
|
126
126
|
require_dependencies 'mysql2'
|
|
127
127
|
when 'postgres'
|
|
128
|
-
ar.gsub!
|
|
129
|
-
ar.gsub!
|
|
130
|
-
ar.gsub!
|
|
128
|
+
ar.gsub!(/!DB_DEVELOPMENT!/, POSTGRES.gsub(/!DB_NAME!/, "'#{db}_development'"))
|
|
129
|
+
ar.gsub!(/!DB_PRODUCTION!/, POSTGRES.gsub(/!DB_NAME!/, "'#{db}_production'"))
|
|
130
|
+
ar.gsub!(/!DB_TEST!/, POSTGRES.gsub(/!DB_NAME!/, "'#{db}_test'"))
|
|
131
131
|
require_dependencies 'pg'
|
|
132
132
|
when 'sqlite'
|
|
133
|
-
ar.gsub!
|
|
134
|
-
ar.gsub!
|
|
135
|
-
ar.gsub!
|
|
133
|
+
ar.gsub!(/!DB_DEVELOPMENT!/, SQLITE.gsub(/!DB_NAME!/, "Padrino.root('db', '#{db}_development.db')"))
|
|
134
|
+
ar.gsub!(/!DB_PRODUCTION!/, SQLITE.gsub(/!DB_NAME!/, "Padrino.root('db', '#{db}_production.db')"))
|
|
135
|
+
ar.gsub!(/!DB_TEST!/, SQLITE.gsub(/!DB_NAME!/, "Padrino.root('db', '#{db}_test.db')"))
|
|
136
136
|
require_dependencies 'sqlite3'
|
|
137
137
|
else
|
|
138
138
|
say "Failed to generate `config/database.rb` for ORM adapter `#{options[:adapter]}`", :red
|
|
139
|
-
|
|
139
|
+
raise ArgumentError
|
|
140
140
|
end
|
|
141
141
|
rescue ArgumentError
|
|
142
|
-
adapter = ask(
|
|
142
|
+
adapter = ask('Please, choose a proper adapter:', limited_to: %w[mysql mysql2 mysql-gem postgres sqlite])
|
|
143
143
|
retry
|
|
144
144
|
end
|
|
145
145
|
|
|
@@ -149,18 +149,18 @@ def setup_orm
|
|
|
149
149
|
middleware :connection_pool_management, CONNECTION_POOL_MIDDLEWARE
|
|
150
150
|
end
|
|
151
151
|
|
|
152
|
-
MR_MODEL =
|
|
153
|
-
class !NAME! < ActiveRecord::Base
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
end
|
|
152
|
+
MR_MODEL = <<~MODEL unless defined?(MR_MODEL)
|
|
153
|
+
class !NAME! < ActiveRecord::Base
|
|
154
|
+
# Fields
|
|
155
|
+
!FIELDS!
|
|
156
|
+
end
|
|
157
157
|
MODEL
|
|
158
158
|
|
|
159
|
-
# options => { :
|
|
160
|
-
def create_model_file(name, options={})
|
|
159
|
+
# options => { fields: ['title:string', 'body:string'], app: 'app' }
|
|
160
|
+
def create_model_file(name, options = {})
|
|
161
161
|
model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
|
|
162
|
-
field_tuples = options[:fields].map { |value| value.split(
|
|
163
|
-
column_declarations = field_tuples.map { |field, kind| "field :#{field}, :
|
|
162
|
+
field_tuples = options[:fields].map { |value| value.split(':') }
|
|
163
|
+
column_declarations = field_tuples.map { |field, kind| "field :#{field}, as: :#{kind}" }.join("\n ")
|
|
164
164
|
model_contents = MR_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
|
|
165
165
|
model_contents.gsub!(/!FIELDS!/, column_declarations)
|
|
166
166
|
create_file(model_path, model_contents)
|
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
MONGOID =
|
|
2
|
-
# Connection.new takes host and port.
|
|
1
|
+
MONGOID = <<~MONGO unless defined?(MONGOID)
|
|
2
|
+
# Connection.new takes host and port.
|
|
3
3
|
|
|
4
|
-
host = 'localhost'
|
|
5
|
-
port = 27017
|
|
4
|
+
host = 'localhost'
|
|
5
|
+
port = 27017
|
|
6
6
|
|
|
7
|
-
database_name = case Padrino.env
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
end
|
|
7
|
+
database_name = case Padrino.env
|
|
8
|
+
when :development then '!NAME!_development'
|
|
9
|
+
when :production then '!NAME!_production'
|
|
10
|
+
when :test then '!NAME!_test'
|
|
11
|
+
end
|
|
12
12
|
|
|
13
|
-
# Use MONGO_URI if it's set as an environmental variable.
|
|
14
|
-
database_settings = if ENV['MONGO_URI']
|
|
15
|
-
|
|
16
|
-
else
|
|
17
|
-
|
|
18
|
-
end
|
|
13
|
+
# Use MONGO_URI if it's set as an environmental variable.
|
|
14
|
+
database_settings = if ENV['MONGO_URI']
|
|
15
|
+
{ default: { uri: ENV['MONGO_URI'] } }
|
|
16
|
+
else
|
|
17
|
+
{ default: { hosts: ["#\{host\}:#\{port\}"], database: database_name} }
|
|
18
|
+
end
|
|
19
19
|
|
|
20
|
-
case Mongoid::VERSION
|
|
21
|
-
when /^(3|4)/
|
|
22
|
-
|
|
23
|
-
else
|
|
24
|
-
|
|
25
|
-
end
|
|
20
|
+
case Mongoid::VERSION
|
|
21
|
+
when /^(3|4)/
|
|
22
|
+
Mongoid::Config.sessions = database_settings
|
|
23
|
+
else
|
|
24
|
+
Mongoid::Config.load_configuration clients: database_settings
|
|
25
|
+
end
|
|
26
26
|
|
|
27
|
-
# If you want to use a YML file for config, use this instead:
|
|
28
|
-
#
|
|
29
|
-
# Mongoid.load!(File.join(Padrino.root, 'config', 'database.yml'), Padrino.env)
|
|
30
|
-
#
|
|
31
|
-
# And add a config/database.yml file like this:
|
|
32
|
-
# development:
|
|
33
|
-
# clients: #Replace clients with sessions to work with Mongoid version 3.x or 4.x
|
|
34
|
-
# default:
|
|
35
|
-
# database: !NAME!_development
|
|
36
|
-
# hosts:
|
|
37
|
-
# - localhost:27017
|
|
38
|
-
#
|
|
39
|
-
# production:
|
|
40
|
-
# clients: #Replace clients with sessions to work with Mongoid version 3.x or 4.x
|
|
41
|
-
# default:
|
|
42
|
-
# database: !NAME!_production
|
|
43
|
-
# hosts:
|
|
44
|
-
# - localhost:27017
|
|
45
|
-
#
|
|
46
|
-
# test:
|
|
47
|
-
# clients: #Replace clients with sessions to work with Mongoid version 3.x or 4.x
|
|
48
|
-
# default:
|
|
49
|
-
# database: !NAME!_test
|
|
50
|
-
# hosts:
|
|
51
|
-
# - localhost:27017
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
# More installation and setup notes are on https://docs.mongodb.org/ecosystem/tutorial/mongoid-installation/
|
|
27
|
+
# If you want to use a YML file for config, use this instead:
|
|
28
|
+
#
|
|
29
|
+
# Mongoid.load!(File.join(Padrino.root, 'config', 'database.yml'), Padrino.env)
|
|
30
|
+
#
|
|
31
|
+
# And add a config/database.yml file like this:
|
|
32
|
+
# development:
|
|
33
|
+
# clients: #Replace clients with sessions to work with Mongoid version 3.x or 4.x
|
|
34
|
+
# default:
|
|
35
|
+
# database: !NAME!_development
|
|
36
|
+
# hosts:
|
|
37
|
+
# - localhost:27017
|
|
38
|
+
#
|
|
39
|
+
# production:
|
|
40
|
+
# clients: #Replace clients with sessions to work with Mongoid version 3.x or 4.x
|
|
41
|
+
# default:
|
|
42
|
+
# database: !NAME!_production
|
|
43
|
+
# hosts:
|
|
44
|
+
# - localhost:27017
|
|
45
|
+
#
|
|
46
|
+
# test:
|
|
47
|
+
# clients: #Replace clients with sessions to work with Mongoid version 3.x or 4.x
|
|
48
|
+
# default:
|
|
49
|
+
# database: !NAME!_test
|
|
50
|
+
# hosts:
|
|
51
|
+
# - localhost:27017
|
|
52
|
+
#
|
|
53
|
+
#
|
|
54
|
+
# More installation and setup notes are on https://docs.mongodb.org/ecosystem/tutorial/mongoid-installation/
|
|
55
55
|
MONGO
|
|
56
56
|
|
|
57
57
|
def setup_orm
|
|
58
|
-
require_dependencies 'mongoid', :
|
|
58
|
+
require_dependencies 'mongoid', version: '>= 3.0.0'
|
|
59
59
|
create_file('config/database.rb', MONGOID.gsub(/!NAME!/, @project_name.underscore))
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
MONGOID_MODEL =
|
|
63
|
-
class !NAME!
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
MONGOID_MODEL = <<~MODEL unless defined?(MONGOID_MODEL)
|
|
63
|
+
class !NAME!
|
|
64
|
+
include Mongoid::Document
|
|
65
|
+
include Mongoid::Timestamps # adds created_at and updated_at fields
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
# field <name>, type: <type>, default: <value>
|
|
68
|
+
!FIELDS!
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
# You can define indexes on documents using the index macro:
|
|
71
|
+
# index :field <, unique: true>
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
end
|
|
73
|
+
# You can create a composite key in mongoid to replace the default id using the key macro:
|
|
74
|
+
# key :field <, :another_field, :one_more ....>
|
|
75
|
+
end
|
|
76
76
|
MODEL
|
|
77
77
|
|
|
78
|
-
# options => { :
|
|
79
|
-
def create_model_file(name, options={})
|
|
78
|
+
# options => { fields: ['title:string', 'body:string'], app: 'app' }
|
|
79
|
+
def create_model_file(name, options = {})
|
|
80
80
|
model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
|
|
81
|
-
field_tuples = options[:fields].map { |value| value.split(
|
|
82
|
-
column_declarations = field_tuples.map { |field, kind| "field :#{field}, :
|
|
81
|
+
field_tuples = options[:fields].map { |value| value.split(':') }
|
|
82
|
+
column_declarations = field_tuples.map { |field, kind| "field :#{field}, type: #{kind.underscore.camelize}" }.join("\n ")
|
|
83
83
|
model_contents = MONGOID_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
|
|
84
84
|
model_contents.gsub!(/!FIELDS!/, column_declarations)
|
|
85
85
|
create_file(model_path, model_contents)
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
MONGO =
|
|
2
|
-
MongoMapper.connection = Mongo::Connection.new('localhost', nil, :
|
|
1
|
+
MONGO = <<~MONGO unless defined?(MONGO)
|
|
2
|
+
MongoMapper.connection = Mongo::Connection.new('localhost', nil, logger: logger)
|
|
3
3
|
|
|
4
|
-
case Padrino.env
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
end
|
|
4
|
+
case Padrino.env
|
|
5
|
+
when :development then MongoMapper.database = '!NAME!_development'
|
|
6
|
+
when :production then MongoMapper.database = '!NAME!_production'
|
|
7
|
+
when :test then MongoMapper.database = '!NAME!_test'
|
|
8
|
+
end
|
|
9
9
|
MONGO
|
|
10
10
|
|
|
11
11
|
def setup_orm
|
|
12
12
|
require_dependencies 'mongo_mapper'
|
|
13
|
-
require_dependencies 'bson_ext', :
|
|
14
|
-
require_dependencies 'activemodel', :
|
|
15
|
-
create_file(
|
|
13
|
+
require_dependencies 'bson_ext', require: 'mongo'
|
|
14
|
+
require_dependencies 'activemodel', version: '< 5'
|
|
15
|
+
create_file('config/database.rb', MONGO.gsub(/!NAME!/, @project_name.underscore))
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
MM_MODEL =
|
|
19
|
-
class !NAME!
|
|
20
|
-
|
|
18
|
+
MM_MODEL = <<~MODEL unless defined?(MM_MODEL)
|
|
19
|
+
class !NAME!
|
|
20
|
+
include MongoMapper::Document
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
end
|
|
22
|
+
# key <name>, <type>
|
|
23
|
+
!FIELDS!
|
|
24
|
+
timestamps!
|
|
25
|
+
end
|
|
26
26
|
MODEL
|
|
27
27
|
|
|
28
|
-
# options => { :
|
|
29
|
-
def create_model_file(name, options={})
|
|
28
|
+
# options => { fields: ['title:string', 'body:string'], app: 'app' }
|
|
29
|
+
def create_model_file(name, options = {})
|
|
30
30
|
model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
|
|
31
|
-
field_tuples = options[:fields].map { |value| value.split(
|
|
31
|
+
field_tuples = options[:fields].map { |value| value.split(':') }
|
|
32
32
|
column_declarations = field_tuples.map { |field, kind| "key :#{field}, #{kind.underscore.camelize}" }.join("\n ")
|
|
33
33
|
model_contents = MM_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
|
|
34
34
|
model_contents.gsub!(/!FIELDS!/, column_declarations)
|
|
@@ -1,78 +1,82 @@
|
|
|
1
|
-
MONGOMATIC =
|
|
1
|
+
MONGOMATIC = <<~MONGO unless defined?(MONGOMATIC)
|
|
2
2
|
|
|
3
|
-
case Padrino.env
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
end
|
|
3
|
+
case Padrino.env
|
|
4
|
+
when :development then Mongomatic.db = Mongo::Connection.new.db('!NAME!_development')
|
|
5
|
+
when :production then Mongomatic.db = Mongo::Connection.new.db('!NAME!_production')
|
|
6
|
+
when :test then Mongomatic.db = Mongo::Connection.new.db('!NAME!_test')
|
|
7
|
+
end
|
|
8
8
|
MONGO
|
|
9
9
|
|
|
10
10
|
def setup_orm
|
|
11
|
-
|
|
11
|
+
_mongomatic = MONGOMATIC
|
|
12
12
|
require_dependencies 'mongomatic'
|
|
13
|
-
require_dependencies 'bson_ext', :
|
|
14
|
-
create_file(
|
|
13
|
+
require_dependencies 'bson_ext', require: 'mongo'
|
|
14
|
+
create_file('config/database.rb', MONGOMATIC.gsub(/!NAME!/, @project_name.underscore))
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
MONGOMATIC_MODEL =
|
|
18
|
-
class !NAME! < Mongomatic::Base
|
|
19
|
-
|
|
17
|
+
MONGOMATIC_MODEL = <<~MODEL unless defined?(MONGOMATIC_MODEL)
|
|
18
|
+
class !NAME! < Mongomatic::Base
|
|
19
|
+
include Mongomatic::Expectations::Helper
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
# Mongomatic does not have the traditional
|
|
22
|
+
# model definition that AR/MM/DM et. al. have.
|
|
23
|
+
# Staying true to the "ad-hoc" nature of MongoDB,
|
|
24
|
+
# there are no explicit column definitions in the
|
|
25
|
+
# model file.
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
# However you can "fake it" by making a column
|
|
28
|
+
# required using expectations
|
|
29
|
+
# For the sake of padrino g model,
|
|
30
|
+
# we'll assume that any property defined
|
|
31
|
+
# on the command-line is required
|
|
32
|
+
# In the case of Integer types, we'll add
|
|
33
|
+
# the expectation: be_a_number
|
|
34
|
+
# Future enhancement may allow a regex for
|
|
35
|
+
# String datatypes
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
# Examples:
|
|
39
|
+
# def validate
|
|
40
|
+
# expectations do
|
|
41
|
+
# be_present self['name'], 'Name cannot be blank'
|
|
42
|
+
# be_present self['email'], 'Email cannot be blank'
|
|
43
|
+
# be_present self['age'], 'Age cannot be blank'
|
|
44
|
+
# be_present self['password'], 'Password cannot be blank'
|
|
45
|
+
# be_a_number self['age'], 'Age must be a number'
|
|
46
|
+
# be_of_length self['password'], 'Password must be at least 8 characters', minimum: 8
|
|
47
|
+
# end
|
|
48
|
+
# end
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
# def create_indexes
|
|
51
|
+
# self.collection.create_index('name', unique: true)
|
|
52
|
+
# self.collection.create_index('email', unique: true)
|
|
53
|
+
# self.collection.create_index('age')
|
|
54
|
+
# end
|
|
55
|
+
def validate
|
|
56
|
+
expectations do
|
|
57
|
+
!FIELDS!
|
|
58
|
+
!INTEGERS!
|
|
59
|
+
end
|
|
59
60
|
end
|
|
60
61
|
end
|
|
61
|
-
|
|
62
|
-
end
|
|
63
62
|
MODEL
|
|
64
63
|
|
|
65
|
-
# options => { :
|
|
66
|
-
def create_model_file(name, options={})
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
64
|
+
# options => { fields: ['title:string', 'body:string'], app: 'app' }
|
|
65
|
+
def create_model_file(name, options = {})
|
|
66
|
+
model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
|
|
67
|
+
column_declarations = []
|
|
68
|
+
integers = []
|
|
69
|
+
|
|
70
|
+
options[:fields].each do |value|
|
|
71
|
+
field, kind = value.split(':')
|
|
72
|
+
column_declarations << "be_present self['#{field}'], '#{field} cannot be blank'"
|
|
73
|
+
integers << "be_a_number self['#{field}'], '#{field} must be a number'" if kind =~ /[Ii]nteger/
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
model_contents = MONGOMATIC_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
|
|
77
|
+
model_contents.gsub!(/!FIELDS!/, column_declarations.join("\n "))
|
|
78
|
+
model_contents.gsub!(/!INTEGERS!/, integers.join("\n "))
|
|
79
|
+
create_file(model_path, model_contents)
|
|
76
80
|
end
|
|
77
81
|
|
|
78
82
|
def create_model_migration(filename, name, fields)
|