Skip to content

Commit

Permalink
application
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair Anderson committed Feb 18, 2015
1 parent f4598ae commit bdd77e9
Show file tree
Hide file tree
Showing 62 changed files with 1,009 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ group :development, :test do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'

gem 'swagger-docs'

gem 'factory_girl_rails'
gem 'faker'
gem 'rspec-rails', '~> 3.0' # https://github.com/rspec/rspec-rails
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
swagger-docs (0.1.9)
activesupport (>= 3, < 5)
rails (>= 3, < 5)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
Expand Down Expand Up @@ -208,6 +211,7 @@ DEPENDENCIES
simple_form
sorcery (= 0.9.0)
spring
swagger-docs
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
16 changes: 16 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
2 changes: 2 additions & 0 deletions app/assets/javascripts/item_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/user_sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
15 changes: 15 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require_self
*/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/item_comments.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the item_comments 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/items.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the items controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
69 changes: 69 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;
&:visited {
color: #666;
}
&:hover {
color: #fff;
background-color: #000;
}
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
ul li {
font-size: 12px;
list-style: square;
}
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/user_sessions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the UserSessions 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/users.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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

private
def not_authenticated
redirect_to login_path, alert: "Please login first"
end
end
33 changes: 33 additions & 0 deletions app/controllers/item_comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class ItemCommentsController < ApplicationController
before_action :set_item

def index
@comments = @item.comments.order(created_at: :asc)
end

def create
@comment = current_user.item_comments.build(comment_params)
if @comment.save
redirect_back_or_to item_path(@item), notice: "Success."
else
redirect_back_or_to item_path(@item), notice: @comment.errors.full_messages.join(". ")
end
end

def update
end

def destroy
end

private

def set_item
@item = Item.find(params[:item_id])
return redirect_back_or_to root_path, notice: 'could not find item' unless @item
end

def comment_params
params.require(:item_comment).permit(:content).merge({item_id: params[:item_id]})
end
end
73 changes: 73 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class ItemsController < ApplicationController
before_action :require_login, only: [:new, :create, :edit, :update, :toggle]
before_action :set_item, only: [:show]
before_action :set_user_item, only: [:edit, :update, :toggle]

swagger_controller :items, "Item Management"

swagger_api :index do
summary "Fetches all Current items"
notes "This lists items"
# param :query, :page, :integer, :optional, "Page number"
# param :path, :nested_id, :integer, :optional, "Team Id"
# response :unauthorized
# response :not_acceptable, "The request you made is not acceptable"
# response :requested_range_not_satisfiable
end
def index
@items = Item.order(score: :desc).includes(:user)
end

def show
@comments = @item.comments.includes(:user).order(created_at: :asc)
end

# GET /items/new
def new
@item = Item.new
end

def edit
end

def create
@item = current_user.items.build(item_params)

if @item.save
redirect_to @item, notice: 'Item was successfully created.'
else
render :new
end
end

def update
if @item.update(item_params)
redirect_to @item, notice: 'Item was successfully updated.'
else
render :edit
end
end

def toggle
@item.update(:disabled, @item.disabled?)
message = item.disabled? ? 'disabled' : 'enabled'
redirect_to @item, notice: "Item #{message}."
end

private
def set_item
@item = Item.find(params[:id])
end

def set_user_item
@item = current_user.items.find(params[:id])
return redirect_to :back, notice: 'Unauthorized' unless @item
end

def item_params
params.require(:item).permit(
:title,
:url
)
end
end
39 changes: 39 additions & 0 deletions app/controllers/user_item_votes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class UserItemVotesController < ApplicationController
before_action :set_item

def create
vote = current_user.votes.build(vote_params)
byebug
if vote.save
redirect_to :back, notice: "Upvoted."
else
redirect_to :back, notice: "Already Upvoted."
end
end

def destroy
vote = current_user.votes.where(vote_params).first
if vote
vote.destroy
message = 'Removed Vote.'
else
message = 'No vote to remove.'
end
redirect_to :back, notice: message
end

private
def set_item
@item = Item.find(params[:id])
unless @item
return redirect_to :back, notice: "Could not find item with #{params[:id]}"
end
end

def vote_params
{
votable_id: @item.id,
votable_type: @item.class.to_s
}
end
end
25 changes: 25 additions & 0 deletions app/controllers/user_sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class UserSessionsController < ApplicationController
before_action :require_login, only: [:destroy]
def new
@user = User.new
end

def create
if @user = login(login_params[:username], login_params[:password])
redirect_back_or_to(:users, notice: 'Login successful')
else
flash.now[:alert] = 'Login failed'
render action: 'new'
end
end

def destroy
logout
redirect_to(:users, notice: 'Logged out!')
end

private
def login_params
params.require(:login).permit(:username, :password)
end
end
Loading

0 comments on commit bdd77e9

Please sign in to comment.