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

Selam Ainalem - Amperes #24

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GITHUB_CLIENT_ID: 583ab79edd6b31eebc57
GITHUB_CLIENT_SECRET: 6ba0970c131329005ed1a5c489f4fb5cab1b320d
10 changes: 9 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

ruby '2.4.0'


git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
Expand Down Expand Up @@ -56,6 +56,7 @@ group :test do
gem 'minitest-reporters'
gem 'minitest-spec-rails'
gem 'minitest-skip'
gem 'simplecov', require: false
end

group :development do
Expand All @@ -65,7 +66,14 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'

end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem "omniauth"
gem "omniauth-github"

# gem 'simplecov', require: false, group: :test
53 changes: 43 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GEM
tzinfo (~> 1.1)
ansi (1.5.0)
arel (7.1.4)
autoprefixer-rails (8.2.0)
autoprefixer-rails (8.3.0)
execjs
babel-source (5.8.35)
babel-transpiler (0.7.0)
Expand All @@ -62,17 +62,25 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.3)
crass (1.0.4)
docile (1.3.0)
dotenv (2.3.0)
dotenv-rails (2.3.0)
dotenv (= 2.3.0)
railties (>= 3.2, < 6.0)
erubi (1.7.1)
erubis (2.7.0)
execjs (2.7.0)
faraday (0.12.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.23)
foundation-rails (6.4.3.0)
railties (>= 3.1.0)
sass (>= 3.3.0, < 3.5)
sprockets-es6 (>= 0.9.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashie (3.5.7)
i18n (1.0.0)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
Expand All @@ -82,6 +90,8 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.1.0)
jwt (1.5.6)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -108,16 +118,33 @@ GEM
minitest (~> 5.0)
rails (>= 4.1)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.3.0)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
oauth2 (1.4.0)
faraday (>= 0.8, < 0.13)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.8.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
omniauth-github (1.3.0)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-oauth2 (1.5.0)
oauth2 (~> 1.1)
omniauth (~> 1.2)
pg (0.21.0)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-rails (0.3.6)
pry (>= 0.10.4)
puma (3.11.3)
puma (3.11.4)
rack (2.0.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -156,6 +183,11 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand All @@ -175,14 +207,14 @@ GEM
thor (0.20.0)
thread_safe (0.3.6)
tilt (2.0.8)
turbolinks (5.1.0)
turbolinks (5.1.1)
turbolinks-source (~> 5.1)
turbolinks-source (5.1.0)
tzinfo (1.2.5)
thread_safe (~> 0.1)
uglifier (4.1.8)
uglifier (4.1.9)
execjs (>= 0.3.0, < 3)
web-console (3.5.1)
web-console (3.6.0)
actionview (>= 5.0)
activemodel (>= 5.0)
bindex (>= 0.4.0)
Expand All @@ -199,6 +231,7 @@ DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.2)
dotenv-rails
foundation-rails
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -207,20 +240,20 @@ DEPENDENCIES
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
rails (~> 5.0.2)
sass-rails (~> 5.0)
simplecov
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)

RUBY VERSION
ruby 2.4.0p0

BUNDLED WITH
1.15.4
1.16.1
10 changes: 9 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ def render_404
raise ActionController::RoutingError.new('Not Found')
end

def require_login
if session[:user_id].nil?
flash[:status] = :failure
flash.now[:result_text] = "Please log in for access to the entire site."
redirect_to root_path
end
end

private
def find_user
if session[:user_id]
if @user.nil?
@login_user = User.find_by(id: session[:user_id])
end
end
Expand Down
86 changes: 65 additions & 21 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,78 @@
class SessionsController < ApplicationController
def login_form
end

def login
username = params[:username]
if username and user = User.find_by(username: username)
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully logged in as existing user #{user.username}"
else
user = User.new(username: username)
if user.save
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
def create
auth_hash = request.env['omniauth.auth']
if auth_hash['uid']
@user = User.find_by(uid: auth_hash['uid'])
if @user.nil?
create_new_user
else
flash.now[:status] = :failure
flash.now[:result_text] = "Could not log in"
flash.now[:messages] = user.errors.messages
render "login_form", status: :bad_request
return
flash[:status] = :success
flash[:result_text] = "Logged in successfully, welcome back #{@user.username}"
end
session[:user_id] = @user.id
else
flash[:status] = :failure
flash[:result_text] = "Log in has failed"
end
redirect_to root_path
end

def logout
def destroy
session[:user_id] = nil
flash[:status] = :success
flash[:result_text] = "Successfully logged out"
flash[:result_text] = "You've logged out"
redirect_to root_path
end

private
def create_new_user
auth_hash = request.env['omniauth.auth']
@user = User.new(username: auth_hash['info']['nickname'], email: auth_hash['info']['email'], uid: auth_hash['uid'], provider: 'Github')
if @user.save
flash[:status] = :success
flash[:result_text] = "Your account has been generated #{@user.username}!"
else
flash[:status] = :failure
flash[:result_text] = "Something has gone wrong in the account generation process."
end
end
# def logout
# session[:user_id] = nil
# flash[:status] = :success
# flash[:result_text] = "Successfully logged out"
# redirect_to root_path
# end

# def login_form
# end

# def login
# username = params[:username]
# if username and user = User.find_by(username: username)
# session[:user_id] = user.id
# flash[:status] = :success
# flash[:result_text] = "Successfully logged in as existing user #{user.username}"
# else
# user = User.new(username: username)
# if user.save
# session[:user_id] = user.id
# flash[:status] = :success
#
# flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
# else
#
# flash.now[:status] = :failure
# flash.now[:result_text] = "Could not log in"
# flash.now[:messages] = user.errors.messages
#
# render "login_form", status: :bad_request
# return
# end
# end
# redirect_to root_path
# end



end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class UsersController < ApplicationController
before_action :require_login
def index
@users = User.all
end
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class WorksController < ApplicationController
# We should always be able to tell what category
# of work we're dealing with
before_action :category_from_work, except: [:root, :index, :new, :create]

before_action :require_login, except:[:root]
def root
@albums = Work.best_albums
@books = Work.best_books
Expand Down Expand Up @@ -50,7 +49,7 @@ def update
flash.now[:status] = :failure
flash.now[:result_text] = "Could not update #{@media_category.singularize}"
flash.now[:messages] = @work.errors.messages
render :edit, status: :not_found
render :edit, status: :bad_request
end
end

Expand All @@ -62,9 +61,12 @@ def destroy
end

def upvote

flash[:status] = :failure

if @login_user
vote = Vote.new(user: @login_user, work: @work)

if vote.save
flash[:status] = :success
flash[:result_text] = "Successfully upvoted!"
Expand All @@ -75,7 +77,6 @@ def upvote
else
flash[:result_text] = "You must log in to do that"
end

# Refresh the page to show either the updated vote count
# or the error message
redirect_back fallback_location: work_path(@work)
Expand Down
14 changes: 8 additions & 6 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
<%= link_to "View all users", users_path, class: "button" %>
</div>
<div class="user-nav">
<% if @login_user %>
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "button" %>
<%= link_to "Log Out", logout_path, method: :post, class: "button" %>
<% else %>
<%= link_to "Log In", login_path, class: "button" %>
<% end %>
<% if session[:user_id] %>
<% user = User.find(session[:user_id]) %>
<%= link_to "Logged in as #{user.username}", logout_path, method: "delete" , class: "button"%>
<% else %>
<%= link_to "Login with Github", "/auth/github" , class: "button"%>
<%end%>


</div>
</nav>
</header>
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email"
end
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'works#root'
get '/login', to: 'sessions#login_form', as: 'login'
post '/login', to: 'sessions#login'
post '/logout', to: 'sessions#logout', as: 'logout'
get "auth/:provider/callback", to: "sessions#create", as: 'auth_callback'
delete '/logout', to:'sessions#destroy', as: 'logout'

# get '/login', to: 'sessions#login_form', as: 'login'
# post '/login', to: 'sessions#login'
# post '/logout', to: 'sessions#logout', as: 'logout'

resources :works
post '/works/:id/upvote', to: 'works#upvote', as: 'upvote'
Expand Down
5 changes: 5 additions & 0 deletions coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"result": {
"covered_percent": 90.85
}
}
Loading