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

Project submitted #1157

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'http://rubygems.org'
gem 'sinatra'
gem 'activerecord', '~> 6.0.0', :require => 'active_record'
gem 'sinatra-activerecord', :require => 'sinatra/activerecord'
gem 'rake'
gem 'rake', '13.0.1'
gem 'require_all'
gem 'sqlite3'
gem 'thin'
Expand Down
107 changes: 58 additions & 49 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)
mini_portile2 (2.5.0)
minitest (5.14.4)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.11.1)
mini_portile2 (~> 2.5.0)
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-protection (2.1.0)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rake (13.0.1)
regexp_parser (1.6.0)
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,11 +103,11 @@ 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
Expand All @@ -110,7 +119,7 @@ DEPENDENCIES
database_cleaner
pry
rack-test
rake
rake (= 13.0.1)
require_all
rspec
shotgun
Expand All @@ -121,4 +130,4 @@ DEPENDENCIES
tux

BUNDLED WITH
2.0.2
2.0.1
20 changes: 20 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ class ApplicationController < Sinatra::Base
configure do
set :public_folder, 'public'
set :views, 'app/views'
enable :sessions
set :session_secret, "fwitter_secret"
end

#home
get '/' do
erb :index
end

#helpers

helpers do
#find current user if the user logged in before
def current_user
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end

def is_logged_in?
!!current_user
end

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

get '/tweets' do
if is_logged_in?
@tweets = Tweet.all
erb :'tweets/tweets'
else
redirect "/login"
end
end

get '/tweets/new' do
if is_logged_in?
erb :'/tweets/new'
else
redirect '/login'
end
end

post '/tweets' do
if is_logged_in?
if params[:content].empty?
redirect "/tweets/new"
else

@tweet=Tweet.create(:content => params[:content])
@tweet.user = current_user
@tweet.save
redirect "/tweets/#{@tweet.id}"
end

else
redirect "/tweets/new"
end

end

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

end

delete '/tweets/:id/delete' do
if is_logged_in?
@tweet= Tweet.find_by_id(params[:id])
if @tweet && @tweet.user == current_user
@tweet.delete
end
redirect "/tweets"
else
redirect "/login"
end
end

get '/tweets/:id/edit' do
if is_logged_in?
@tweet=Tweet.find_by_id(params[:id])
if @tweet && @tweet.user == current_user
erb :'tweets/edit_tweet'
else
redirect "/tweets"
end
else
redirect "/login"
end
end

patch '/tweets/:id' do
if is_logged_in?

@tweet=Tweet.find_by_id(params[:id])
if params[:content].empty?
redirect "/tweets/#{params[:id]}/edit"
else
if @tweet && @tweet.user == current_user

@tweet.update(:content => params[:content])
redirect "/tweets/#{@tweet.id}"
else
redirect "/tweets/#{@tweet.id}/edit"
end
end
else
redirect "/login"
end
end

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

get '/users/:slug' do
@user = User.find_by_slug(params[:slug])
erb :'users/show'
end

get '/login' do

if !is_logged_in?
erb :'users/login'
else
redirect "/tweets"
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
redirect "/signup"
end
end

get '/signup' do
if is_logged_in?
redirect '/tweets'
else
erb :'users/create_user'
end

end

post '/signup' do

if !params[:username].empty? && !params[:email].empty? && !params[:password].empty?
@user = User.create(:username => params[:username], :email => params[:email], :password => params[:password])
session[:user_id]= @user.id
redirect '/tweets'
else
redirect "/signup"
end
end

get '/logout' do
if is_logged_in?
session.destroy
end
redirect "/login"
end


end
11 changes: 11 additions & 0 deletions app/helpers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#this is not in the lab file structure, but rspec requires that a Helpers class exists with the tweet/new.erb view download from github.
class Helpers

def self.current_user(session_hash) #get all user info from data base using session user id
@user = User.find(session_hash[:user_id])
end
def self.is_logged_in?(session_hash) #verified that the user who log on is the same save in the session.
!!session_hash[:user_id]
end
end
#this methods are used in the views instead of use the variables @user, etc directly.
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
class User < ActiveRecord::Base
has_secure_password
has_many :tweets

def slug
username.downcase.gsub(' ', '-')
end

def self.find_by_slug(slug)
User.all.find { |u| u.slug == slug }
end
end
12 changes: 12 additions & 0 deletions app/views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>Twitter</h1>

<nav>

<a href="/login">Log In</a>
<a href="/signup">Sign In</a>


</nav>
<div>
<h2></h2>
</div>
Loading