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

Ampers Abinnet Ainalem #29

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
449e69a
tested an edge case for works root controller action
Abiaina Apr 29, 2018
a713917
changed method for removing existing work instances from controller m…
Abiaina Apr 29, 2018
0e7d77c
controller testing for root nominal and edge cases failed then passin…
Abiaina Apr 29, 2018
f470b9d
controller test for index/works path when no media failed then passing
Abiaina Apr 29, 2018
3208c84
pushing uncommitted changes in works controller test
Abiaina Apr 29, 2018
58680c1
controller test for work new action
Abiaina Apr 29, 2018
7a108a4
work controller test for create action nominal and edge cases fail th…
Abiaina Apr 29, 2018
85e7366
added work controller test nominal and edge for show action
Abiaina Apr 29, 2018
93586ee
update controller tests nominal and edge cases fail then passing
Abiaina Apr 29, 2018
37a73af
destroy action controller tests completed, small change to check for …
Abiaina Apr 29, 2018
c2446ac
test upvote controller method tests failed and now passing. work cont…
Abiaina Apr 30, 2018
688bf2c
added spaces and left comment about setting up sessions/logout enviro…
Abiaina Apr 30, 2018
5876eff
test nominal case for login and login_form, update login path params …
Abiaina Apr 30, 2018
a1f53fa
sessions controller tests failed then passing for login and logout ac…
Abiaina Apr 30, 2018
48c9619
user controller testing complete
Abiaina Apr 30, 2018
bee6d56
set up OAuth, needs to incorporate new routes and controller methods …
Abiaina Apr 30, 2018
7ac7941
after adding OAuth mocks all tests failing, updated fixtures and val…
Abiaina May 1, 2018
f99c45e
fixed controller filter issue and tests updated and passing...except …
Abiaina May 2, 2018
5ef2178
fixed works show controller method, all tests passing
Abiaina May 2, 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
Binary file added .DS_Store
Binary file not shown.
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: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -65,7 +64,11 @@ 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"
51 changes: 38 additions & 13 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.1)
execjs
babel-source (5.8.35)
babel-transpiler (0.7.0)
Expand All @@ -62,26 +62,34 @@ 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)
multi_json (>= 1.2)
jquery-rails (4.3.1)
jquery-rails (4.3.3)
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,17 +116,34 @@ 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)
puma (3.11.4)
rack (2.0.5)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.7)
Expand Down Expand Up @@ -175,14 +200,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.10)
execjs (>= 0.3.0, < 3)
web-console (3.5.1)
web-console (3.6.1)
actionview (>= 5.0)
activemodel (>= 5.0)
bindex (>= 0.4.0)
Expand All @@ -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 All @@ -219,8 +247,5 @@ DEPENDENCIES
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,7 +8,15 @@ def render_404
raise ActionController::RoutingError.new('Not Found')
end

private
def block_guest
if @login_user == nil
flash[:result_text] = "You must be logged in to do that"
redirect_to :root
end
end


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

before_action :block_guest, except: [:create]

#Why do I need this?
def index
@user = User.find(session[:user_id]) # < recalls the value set in a previous request
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 destroy
session[:user_id] = nil
flash[:result_text] = "Successfully logged out!"
redirect_to root_path
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 = User.build_from_github(auth_hash)
flash[:result_text] = "Logged in successfully and created new Media Ranker account"
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[:result_text] = "Logged in successfully"
end
session[:user_id] = user.id
else
flash[:error] = "Could not log in"
end
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
end
2 changes: 2 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 :block_guest

def index
@users = User.all
end
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class WorksController < ApplicationController
# We should always be able to tell what category
# of work we're dealing with

before_action :block_guest, except: [:root]

before_action :category_from_work, except: [:root, :index, :new, :create]

def root
Expand Down Expand Up @@ -34,10 +37,16 @@ def create
end

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

def edit
work = Work.find_by(id: params[:id])
if work.nil?
render :edit, status: :not_found
end
end

def update
Expand Down
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ class User < ApplicationRecord
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true

validates :uid, uniqueness: true, presence: true


def self.build_from_github (auth_hash)
user = User.new
user.username = auth_hash["info"]["nickname"]
user.email = auth_hash["info"]["email"]
user.uid = auth_hash["uid"]
user.provider = "github"
user.save
return user
end
end
7 changes: 3 additions & 4 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
<%= 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 "Log out", logout_path, method: "delete" %>
<% else %>
<%= link_to "Log In", login_path, class: "button" %>
<%= link_to "Login with Github", "/auth/github" %>
<% end %>
</div>
</nav>
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
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get "/auth/:provider/callback", to: "sessions#create", as: "login"
delete "/logout", to: "sessions#destroy", as: "logout"
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'
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20180430041115_add_uidand_provider_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddUidandProviderToUser < ActiveRecord::Migration[5.0]
def change
add_column :users, :uid, :integer, null: false
add_column :users, :provider, :string, null: false
end
end
5 changes: 5 additions & 0 deletions db/migrate/20180430190233_add_name_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNameToUser < ActiveRecord::Migration[5.0]
def change
add_column :users, :name, :string
end
end
6 changes: 6 additions & 0 deletions db/migrate/20180430190738_remove_name_add_email_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveNameAddEmailToUser < ActiveRecord::Migration[5.0]
def change
remove_column :users, :name, :string
add_column :users, :email, :string
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

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

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -19,6 +19,9 @@
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "uid", null: false
t.string "provider", null: false
t.string "email"
end

create_table "votes", force: :cascade do |t|
Expand Down
Binary file added test/.DS_Store
Binary file not shown.
Loading