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

goeun / edges / media ranker 2.0 #37

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore .env
.env

# Ignore bundler config.
/.bundle

Expand Down
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails'

gem 'autoprefixer-rails', '8.6.5'

gem "omniauth"
gem "omniauth-github"

# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
Expand Down Expand Up @@ -56,6 +62,7 @@ group :test do
end

group :development do
gem 'dotenv-rails'
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
Expand Down
35 changes: 32 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GEM
tzinfo (~> 1.1)
ansi (1.5.0)
arel (9.0.0)
autoprefixer-rails (9.1.4)
autoprefixer-rails (8.6.5)
execjs
better_errors (2.5.0)
coderay (>= 1.0.0)
Expand All @@ -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)
nokogiri (1.8.5)
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 @@ -202,18 +227,22 @@ PLATFORMS
ruby

DEPENDENCIES
autoprefixer-rails (= 8.6.5)
better_errors
binding_of_caller
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 All @@ -227,4 +256,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.16.5
1.16.4
14 changes: 12 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :find_user
before_action :find_user, :require_login

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])
end
end


def require_login
# if @login_user.nil?
# flash[:error] = "You must be logged in to view this"
# redirect_to root_path

Choose a reason for hiding this comment

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

I love the idea of putting this in a controller filter - why is this commented out?

# end

end

end
36 changes: 20 additions & 16 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
class SessionsController < ApplicationController
def login_form
end
skip_before_action :require_login

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

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}"
user = User.find_by(uid: auth_hash[:uid], provider: 'github')
if user
flash[:success] = "Logged in as returning user #{user.name}"
else
user = User.new(username: username)
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[:success] = "Logged in as new user #{user.name}"
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
flash[:error] = "Could not create new user account: #{user.errors.messages}"
redirect_to root_path
return
end
end
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"
redirect_to root_path
end

def destroy
session[:user_id] = nil
flash[:success] = "Successfully logged out!"

Choose a reason for hiding this comment

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

Do you need both destroy and logout here?


redirect_to root_path
end
end
5 changes: 4 additions & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 @@ -63,6 +63,9 @@ def destroy

def upvote
flash[:status] = :failure

puts "session is: #{session[:user_id]}"
puts "login is: #{@login_user} and work is: #{@work}"
if @login_user
vote = Vote.new(user: @login_user, work: @work)
if vote.save
Expand Down
14 changes: 13 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@ class User < ApplicationRecord
has_many :votes
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true
validates :name, uniqueness: true, presence: true

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

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

end
8 changes: 5 additions & 3 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@
<% if @login_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 #{@login_user.name}", user_path(@login_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 "Log In with Github", login_path, class: "btn btn-primary" %>

</li>
<% end %>

Expand Down
6 changes: 3 additions & 3 deletions app/views/sessions/login_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<%= form_tag do %>
<div class="form-group">
<%= label_tag :username, "Username" %>
<%= text_field_tag :username, nil, class: "form-control" %>
<%= label_tag :name, "name" %>
<%= text_field_tag :name, nil, class: "form-control" %>
</div>
<div class="form-group">
<%= submit_tag "Log In", class: "btn btn-primary" %>
Expand All @@ -15,7 +15,7 @@
<p>
There is no password field. In fact, there is no indication whatsoever
that you are who you say you are. There's nothing special about users -
username is just another piece of data that the user entered and we have to keep
name is just another piece of data that the user entered and we have to keep
track of.
</p>

Expand Down
4 changes: 2 additions & 2 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>name</th>
<th>Votes</th>
<th>Joined</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= link_to user.username, user_path(user) %></td>
<td><%= link_to user.name, user_path(user) %></td>
<td><%= user.votes.count %></td>
<td><%= render_date user.created_at %></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h2>User Summary: <%= link_to @user.username, user_path(@user) %></h2>
<h2>User Summary: <%= link_to @user.name, user_path(@user) %></h2>
<p>Joined site <%= render_date @user.created_at %></p>

<section class="user-votes__container">
Expand Down
2 changes: 1 addition & 1 deletion app/views/works/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<tbody>
<% @votes.each do |vote| %>
<tr>
<td><%= link_to vote.user.username, user_path(vote.user) %></td>
<td><%= link_to vote.user.name, user_path(vote.user) %></td>
<td><%= render_date vote.created_at %></td>
</li>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ development:
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: media-ranker-2_0
#name: media-ranker-2_0

# The password associated with the postgres role (username).
# The password associated with the postgres role (name).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a
Expand Down Expand Up @@ -81,5 +81,5 @@ test:
production:
<<: *default
database: media-ranker-2_0_production
username: media-ranker-2_0
name: media-ranker-2_0
password: <%= ENV['MEDIA-RANKER-2_0_DATABASE_PASSWORD'] %>
3 changes: 3 additions & 0 deletions config/initializers/ominiauth.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
14 changes: 10 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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'

resources :works
post '/works/:id/upvote', to: 'works#upvote', as: 'upvote'

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

get '/auth/github', to: 'sessions#login_form', as: 'login'
get "/auth/:provider/callback", to: "sessions#create", as: 'auth_callback'

post '/login', to: 'sessions#login'

delete "/logout", to: "sessions#destroy", as: "logout"



end
2 changes: 1 addition & 1 deletion db/migrate/20170329211434_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :username
t.string :name

t.timestamps
end
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20181016181701_add_github_oauth_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddGithubOauthToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :uid, :integer, null: false
add_column :users, :provider, :string, null: false
add_column :users, :email, :string
end
end
6 changes: 6 additions & 0 deletions db/migrate/20181016183042_change_username_to_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeuserToName < ActiveRecord::Migration[5.2]
def change
remove_column :users, :user, :string
add_column :users, :name, :string
end
end
Loading