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

tor - octos - mediaranker-revisited #45

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@

# Ignore Byebug command history file.
.byebug_history
.env
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git_source(:github) do |repo_name|
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
gem 'rails', '~> 5.1.6'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
Expand Down Expand Up @@ -40,6 +40,9 @@ gem 'jbuilder', '~> 2.5'
gem 'foundation-rails'
gem 'autoprefixer-rails'

gem 'omniauth'
gem 'omniauth-github'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
Expand All @@ -65,6 +68,8 @@ 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
115 changes: 71 additions & 44 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.7)
actionpack (= 5.0.7)
nio4r (>= 1.2, < 3.0)
actioncable (5.1.6)
actionpack (= 5.1.6)
nio4r (~> 2.0)
websocket-driver (~> 0.6.1)
actionmailer (5.0.7)
actionpack (= 5.0.7)
actionview (= 5.0.7)
activejob (= 5.0.7)
actionmailer (5.1.6)
actionpack (= 5.1.6)
actionview (= 5.1.6)
activejob (= 5.1.6)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.7)
actionview (= 5.0.7)
activesupport (= 5.0.7)
actionpack (5.1.6)
actionview (= 5.1.6)
activesupport (= 5.1.6)
rack (~> 2.0)
rack-test (~> 0.6.3)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.7)
activesupport (= 5.0.7)
actionview (5.1.6)
activesupport (= 5.1.6)
builder (~> 3.1)
erubis (~> 2.7.0)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.0.7)
activesupport (= 5.0.7)
activejob (5.1.6)
activesupport (= 5.1.6)
globalid (>= 0.3.6)
activemodel (5.0.7)
activesupport (= 5.0.7)
activerecord (5.0.7)
activemodel (= 5.0.7)
activesupport (= 5.0.7)
arel (~> 7.0)
activesupport (5.0.7)
activemodel (5.1.6)
activesupport (= 5.1.6)
activerecord (5.1.6)
activemodel (= 5.1.6)
activesupport (= 5.1.6)
arel (~> 8.0)
activesupport (5.1.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
ansi (1.5.0)
arel (7.1.4)
arel (8.0.0)
autoprefixer-rails (8.2.0)
execjs
babel-source (5.8.35)
Expand All @@ -62,18 +62,24 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.3)
crass (1.0.4)
dotenv (2.2.2)
dotenv-rails (2.2.2)
dotenv (= 2.2.2)
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)
i18n (1.0.0)
hashie (3.5.7)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
Expand All @@ -82,6 +88,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.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,39 +115,56 @@ 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)
rack (2.0.4)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.7)
actioncable (= 5.0.7)
actionmailer (= 5.0.7)
actionpack (= 5.0.7)
actionview (= 5.0.7)
activejob (= 5.0.7)
activemodel (= 5.0.7)
activerecord (= 5.0.7)
activesupport (= 5.0.7)
rack (2.0.5)
rack-test (1.0.0)
rack (>= 1.0, < 3)
rails (5.1.6)
actioncable (= 5.1.6)
actionmailer (= 5.1.6)
actionpack (= 5.1.6)
actionview (= 5.1.6)
activejob (= 5.1.6)
activemodel (= 5.1.6)
activerecord (= 5.1.6)
activesupport (= 5.1.6)
bundler (>= 1.3.0)
railties (= 5.0.7)
railties (= 5.1.6)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.0.7)
actionpack (= 5.0.7)
activesupport (= 5.0.7)
railties (5.1.6)
actionpack (= 5.1.6)
activesupport (= 5.1.6)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
Expand Down Expand Up @@ -199,6 +223,7 @@ DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.2)
dotenv-rails
foundation-rails
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -207,10 +232,12 @@ DEPENDENCIES
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
rails (~> 5.0.2)
rails (~> 5.1.6)
sass-rails (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :find_user
# before_action :require_login #____WHY IS THIS NOT WORKING____

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


private
# def require_login
# unless session[:user_id]
# flash[:failure] = 'You must be logged in to do that'
# redirect_to root_path
# end
# end

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

end
33 changes: 14 additions & 19 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
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}"
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
def create
auth_hash = request.env['omniauth.auth']

if auth_hash['uid']
user = User.get_user(auth_hash)
if user.nil?
flash[:result_text] = 'Could not log in'
flash[:messages] = user.errors.messages
redirect_to root_path
return
end
session[:user_id] = user.id
flash[:success] = 'Successfully logged in'
else
flash[:result_text] = "Could not log in"
end

redirect_to root_path
end

Expand Down
10 changes: 10 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class UsersController < ApplicationController
before_action :require_login

def index
@users = User.all
end
Expand All @@ -7,4 +9,12 @@ def show
@user = User.find_by(id: params[:id])
render_404 unless @user
end

private

def require_login
unless session[:user_id]
redirect_to root_path
end
end
end
10 changes: 10 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class WorksController < ApplicationController
# 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 @@ -91,4 +93,12 @@ def category_from_work
render_404 unless @work
@media_category = @work.category.downcase.pluralize
end

def require_login
unless session[:user_id]
flash[:failure] = 'You must be logged in to do that'
redirect_to root_path
end
end

end
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module ApplicationHelper
def render_date(date)
date.strftime("%b %e, %Y")
end

def email_or_name(user)
return user.name ? user.name : user.email
end
end
23 changes: 21 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
class User < ApplicationRecord
has_many :votes
has_many :votes, dependent: :destroy
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true
validates :uid, presence: true, uniqueness: true
validates :provider, presence: true
validates :email, presence: true # might be redundant with github needing an email

def self.get_user(data_hash)
user = User.find_by(uid: data_hash[:uid], provider: data_hash[:provider])
if user.nil?
user_data = {
uid: data_hash['uid'],
provider: data_hash['provider'],
name: data_hash['info']['name'],
email: data_hash['info']['email']
}
user = User.new(user_data)
return user.save ? user : nil
end
return user
end


end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
</div>
<div class="user-nav">
<% if @login_user %>
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "button" %>
<%= link_to "Logged in as #{email_or_name(@login_user)}", 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" %>
<%= link_to "Log In", '/auth/github', class: "button" %>
<% end %>
</div>
</nav>
Expand Down
Loading