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

Ana Lisa Sutherland: Octos C9: MediaRanker-Revisited #31

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
5c8b36d
Have working tests for the def root method in Works Controller
The-Beez-Kneez Apr 16, 2018
d5e5551
Have working tests for index method in Works Controller, basically co…
The-Beez-Kneez Apr 16, 2018
8a5e08c
Have test working for new method in Works Controller, modeled after l…
The-Beez-Kneez Apr 16, 2018
2ca8add
Think I have all the create tests made and passing, still would like …
The-Beez-Kneez Apr 16, 2018
070014e
Created show method tests in work controller, all passing and testing…
The-Beez-Kneez Apr 17, 2018
46e092e
Added in Edit Method tests in Work Controller, followed same layout f…
The-Beez-Kneez Apr 17, 2018
9c39792
Finished Passing tests for Update Method in Works Controller, also fi…
The-Beez-Kneez Apr 17, 2018
df7fb7e
Completed passing tests for destroy method tests in Works Controller.
The-Beez-Kneez Apr 17, 2018
693f77a
Finished passing tests for Upvote method in Works Controller.
The-Beez-Kneez Apr 17, 2018
57505f9
Added in index method tests for User Controller
The-Beez-Kneez Apr 17, 2018
e6a732d
Accidentally made changes before I merged branches
The-Beez-Kneez Apr 17, 2018
a8dc582
Merge branch 'user_index_tests'
The-Beez-Kneez Apr 17, 2018
9513238
Added in tests for show method in User Controller
The-Beez-Kneez Apr 17, 2018
c697371
Made upvote tests more robust, added another to check that a logged i…
The-Beez-Kneez Apr 18, 2018
b558f51
Created login tests to test user login
The-Beez-Kneez Apr 18, 2018
f82a15f
Added in tests for logout.
The-Beez-Kneez Apr 18, 2018
99ea2f2
Added in OmniAuth Github login, updated session controller to reflect…
The-Beez-Kneez May 1, 2018
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@

# Ignore Byebug command history file.
.byebug_history

# Ignore .env files, so that it is never put on Git
.env
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ gem 'jbuilder', '~> 2.5'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem "omniauth"
gem "omniauth-github"

# Use the Foundation CSS framework
gem 'foundation-rails'
gem 'autoprefixer-rails'
Expand Down Expand Up @@ -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'
# Use for .env file
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 @@ -63,16 +63,23 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.3)
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)
hashie (3.5.7)
i18n (1.0.0)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
Expand All @@ -82,6 +89,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,9 +116,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.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)
Expand Down Expand Up @@ -199,6 +224,7 @@ DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.2)
dotenv-rails
foundation-rails
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -207,6 +233,8 @@ DEPENDENCIES
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
Expand Down
57 changes: 36 additions & 21 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@ 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 index
@user = User.find(session[:user_id]) # < recalls the value set in a previous request
end

def new
@user = User.new
end

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

if auth_hash['uid']
@user = User.find_by(uid: auth_hash[:uid], provider: 'github')
if @user.nil?
# User doesn't match anything in the DB
# Attempt to create a new user
@user = User.new( name: auth_hash['info']['name'], email: auth_hash['info']['email'], uid: auth_hash['uid'], provider: auth_hash['provider'])
if @user.save
# saved successfully
session[:user_id] = @user.id
flash[:success] = "Logged in successfully"
redirect_to root_path
else
# not saved successfully
flash[:error] = "Could not log in"
redirect_to root_path
end
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
session[:user_id] = @user.id
flash[:success] = "Logged in successfully"
redirect_to root_path
end
else
flash[:error] = "Could not log in"
redirect_to root_path
end
redirect_to root_path
end

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

redirect_to root_path
end
end
3 changes: 2 additions & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pry'
class WorksController < ApplicationController
# We should always be able to tell what category
# of work we're dealing with
Expand Down Expand Up @@ -50,7 +51,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 Down
4 changes: 0 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
class User < ApplicationRecord
has_many :votes
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true
end
2 changes: 1 addition & 1 deletion app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.to_category_hash
end

def self.by_category(category)
category = category.singularize.downcase
category = category.to_s.singularize.downcase
self.where(category: category).order(vote_count: :desc)
end

Expand Down
29 changes: 14 additions & 15 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@
<%= 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" %>
<% if session[:user_id] %>
<%= link_to "Logout", logout_path, method: :delete, class: "hollow button" %>
<% else %>
<%= link_to "Log In", login_path, class: "button" %>
<%= link_to "Login", "/auth/github", class: "hollow button" %>
<% end %>
</div>
</nav>
</header>

<% if flash[:result_text] or flash[:messages] %>
<section class="status <%= flash[:status] %>">
<h3><%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %></h3>
<% if flash[:messages] %>
<ul>
<% flash[:messages].each do |name, problems| %>
<% problems.each do |problem| %>
<li><%= name %>: <%= problem %></li>
<% end %>
<% end %>
</ul>
<section class="status <%= flash[:status] %>">
<h3><%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %></h3>
<% if flash[:messages] %>
<ul>
<% flash[:messages].each do |name, problems| %>
<% problems.each do |problem| %>
<li><%= name %>: <%= problem %></li>
<% end %>
</section>
<% end %>
</ul>
<% end %>
</section>
<% end %>

<main>
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
11 changes: 8 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +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#new', as: 'login'
get "/auth/:provider/callback", to: "sessions#create"
# post '/login', to: 'sessions#login'
# post '/logout', to: 'sessions#logout', as: 'logout'
delete "/logout", to: "sessions#destroy", as: "logout"


resources :works

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

resources :users, only: [:index, :show]
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20180501072044_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.integer :uid, null: false # this is the identifier provided by GitHub

Choose a reason for hiding this comment

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

This project already has a table users, so creating a new table probably isn't what you want here. Instead you might edit the existing table using add_column.

Looks like Rails takes the last one if there are multiple migrations with the same name, so this may have worked accidentally.

t.string :provider, null: false # this tells us who provided the identifier

t.timestamps
end
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170407164321) do
ActiveRecord::Schema.define(version: 20180501072044) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
t.string "username"
t.string "name"
t.string "email"
t.integer "uid", null: false
t.string "provider", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand All @@ -41,6 +44,5 @@
t.integer "publication_year"
end

add_foreign_key "votes", "users"
add_foreign_key "votes", "works"
end
Loading