Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #1158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Done #1158

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem 'shotgun'
gem 'pry'
gem 'bcrypt'
gem "tux"
gem 'rack-flash3'

group :test do
gem 'rspec'
Expand Down
110 changes: 60 additions & 50 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (6.0.1)
activesupport (= 6.0.1)
activerecord (6.0.1)
activemodel (= 6.0.1)
activesupport (= 6.0.1)
activesupport (6.0.1)
activemodel (6.0.3.5)
activesupport (= 6.0.3.5)
activerecord (6.0.3.5)
activemodel (= 6.0.3.5)
activesupport (= 6.0.3.5)
activesupport (6.0.3.5)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
zeitwerk (~> 2.2, >= 2.2.2)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
bcrypt (3.1.13)
bcrypt (3.1.16)
bond (0.5.1)
capybara (3.29.0)
capybara (3.35.3)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
coderay (1.1.2)
concurrent-ruby (1.1.5)
coderay (1.1.3)
concurrent-ruby (1.1.8)
daemons (1.3.1)
database_cleaner (1.7.0)
diff-lcs (1.3)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
diff-lcs (1.4.4)
eventmachine (1.2.7)
i18n (1.7.0)
i18n (1.8.9)
concurrent-ruby (~> 1.0)
method_source (0.9.2)
method_source (1.0.0)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.13.0)
mustermann (1.0.3)
nokogiri (1.10.8)
mini_portile2 (~> 2.4.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.1)
minitest (5.14.4)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.11.1-x86_64-darwin)
racc (~> 1.4)
pry (0.14.0)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
racc (1.5.2)
rack (2.2.3)
rack-protection (2.0.7)
rack-flash3 (1.0.5)
rack
rack-protection (2.1.0)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rake (13.0.1)
regexp_parser (1.6.0)
rake (13.0.3)
regexp_parser (2.1.1)
require_all (3.0.0)
ripl (0.7.1)
bond (~> 0.5.1)
Expand All @@ -59,31 +67,32 @@ GEM
rack (>= 1.0)
rack-test (>= 0.5)
ripl (>= 0.3.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.0)
rspec-support (~> 3.9.0)
rspec-expectations (3.9.0)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
ruby2_keywords (0.0.4)
shotgun (0.9.2)
rack (>= 1.0)
sinatra (2.0.7)
sinatra (2.1.0)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.7)
rack (~> 2.2)
rack-protection (= 2.1.0)
tilt (~> 2.0)
sinatra-activerecord (2.0.14)
activerecord (>= 3.2)
sinatra-activerecord (2.0.22)
activerecord (>= 4.1)
sinatra (>= 1.0)
sqlite3 (1.4.1)
thin (1.7.2)
sqlite3 (1.4.2)
thin (1.8.0)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
Expand All @@ -94,21 +103,22 @@ GEM
ripl-multi_line (>= 0.2.4)
ripl-rack (>= 0.2.0)
sinatra (>= 1.2.1)
tzinfo (1.2.5)
tzinfo (1.2.9)
thread_safe (~> 0.1)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.2.1)
zeitwerk (2.4.2)

PLATFORMS
ruby
x86_64-darwin-19

DEPENDENCIES
activerecord (~> 6.0.0)
bcrypt
capybara
database_cleaner
pry
rack-flash3
rack-test
rake
require_all
Expand All @@ -121,4 +131,4 @@ DEPENDENCIES
tux

BUNDLED WITH
2.0.2
2.2.7
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ require 'sinatra/activerecord/rake'



# Type `rake -T` on your command line to see the available rake tasks.
# Type `rake -T` on your command line to see the available rake tasks.

task :console do
Pry.start
end
61 changes: 61 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,71 @@
require './config/environment'
require 'rack-flash'

class ApplicationController < Sinatra::Base
use Rack::Flash

configure do
register Sinatra::ActiveRecordExtension
enable :sessions
set :session_secret, "my_application_secret"
set :public_folder, 'public'
set :views, 'app/views'
end

get '/' do
erb :index
end

get '/signup' do
if session[:user_id] == nil
erb :signup
else
redirect '/tweets'
end
end

post '/signup' do
user = User.new(:username => params[:username], :email=> params[:email], :password => params[:password])
if user.username.empty? || user.email.empty? || !user.save
flash[:notice] = "User creation unsuccessful, please try again."
redirect '/signup'
else user.save
session[:user_id] = user.id
redirect '/tweets'
end
end

get '/login' do
if Helpers.is_logged_in?(session)
redirect '/tweets'
else
erb :'/users/login'
end
end

post "/login" do
@user = User.find_by(:username => params[:username])

if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect "/tweets"
else
flash[:notice] = "Login unsuccessful, please try again."
redirect "/login"
end
end

get '/logout' do
if session[:user_id] == nil
redirect "/"
else
session.clear
redirect '/login'
end
end





end
12 changes: 12 additions & 0 deletions app/controllers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Helpers < ApplicationController


def self.is_logged_in?(session)
!!session[:user_id]
end

def self.current_user(user)
@user= User.find(id: user[:user_id])
end

end
70 changes: 70 additions & 0 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
class TweetsController < ApplicationController

get '/tweets' do
if Helpers.is_logged_in?(session)
@user = User.find_by(id: session[:user_id])
erb :'/tweets/tweets'
else
redirect '/login'
end
end

get '/tweets/new' do
if Helpers.is_logged_in?(session)
erb :'/tweets/new'
else
redirect '/login'
end
end

post '/tweets' do
if params[:content] != ""
@tweet = Tweet.create(:content => params[:content])
@tweet.user = User.find_by(:id => session[:user_id])
@tweet.save
redirect "/tweets"
else
redirect "/tweets/new"
end

end

get '/tweets/:id' do
if Helpers.is_logged_in?(session)
@tweet = Tweet.find(params[:id])
erb :'/tweets/show'
else
redirect '/login'
end
end

delete '/tweets/:id/delete' do
@tweet= Tweet.find(params[:id])
if Helpers.is_logged_in?(session) && @tweet.user.id == session[:user_id]
@tweet.delete
redirect '/tweets'
else
redirect '/login'
end
end

get '/tweets/:id/edit' do
if Helpers.is_logged_in?(session) == false
redirect '/login'
else
@tweet = Tweet.find(params[:id])
erb :'/tweets/edit'
end

end

patch '/tweets/:id/edit' do
if Helpers.is_logged_in?(session)
@tweet = Tweet.find(params[:id])
end
if params[:updated_content] != ""
@tweet.content = params[:updated_content]
@tweet.save
end
redirect "/tweets/#{@tweet.id}/edit"
end
end



11 changes: 11 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
class UsersController < ApplicationController


get '/users/:slug' do
if Helper.is_logged_in?(session)
@user = User.all.find_by_slug(params[:slug])
erb :'/users/show'
else
redirect '/login'
end
end



end
11 changes: 11 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
class User < ActiveRecord::Base
has_secure_password
has_many :tweets


def slug
self.username.gsub(" ", "-").gsub(/[^\w\-]/,"").downcase
end


def self.find_by_slug(slug)
self.all.find{|s| s.slug == slug}
end

end
5 changes: 5 additions & 0 deletions app/views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Welcome</h1>

<a href= "/tweets">See Tweets Here </a>
<br>
<a href= "/tweets/new"> Create a New Tweet </a>
Loading