Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
# Ignore bundler config.
/.bundle

# Hide the .env. It's full of secrets!
.env

.DS_Store

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
Expand Down
11 changes: 10 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
source 'https://rubygems.org'


gem "omniauth"
gem "omniauth-github"

gem "awesome_print"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.7'
# Use sqlite3 as the database for Active Record
Expand Down Expand Up @@ -35,6 +39,8 @@ gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'dotenv-rails'
gem 'better_errors'
end

group :development do
Expand All @@ -44,4 +50,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'
end

#
# group :test do
# gem 'minitest-reporters'
# end
38 changes: 37 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
awesome_print (1.7.0)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (9.0.5)
coderay (1.1.1)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.1.x)
Expand All @@ -50,10 +56,17 @@ GEM
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
dotenv (2.1.1)
dotenv-rails (2.1.1)
dotenv (= 2.1.1)
railties (>= 4.0, < 5.1)
erubis (2.7.0)
execjs (2.7.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
globalid (0.3.7)
activesupport (>= 4.1.0)
hashie (3.4.6)
i18n (0.7.0)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
Expand All @@ -63,6 +76,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
jwt (1.5.6)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
Expand All @@ -73,9 +87,26 @@ GEM
mini_portile2 (2.1.0)
minitest (5.9.1)
multi_json (1.12.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
oauth2 (1.2.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.3.1)
hashie (>= 1.2, < 4)
rack (>= 1.0, < 3)
omniauth-github (1.1.2)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.1)
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
pkg-config (1.1.7)
rack (1.6.4)
rack-test (0.6.3)
Expand Down Expand Up @@ -146,10 +177,15 @@ PLATFORMS
ruby

DEPENDENCIES
awesome_print
better_errors
byebug
coffee-rails (~> 4.1.0)
dotenv-rails
jbuilder (~> 2.0)
jquery-rails
omniauth
omniauth-github
rails (= 4.2.7)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand All @@ -160,4 +196,4 @@ DEPENDENCIES
web-console (~> 2.0)

BUNDLED WITH
1.13.1
1.13.2
3 changes: 3 additions & 0 deletions app/assets/javascripts/homepages.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/sessions.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/homepages.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the homepages controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/sessions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
12 changes: 12 additions & 0 deletions app/controllers/homepages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class HomepagesController < ApplicationController

def index
get_current_user
end

private
def get_current_user
@user = User.find_by(id: session[:user_id])
end

end
30 changes: 30 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class SessionsController < ApplicationController

def create
auth_hash = request.env['omniauth.auth']
redirect to login_failure_path unless 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.build_from_github(auth_hash)
render :creation_failure unless @user.save
end

# Save the user ID in the session
session[:user_id] = @user.id

redirect_to index_path
end

def index
@user = User.find(session[:user_id]) # < recalls the value set in a previous request
end

def destroy
session[:user_id] = nil
redirect_to root_path
end

end
14 changes: 8 additions & 6 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class TasksController < ApplicationController
def index
@tasks = Task.all
@tasks = Task.where(user_id: session[:user_id])
@user = User.find(session[:user_id])
end

def new
Expand All @@ -24,8 +25,8 @@ def edit
def update
@mytask = Task.find(params[:id].to_i)
@params = params
@mytask.task_name = params[:task_name]
@mytask.description = params[:description]
@mytask.task_name = params[:task][:task_name]
@mytask.description = params[:task][:description]
@mytask.save

redirect_to action: "index"
Expand All @@ -34,10 +35,11 @@ def update
def create
@params = params
@mytask = Task.new
@mytask.task_name = params[:task_name]
@mytask.description = params[:description]
@mytask.task_name = params[:task][:task_name]
@mytask.description = params[:task][:description]
@mytask.completion_status = "incomplete"
@mytask.completion_date = "Not completed yet!"
@mytask.user_id = session[:user_id]
@mytask.save

redirect_to action: "index"
Expand All @@ -64,7 +66,7 @@ def mark_incomplete

private
def user_params
params.require(:task).permit(:task_name, :description, :completion_status, :completeion_date)
params.require(:task).permit(:task_name, :description, :completion_status, :completion_date)
end

end
2 changes: 2 additions & 0 deletions app/helpers/homepages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HomepagesHelper
end
2 changes: 2 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SessionsHelper
end
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class User < ActiveRecord::Base
validates :email, :uid, :provider, 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']

return user
end
end
9 changes: 9 additions & 0 deletions app/views/homepages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% if @user %>
<h1>Welcome <%= @user.name %>!</h1>
<% else %>
<h1>Please log in</h1>
<% end %>

<% if flash[:notice] %>
<p class="flash-notice"><%= flash[:notice] %></p>
<% end %>
7 changes: 7 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
<body>
<header>
<h1>Task List</h1>
<% if session[:user_id] == nil %>
<p><%= link_to "log in via github", "/auth/github" %></p>
<% else %>
<p><%= link_to "logout", sessions_destroy_path,:method => :delete %></p>
<!--if you do "sessions/destroy" here you will get a routing error in any page other than the homepage-->
<p><%= link_to "View my Tasks", index_path %></p>
<% end %>
</header>

<%= yield %>
Expand Down
1 change: 1 addition & 0 deletions app/views/sessions/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Sessions#create</h1>
10 changes: 10 additions & 0 deletions app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= form_for @mytask, url: action_name do |f| %>

<%= f.label :task_name %>
<%= f.text_field :task_name %>

<%= f.label :description %>
<%= f.text_area :description %>

<%= f.submit %>
<% end %>
6 changes: 4 additions & 2 deletions app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h2>Edit Your Task</h2>

<!--
<form name="edit-task" action="update" method="post">
<input type="hidden" name="_method" value="PUT">

Expand All @@ -12,4 +12,6 @@
<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
<button type="submit">Submit</button>

</form>
</form> -->

<%= render partial: "form", locals: { action_name: "create"} %>
32 changes: 18 additions & 14 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<h1>Welcome <%= @user.name %>!</h1>

<h2>Get to work!</h2>
<% @tasks.each do |task| %>
<article>
<h2><%= link_to( task.task_name, show_path(task.id)) %></h2>
<table>
<tr>
<td><h2><%= link_to( task.task_name, show_path(task.id)) %></h2></td>

<% if task.completion_status == 'incomplete' %>
<p> <%= button_to 'mark as complete', {action: "mark_complete", id: task.id },
method: :mark_complete %> </p>
<% else %>
<p> <%= button_to 'mark as incomplete', {action: "mark_incomplete", id: task.id },
method: :mark_incomplete %> </p>
<% end %>
<% if task.completion_status == 'incomplete' %>
<td> <%= button_to 'mark as complete', {action: "mark_complete", id: task.id },
method: :mark_complete %> </td>
<% else %>
<td> <%= button_to 'mark as incomplete', {action: "mark_incomplete", id: task.id },
method: :mark_incomplete %> </td>
<% end %>

<p> <%= button_to 'edit task', {action: "edit", id: task.id },
method: :edit %> </p>
<td><%= button_to 'edit task', {action: "edit", id: task.id },
method: :edit %> </td>

<p> <%= button_to 'delete task', {action: "delete", id: task.id },
method: :delete, data: {confirm: "Are you sure you want to delete this task?"} %> </p>
</article>
<td> <%= button_to 'delete task', {action: "delete", id: task.id },
method: :delete, data: {confirm: "Are you sure you want to delete this task?"} %> </td>
</tr>
</table>
<% end %>

<article>
Expand Down
8 changes: 5 additions & 3 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<h2>Make a new task!</h2>

<form name="new-task" action="create" method="post">
<!-- <form name="new-task" action="create" method="post">

<label for="task_name">Task:</label>
<input type="text" name="task_name" id="task_name">

<label for="description">Description:</label>
<input type="text" name="description" id="description">

<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
<%# tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
<button type="submit">Submit</button>

</form>
</form> -->

<%= render partial: "form" , locals: { action_name: "create"}%>
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
12 changes: 11 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Rails.application.routes.draw do

root to: 'tasks#index'
root to: 'homepages#index'

get 'tasks/index' => 'tasks#index', as: 'index'

Expand All @@ -20,6 +20,16 @@

post 'tasks/create' => 'tasks#create', as: 'create'

# get 'sessions/create'

get 'sessions/create'

delete 'sessions/destroy'

get "/auth/:provider/callback" => "sessions#create"

get 'homepages/index'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
Loading