Skip to content

Commit 4e697e1

Browse files
committed
need to run rake db:create
1 parent f8f3b69 commit 4e697e1

File tree

8 files changed

+138
-29
lines changed

8 files changed

+138
-29
lines changed

Gemfile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ source "https://rubygems.org"
66

77
gem "sinatra", "~> 2.2"
88
gem "sinatra-contrib", "~> 2.2"
9-
gem "pg", "~> 1.3"
109
gem "webrick", "~> 1.7"
1110
gem "rack-test", "~> 1.1"
1211

1312
gem "rspec", "~> 3.11"
1413

1514
gem "rainbow", "~> 3.1"
15+
16+
gem "activerecord", "~> 7.0"
17+
18+
gem "sinatra-activerecord", "~> 2.0"
19+
20+
gem "rake", "~> 13.0"
21+
gem "bcrypt", "~> 3.1"
22+
23+
gem "pg", "~> 1.4"

Gemfile.lock

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,84 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
activemodel (7.0.4)
5+
activesupport (= 7.0.4)
6+
activerecord (7.0.4)
7+
activemodel (= 7.0.4)
8+
activesupport (= 7.0.4)
9+
activesupport (7.0.4)
10+
concurrent-ruby (~> 1.0, >= 1.0.2)
11+
i18n (>= 1.6, < 2)
12+
minitest (>= 5.1)
13+
tzinfo (~> 2.0)
14+
bcrypt (3.1.18)
15+
concurrent-ruby (1.1.10)
416
diff-lcs (1.5.0)
17+
i18n (1.12.0)
18+
concurrent-ruby (~> 1.0)
19+
minitest (5.17.0)
520
multi_json (1.15.0)
6-
mustermann (1.1.1)
21+
mustermann (2.0.2)
722
ruby2_keywords (~> 0.0.1)
8-
pg (1.3.5)
9-
rack (2.2.3.1)
10-
rack-protection (2.2.0)
23+
pg (1.4.5)
24+
rack (2.2.6)
25+
rack-protection (2.2.4)
1126
rack
1227
rack-test (1.1.0)
1328
rack (>= 1.0, < 3)
1429
rainbow (3.1.1)
15-
rspec (3.11.0)
16-
rspec-core (~> 3.11.0)
17-
rspec-expectations (~> 3.11.0)
18-
rspec-mocks (~> 3.11.0)
19-
rspec-core (3.11.0)
20-
rspec-support (~> 3.11.0)
21-
rspec-expectations (3.11.0)
30+
rake (13.0.6)
31+
rspec (3.12.0)
32+
rspec-core (~> 3.12.0)
33+
rspec-expectations (~> 3.12.0)
34+
rspec-mocks (~> 3.12.0)
35+
rspec-core (3.12.0)
36+
rspec-support (~> 3.12.0)
37+
rspec-expectations (3.12.2)
2238
diff-lcs (>= 1.2.0, < 2.0)
23-
rspec-support (~> 3.11.0)
24-
rspec-mocks (3.11.1)
39+
rspec-support (~> 3.12.0)
40+
rspec-mocks (3.12.3)
2541
diff-lcs (>= 1.2.0, < 2.0)
26-
rspec-support (~> 3.11.0)
27-
rspec-support (3.11.0)
42+
rspec-support (~> 3.12.0)
43+
rspec-support (3.12.0)
2844
ruby2_keywords (0.0.5)
29-
sinatra (2.2.0)
30-
mustermann (~> 1.0)
45+
sinatra (2.2.4)
46+
mustermann (~> 2.0)
3147
rack (~> 2.2)
32-
rack-protection (= 2.2.0)
48+
rack-protection (= 2.2.4)
3349
tilt (~> 2.0)
34-
sinatra-contrib (2.2.0)
50+
sinatra-activerecord (2.0.26)
51+
activerecord (>= 4.1)
52+
sinatra (>= 1.0)
53+
sinatra-contrib (2.2.4)
3554
multi_json
36-
mustermann (~> 1.0)
37-
rack-protection (= 2.2.0)
38-
sinatra (= 2.2.0)
55+
mustermann (~> 2.0)
56+
rack-protection (= 2.2.4)
57+
sinatra (= 2.2.4)
3958
tilt (~> 2.0)
40-
tilt (2.0.10)
59+
tilt (2.0.11)
60+
tzinfo (2.0.5)
61+
concurrent-ruby (~> 1.0)
4162
webrick (1.7.0)
4263

4364
PLATFORMS
4465
arm64-darwin-20
4566
arm64-darwin-21
4667
x86_64-darwin-20
68+
x86_64-darwin-21
4769

4870
DEPENDENCIES
49-
pg (~> 1.3)
71+
activerecord (~> 7.0)
72+
bcrypt (~> 3.1)
73+
pg (~> 1.4)
5074
rack-test (~> 1.1)
5175
rainbow (~> 3.1)
76+
rake (~> 13.0)
5277
rspec (~> 3.11)
5378
sinatra (~> 2.2)
79+
sinatra-activerecord (~> 2.0)
5480
sinatra-contrib (~> 2.2)
5581
webrick (~> 1.7)
5682

5783
BUNDLED WITH
58-
2.4.3
84+
2.3.26

Rakefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# in Rakefile
2+
require "sinatra/activerecord/rake"
3+
4+
namespace :db do
5+
task :load_config do
6+
require "./app"
7+
end
8+
end

app.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
require 'sinatra/base'
22
require 'sinatra/reloader'
3+
require 'sinatra/activerecord'
4+
require 'bcrypt'
35

46
class Application < Sinatra::Base
57
configure :development do
68
register Sinatra::Reloader
79
end
10+
register Sinatra::ActiveRecordExtension
811

912
get '/' do
1013
return erb(:index)
1114
end
15+
end

config/database.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# in config/database.rb
2+
# set the database based on the current environment
3+
# The name cloudbnb in the below line is because my main app controller class in app.rb is called cloudbnb
4+
database_name = "cloudbnb-#{Application.environment}"
5+
db = URI.parse( ENV['DATABASE_URL'] || "postgres://localhost/#{database_name}")
6+
7+
# connect ActiveRecord with the current database
8+
ActiveRecord::Base.establish_connection(
9+
:adapter => db.scheme == "postgres" ? "postgresql" : db.scheme,
10+
:host => db.host,
11+
:port => db.port,
12+
:username => db.user,
13+
:password => db.password,
14+
:database => "#{database_name}",
15+
:encoding => "utf8"
16+
)

config/database.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# in config/database.yml
2+
# this defines the database for the development environment (default environment)
3+
development:
4+
# the type of database it will be
5+
adapter:
6+
postgresql
7+
# name of db to make
8+
database: cloudbnb-development
9+
host: localhost
10+
11+
# same but for test env
12+
test:
13+
adapter: postgresql
14+
database: cloudbnb-test

config/environment.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# in config/environment.rb
2+
# get the path of the root of the app
3+
# assuming that this file is one folder down in config/
4+
APP_ROOT = File.expand_path("..", __dir__)
5+
6+
# require the controller(s)
7+
# says that app.rb is in the root
8+
Dir.glob(File.join(APP_ROOT, "app", "*.rb")).each { |file| require file }
9+
10+
# require the model(s)
11+
# models stored in lib/
12+
Dir.glob(File.join(APP_ROOT, "lib", "*.rb")).each { |file| require file }
13+
14+
# require database configurations
15+
# the database setup files that were just made in config/
16+
require File.join(APP_ROOT, "config", "database")
17+
18+
# configure Chitter settings
19+
# This bit of config could also be done in the main app class definition
20+
# This is monkey-patching onto the main class definition
21+
# I suppose it's neater to move it here
22+
# The public/ folder is where images and stylesheets should be, the last line confirms that location
23+
class Application < Sinatra::Base
24+
set :method_override, true
25+
set :root, APP_ROOT
26+
set :views, File.join(APP_ROOT, "views")
27+
set :public_folder, File.join(APP_ROOT, "public")
28+
end

spec/spec_helper.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
ENV['ENV'] = 'test'
1+
# ENV['ENV'] = 'test'
22

3-
require 'database_connection'
3+
# require 'database_connection'
44

5-
DatabaseConnection.connect('makersbnb_test')
5+
# DatabaseConnection.connect('makersbnb_test')
6+
7+
# very important line to say that tests are run in "test" environment
8+
ENV["RACK_ENV"] = "test"
9+
# imports the actual controller file
10+
require_relative "../app"
611

712
# This file was generated by the `rspec --init` command. Conventionally, all
813
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.

0 commit comments

Comments
 (0)