-
Notifications
You must be signed in to change notification settings - Fork 1
Don't merge - Review #39
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
base: review-base
Are you sure you want to change the base?
Changes from all commits
a33b890
b45b7d2
d294cde
d95ae60
47a02dc
ba1164a
feeb1f8
d3d5e48
06bab42
a5d7a89
1152909
f20d882
56866ea
5d38e0a
5c269c6
f1ad791
5a08341
74e4a07
28d3e63
2bfb61a
600e6fd
5d66116
0c73266
39211db
78522e3
6d0bba3
b6fe8ca
b537b00
82dabb2
1b8649b
2ac1bff
0b00192
6bb3b1d
23b42c4
60fa66f
a2fb405
015451c
cfbbd65
6136a7b
e9262f6
d7b4974
11a16b6
44f15dd
bb31b04
1ccfb90
3c32073
5fdab53
ed223c7
920754b
88af863
9bb84db
16cfd52
b102add
e71cdfa
8cfb718
415dde9
41660e9
cce89c7
9631759
a326baa
6078a1f
09d4af0
cb95a26
ddce454
1a928e4
d3f9b8d
c8c01d5
5cea91f
84635c9
bc905f0
7fc3071
411071d
50b5640
b96e66e
433a350
904da2e
b773e93
ada8a90
d76e576
88ea29a
3e87a0b
4869b32
ea810d1
2a3d16e
b49f3c2
6ff2480
46e1114
8d2ac66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| exit | ||
| Question.questions_sorted_by_most_votes | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| bin/rspec | ||
| coverage/ | ||
| log/test.log |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| --color | ||
| --require spec_helper | ||
| --format documentation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
|
|
||
| body { | ||
| background-color: #eeeeee; | ||
| font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; | ||
| font-weight: 500; | ||
| font-size: 14px; | ||
| color: #1e1e1e; | ||
| } | ||
|
|
||
| h1 { | ||
| font-family: Georgia, serif; | ||
| font-style: italic; | ||
| color: #BA55D3; | ||
| } | ||
|
|
||
| h2 { | ||
| font-family: Georgia, serif; | ||
| font-style: italic; | ||
| color: #BA55D3; | ||
| } | ||
|
|
||
| nav { | ||
| background-color: #004060; | ||
| color: #ffffff; | ||
| padding: 4px 8px; | ||
| } | ||
|
|
||
| li { | ||
| color: #BA55D3 ; | ||
| } | ||
|
|
||
| p3 { | ||
| font-style: italic; | ||
| color: #008000; | ||
| } | ||
|
|
||
| p2 { | ||
| font-style: italic; | ||
| color: #DC143C; | ||
| } | ||
|
|
||
| .question_header { | ||
| border: dotted black 2px ; | ||
| } | ||
|
|
||
| .answer_header { | ||
| margin-left: 5%; | ||
| font-size: 1.5em; | ||
| } | ||
|
|
||
| .comments { | ||
| margin-left: 5%; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class AnswersController < ApplicationController | ||
| before_action :ensure_logged_in, only:[:create] | ||
| def create | ||
| question = Question.find_by(id: params[:question_id]) | ||
| @answer = current_user.answers.build(answer_params) | ||
| @answer.question = question | ||
| if @answer.save | ||
| redirect_to question_path(question.id) | ||
| else | ||
| flash[:notice] = "Something went wrong." | ||
| redirect_to question_path(question.id) | ||
| end | ||
| end | ||
|
|
||
| def answer_params | ||
| params.require(:answer).permit(:body) | ||
| end | ||
| end | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,17 @@ class ApplicationController < ActionController::Base | |
| # Prevent CSRF attacks by raising an exception. | ||
| # For APIs, you may want to use :null_session instead. | ||
| protect_from_forgery with: :exception | ||
| helper_method :current_user, :logged_in? | ||
|
|
||
| def current_user | ||
| @current_user ||= User.find_by(id: session[:user_id]) | ||
| end | ||
|
|
||
| def logged_in? | ||
| !!session[:user_id] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do !!current_user to ensure that session_id maps to a valid user |
||
| end | ||
|
|
||
| def ensure_logged_in | ||
| redirect_to login_path unless current_user | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| class CommentsController < ApplicationController | ||
| before_action :ensure_logged_in, only:[:create] | ||
| def new | ||
| @comment = Comment.new | ||
| end | ||
|
|
||
| def create | ||
| if (params.has_key?(:answer_id)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If your routes are nested a trick like the following can be helpful to avoid a bunch of if / elseif stuff. |
||
| @answer = Answer.find_by(id: params[:answer_id]) | ||
| comment = current_user.comments.build(comment_params) | ||
| comment.commentable_id = params[:answer_id] | ||
| comment.commentable_type = "Answer" | ||
| params[:question_id] = @answer.question.id | ||
| else | ||
| comment = current_user.comments.build(comment_params) | ||
| comment.commentable_type = "Question" | ||
| comment.commentable_id = params[:question_id] | ||
| end | ||
|
|
||
| if comment.save | ||
| redirect_to question_path(params[:question_id]) #have to pass in question.id as a local in the question show page when the comment is to an answer | ||
| else | ||
| render 'new' | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def comment_params | ||
| params.require(:comment).permit(:body) | ||
| end | ||
|
|
||
| end | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| class QuestionsController < ApplicationController | ||
| before_action :ensure_logged_in, only:[:new, :create, :update] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be tempted to move this to ApplicationController and use skip_before_action in the (few) controllers you don't want it to apply |
||
| def index | ||
| @questions = Question.all | ||
| end | ||
|
|
||
| def trending | ||
| @questions = Question.trending_questions | ||
| render 'index' | ||
| end | ||
|
|
||
| def voted | ||
| @questions = Question.top_voted_questions | ||
| render 'index' | ||
| end | ||
|
|
||
| def new | ||
| @question = Question.new | ||
| end | ||
|
|
||
| def create | ||
| question = current_user.questions.build(question_params) | ||
| if question.save | ||
| redirect_to root_path | ||
| else | ||
| render 'new' | ||
| end | ||
| end | ||
|
|
||
| def show | ||
| @question = Question.find(params[:id]) | ||
| @answers = Answer.top_voted_answer(@question) | ||
| @answer = Answer.new | ||
| @comment = Comment.new | ||
| @vote = Vote.new | ||
| end | ||
|
|
||
| def update | ||
| question = Question.find(params[:id]) | ||
| question.update_attributes(accepted_answer_id: params[:chosen_answer_id]) | ||
| redirect_to question_path(question) | ||
| end | ||
|
|
||
|
|
||
|
|
||
| private | ||
|
|
||
| def question_params | ||
| params.require(:question).permit(:title, :body) | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class SessionsController < ApplicationController | ||
| skip_before_action :ensure_logged_in | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way things are now this is unnecessary |
||
|
|
||
| def new | ||
| end | ||
|
|
||
| def create | ||
| user = User.find_by(username: params[:username]) | ||
| if user && user.authenticate(params[:password]) | ||
| flash[:notice] = 'Welcome to our site!' | ||
| session[:user_id] = user.id | ||
| redirect_to root_path | ||
| else | ||
| flash.now[:warning] = 'Login failed. Please Try Again.' | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| session.clear | ||
| flash[:notice] = 'Thanks For Visiting The Site! Please Return Soon!' | ||
| redirect_to root_path | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class UsersController < ApplicationController | ||
| def new | ||
| @user = User.new | ||
| end | ||
|
|
||
| def create | ||
| @user = User.new(user_params) | ||
| if @user.save | ||
| session[:user_id] = @user.id | ||
| flash[:notice] = 'Congratulations! You Are Now Registered!' | ||
| redirect_to root_path | ||
| else | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def edit | ||
| end | ||
|
|
||
| def destroy | ||
| end | ||
|
|
||
| def user_params | ||
| params.require(:user).permit(:username, :password, :password_confirmation) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to add if session[:user_id] to the end of this to avoid running the query when we know the session[:user_id] is nil.