Class: Hoboken::AddOns::ActiveRecord
- Inherits:
-
Group
- Object
- Thor::Group
- Group
- Hoboken::AddOns::ActiveRecord
show all
- Defined in:
- lib/hoboken/add_ons/active_record.rb
Overview
ActiveRecord database access via sinatra-activerecord gem.
Instance Method Summary
collapse
Methods inherited from Group
#classic?, #modular?, #rspec?, #rubocop?, #sequel?, source_root
#gem, #indent
Instance Method Details
#add_database_spec_helper ⇒ Object
rubocop:disable Metrics/MethodLength
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/hoboken/add_ons/active_record.rb', line 75
def add_database_spec_helper
return unless rspec?
insert_into_file('spec/spec_helper.rb', before: /ENV\['RACK_ENV'\] = 'test'/) do
"ENV['DATABASE_URL'] = 'sqlite3:db/test.db'\n"
end
snippet = <<~CODE
config.around do |example|
ActiveRecord::Base.transaction do
example.run
ActiveRecord::Rollback
end
end
CODE
location = /RSpec\.configure do \|config\|\n/
insert_into_file('spec/spec_helper.rb', after: location) do
"#{indent(snippet, 2)}\n"
end
end
|
#add_database_test_helper_class ⇒ Object
rubocop:disable Metrics/MethodLength
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/hoboken/add_ons/active_record.rb', line 50
def add_database_test_helper_class
return if rspec?
insert_into_file('test/test_helper.rb', before: /ENV\['RACK_ENV'\] = 'test'/) do
"ENV['DATABASE_URL'] = 'sqlite3:db/test.db'\n"
end
snippet = <<~CODE
def run(*args, &block)
result = nil
ActiveRecord::Base.connection.transaction do
result = super
raise ActiveRecord::Rollback
end
result
end
CODE
insert_into_file('test/test_helper.rb', after: /include RackHelpers\n/) do
"\n#{indent(snippet, 6)}"
end
end
|
#add_gems ⇒ Object
8
9
10
11
|
# File 'lib/hoboken/add_ons/active_record.rb', line 8
def add_gems
gem 'sinatra-activerecord', version: '2.0'
gem 'sqlite3', version: '1.4', group: %i[development test]
end
|
#copy_example_seeds_file ⇒ Object
18
19
20
|
# File 'lib/hoboken/add_ons/active_record.rb', line 18
def copy_example_seeds_file
copy_file('hoboken/templates/seeds.rb', 'db/seeds.rb')
end
|
#copy_rake_task ⇒ Object
22
23
24
|
# File 'lib/hoboken/add_ons/active_record.rb', line 22
def copy_rake_task
copy_file('hoboken/templates/active_record.rake', 'tasks/active_record.rake')
end
|
#reminders ⇒ Object
rubocop:enable Metrics/MethodLength
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/hoboken/add_ons/active_record.rb', line 127
def reminders
say "\nGemfile updated... don't forget to 'bundle install'"
say <<~TEXT
Notes:
* The sqlite3 gem has been installed for dev and test environments
only. You will need to specify a gem to use for production.
* You will need to specify an environment variable 'DATABASE_URL'
(either add it to .env or export it). For example, in your .env
file add: `DATABASE_URL=sqlite3:db/development.db`.
TEXT
end
|
#require_sinatra_activerecord ⇒ Object
26
27
28
29
30
31
|
# File 'lib/hoboken/add_ons/active_record.rb', line 26
def require_sinatra_activerecord
location = %r{require_relative '\.\./app'}
insert_into_file('config/environment.rb', before: location) do
"require 'sinatra/activerecord'\n\n"
end
end
|
#set_database_variable ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/hoboken/add_ons/active_record.rb', line 33
def set_database_variable
snippet = "set :database, ENV['DATABASE_URL']"
snippet = "register Sinatra::ActiveRecordExtension\n#{snippet}" if modular?
indentation = classic? ? 2 : 6
location = /set :erb.+\n/
insert_into_file('config/environment.rb', after: location) do
"\n#{indent(snippet, indentation)}\n"
end
end
|
#setup_directories ⇒ Object
13
14
15
16
|
# File 'lib/hoboken/add_ons/active_record.rb', line 13
def setup_directories
empty_directory('db/migrate')
empty_directory('tasks')
end
|
#setup_puma_config ⇒ Object
43
44
45
46
47
|
# File 'lib/hoboken/add_ons/active_record.rb', line 43
def setup_puma_config
insert_into_file('config/puma.rb', after: 'on_worker_boot do') do
"\n ActiveRecord::Base.establish_connection"
end
end
|
#update_readme ⇒ Object
rubocop:disable Metrics/MethodLength
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
|
# File 'lib/hoboken/add_ons/active_record.rb', line 99
def update_readme
snippet = <<~CODE
<tr>
<td>DATABASE_URL</td>
<td>Yes</td>
<td>
`sqlite3:db/test.db` (for the test environment <em>only</em>)
</td>
<td>
Connection URL to the database. The format varies according
database adapter. Refer to the documentation for the adapter
you're using for more information. Some examples:
<dl>
<dt>Sqlite3</dt>
<dd>`sqlite3:db/development.db`</dd>
<dt>PostgreSQL</dt>
<dd>`postgresql://localhost/myapp_development?pool=5`</dd>
</dl>
</td>
</tr>
CODE
insert_into_file('README.md', after: /<tbody>\n/) do
indent(snippet, 8)
end
end
|