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

MediaRanker-Revisited #22

Open
wants to merge 13 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@

# Ignore Byebug command history file.
.byebug_history

.env
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ gem 'jbuilder', '~> 2.5'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem "omniauth"
gem "omniauth-github"


gem 'bootstrap', '~> 4.1.3'

group :development, :test do
Expand Down Expand Up @@ -62,6 +66,7 @@ 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
Expand Down
28 changes: 28 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ GEM
concurrent-ruby (1.0.5)
crass (1.0.4)
debug_inspector (0.0.3)
dotenv (2.5.0)
dotenv-rails (2.5.0)
dotenv (= 2.5.0)
railties (>= 3.2, < 6.0)
erubi (1.7.1)
execjs (2.7.0)
faraday (0.15.3)
multipart-post (>= 1.2, < 3)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashie (3.5.7)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
Expand All @@ -84,6 +91,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jwt (2.1.0)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -113,9 +121,26 @@ 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.1)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
jwt (>= 1.0, < 3.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)
popper_js (1.14.3)
pry (0.11.3)
Expand Down Expand Up @@ -207,13 +232,16 @@ DEPENDENCIES
bootstrap (~> 4.1.3)
byebug
coffee-rails (~> 4.2)
dotenv-rails
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
minitest-rails
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
Expand Down
13 changes: 11 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ class ApplicationController < ActionController::Base

before_action :find_user


def render_404
# DPR: this will actually render a 404 page in production
raise ActionController::RoutingError.new('Not Found')
end

private
private

def find_user
if session[:user_id]
@login_user = User.find_by(id: session[:user_id])
@user = User.find_by(id: session[:user_id])
end
end

def require_login
if find_user.nil?
flash[:result_text] = "You must be logged in to view this section"
redirect_to root_path
end
end
end
50 changes: 32 additions & 18 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,47 @@ 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}"

def create
auth_hash = request.env['omniauth.auth']

user = User.find_by(uid: auth_hash[:uid], provider: 'github')
if user
# User was found in the database
flash[:result_text] = "Logged in as returning user #{user.username}"

else
user = User.new(username: username)
# User doesn't match anything in the DB
# Attempt to create a new user
user = User.build_from_github(auth_hash)

if user.save
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
flash[:result_text] = "Logged in as new user #{user.username}"

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
# Couldn't save the user for some reason. If we
# hit this it probably means there's a bug with the
# way we've configured GitHub. Our strategy will
# be to display error messages to make future
# debugging easier.

flash[:result_text] = "Could not create new user account: #{user.errors.messages}"
redirect_to root_path
return
end
end

# If we get here, we have a valid user instance
session[:user_id] = user.id
redirect_to root_path
end

def logout
session[:user_id] = nil
flash[:status] = :success
flash[:result_text] = "Successfully logged out"
def destroy
if @user
session[:user_id] = nil
flash[:result_text] = "Successfully logged out!"
end
redirect_to root_path
end

end
13 changes: 10 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
class UsersController < ApplicationController
before_action :require_login

def index
@users = User.all

@users = User.all

end

def show
@user = User.find_by(id: params[:id])
render_404 unless @user
unless @user.id == params[:id].to_i
flash[:result_text] = "Not allowed."
redirect_to root_path
end
end

end
22 changes: 12 additions & 10 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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 :find_user, except: [:root]
before_action :require_login, except: [:root]

def root
@albums = Work.best_albums
Expand All @@ -11,11 +13,12 @@ def root
end

def index
@works_by_category = Work.to_category_hash

@works_by_category = Work.to_category_hash
end

def new
@work = Work.new
@work = Work.new
end

def create
Expand All @@ -34,23 +37,25 @@ def create
end

def show
@votes = @work.votes.order(created_at: :desc)
@votes = @work.votes.order(created_at: :desc)
end

def edit
end

def update

@work.update_attributes(media_params)
if @work.save
flash[:status] = :success
flash[:result_text] = "Successfully updated #{@media_category.singularize} #{@work.id}"
redirect_to work_path(@work)
else

elsif @work && !@work.valid?
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 @@ -63,18 +68,15 @@ def destroy

def upvote
flash[:status] = :failure
if @login_user
vote = Vote.new(user: @login_user, work: @work)

vote = Vote.new(user: @user, work: @work)
if vote.save
flash[:status] = :success
flash[:result_text] = "Successfully upvoted!"
else
flash[:result_text] = "Could not upvote"
flash[:messages] = vote.errors.messages
end
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
Expand Down
14 changes: 14 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,18 @@ class User < ApplicationRecord
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true
# validates :uid, presence: true
# validates :provider, presence: true

def self.build_from_github(auth_hash)
user = User.new
user.uid = auth_hash[:uid]
user.provider = 'github'
user.username = auth_hash[:info][:name]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so you know, it's perfectly legal on Github to have an account with no :name field set.

user.email = auth_hash[:info][:email]

# Note that the user has not been saved
return user
end

end
9 changes: 5 additions & 4 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@
</li>
</ul>
<ul class="nav app-header__user-nav-container">
<% if @login_user %>

<% if @user %>

<li class="nav-item app-header__nav_item">
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "btn btn-primary" %>
<%= link_to "Logged in as #{@user.username}", user_path(@user), class: "btn btn-primary" %>
</li>
<li class="nav-item app-header__nav_item">
<%= link_to "Log Out", logout_path, method: :post, class: "btn btn-primary" %>
<%= link_to "Log Out", logout_path, method: :delete, class: "btn btn-primary" %>
</li>

<% else %>

<li class="nav-item app-header__nav_item">
<%= link_to "Log In", login_path, class: "btn btn-primary" %>
<%= link_to "Login with Github", "/auth/github", class: "btn btn-primary" %>
</li>
<% end %>

Expand Down
4 changes: 4 additions & 0 deletions app/views/works/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<% if @user %>

<h2>List of Works</h2>

<% @works_by_category.each do |category, works| %>
Expand Down Expand Up @@ -30,3 +32,5 @@

<%= link_to "View top media", root_path, class: "btn btn-secondary" %>
<%= link_to "Add a new work", new_work_path, class: "btn btn-primary" %>

<%end%>
2 changes: 2 additions & 0 deletions app/views/works/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<% if @user %>
<h2>Add a new work</h2>
<%= render partial: "form" %>
<%end%>
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

end
4 changes: 4 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email"
end
10 changes: 7 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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 '/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'

resources :users, only: [:index, :show]

get "/auth/:provider/callback", to: "sessions#create", as: "auth_callback"
delete "/logout", to: "sessions#destroy", as: "logout"

end
7 changes: 7 additions & 0 deletions db/migrate/20181016220353_add_user_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddUserColumn < ActiveRecord::Migration[5.2]
def change
add_column(:users, :email, :string)
add_column(:users, :uid, :integer)
add_column(:users, :provider, :string)
end
end
Loading