Skip to content
Open
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
6 changes: 6 additions & 0 deletions kaku/sample_app/!!
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</li>
4 changes: 4 additions & 0 deletions kaku/sample_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ ruby '2.0.0'
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.7'
gem 'rspec-rails', '2.13.1'
Expand All @@ -13,6 +16,7 @@ group :development, :test do
end

group :test do
gem 'webrat'
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'guard-rspec','2.5.0'
Expand Down
13 changes: 13 additions & 0 deletions kaku/sample_app/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ GEM
bcrypt-ruby (3.0.1)
bootstrap-sass (2.3.2.0)
sass (~> 3.2)
bootstrap-will_paginate (0.0.9)
will_paginate
builder (3.1.4)
capybara (2.1.0)
mime-types (>= 1.16)
Expand All @@ -63,6 +65,8 @@ GEM
factory_girl_rails (4.2.1)
factory_girl (~> 4.2.0)
railties (>= 3.0.0)
faker (1.1.2)
i18n (~> 0.5)
ffi (1.9.0)
formatador (0.2.4)
guard (1.8.1)
Expand Down Expand Up @@ -186,6 +190,11 @@ GEM
uglifier (2.1.1)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
webrat (0.7.3)
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
will_paginate (3.0.4)
xpath (2.0.0)
nokogiri (~> 1.3)

Expand All @@ -195,10 +204,12 @@ PLATFORMS
DEPENDENCIES
bcrypt-ruby (= 3.0.1)
bootstrap-sass (= 2.3.2.0)
bootstrap-will_paginate (= 0.0.9)
capybara (= 2.1.0)
childprocess (= 0.3.6)
coffee-rails (= 4.0.0)
factory_girl_rails (= 4.2.1)
faker (= 1.1.2)
guard-rspec (= 2.5.0)
guard-spork (= 1.5.0)
jbuilder (= 1.0.2)
Expand All @@ -213,3 +224,5 @@ DEPENDENCIES
sqlite3 (= 1.3.7)
turbolinks (= 1.1.1)
uglifier (= 2.1.1)
webrat
will_paginate (= 3.0.4)
1 change: 1 addition & 0 deletions kaku/sample_app/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
3 changes: 3 additions & 0 deletions kaku/sample_app/app/assets/javascripts/sessions.js.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/
56 changes: 55 additions & 1 deletion kaku/sample_app/app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,58 @@ input, textarea, select, .uneditable-input {

input {
height: auto !important;
}
}

#error_explanation {
color: #f00;
ul {
list-style: none;
margin: 0 0 18px 0;
}
}

.field_with_errors {
@extend .control-group;
@extend .error;
}

.users {
list-style: none;
margin: 0;
li {
overflow: auto;
padding: 10px 0;
border-top: 1px solid $grayLighter;
&:last-child {
border-bottom: 1px solid $grayLighter;
}
}
}

/* microposts */

.microposts {
list-style: none;
margin: 10px 0 0 0;

li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;
}
}
.content {
display: block;
}
.timestamp {
color: $grayLight;
}
.gravatar {
float: left;
margin-right: 10px;
}
aside {
textarea {
height: 100px;
margin-bottom: 5px;
}
}
3 changes: 3 additions & 0 deletions kaku/sample_app/app/assets/stylesheets/sessions.css.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/
Binary file not shown.
1 change: 1 addition & 0 deletions kaku/sample_app/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ 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
include SessionsHelper
end
31 changes: 31 additions & 0 deletions kaku/sample_app/app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class MicropostsController < ApplicationController
before_action :signed_in_user
before_action :correct_user, only: :destroy

def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end

def destroy
@micropost.destroy
redirect_to root_url
end

private

def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end

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

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end

def destroy
sign_out
redirect_to root_path
end
end
4 changes: 4 additions & 0 deletions kaku/sample_app/app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end

def help
Expand Down
70 changes: 61 additions & 9 deletions kaku/sample_app/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,75 @@
class UsersController < ApplicationController
before_action :currect_user,only: [:edit, :update]
before_action :signed_in_user,only: [:index, :edit, :update]
before_action :admin_user, only: :destroy

def new
@user = User.new
end

def show
@user = User.find(params[:id])
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end

def create
@user = User.new(params[:user]) # Not the final implementation!
@user = User.new(user_params) # Not the final implementation!
if @user.save
# Handle a successful save.
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end


def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end

def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end

def edit
@user = User.find(params[:id])
end

def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'

end
end

def index
@users = User.paginate(page: params[:page])
end

private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?

end

def currect_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end

def admin_user
redirect_to(root_path) unless current_user.admin?
end
end
61 changes: 61 additions & 0 deletions kaku/sample_app/app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end

def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end

def signed_in?
!current_user.nil?
end

def current_user=(user)
@current_user = user
end

def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end

def current_user?(user)
user == current_user
end

def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end

def redirect_back_or(default)
redirect_to(seeesion[:return_to] || default)
session.delete(:return_to)
end

def store_location
session[:return_to] = request.fullpath
end

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(session_params)
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end

def session_params
params.require(:session).permit(:email, :password)
end
end
6 changes: 6 additions & 0 deletions kaku/sample_app/app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Micropost < ActiveRecord::Base
belongs_to :user
default_scope -> {order('created_at DESC')}
validates :content, presence: true,length: { maximum: 140 }
validates :user_id, presence: true
end
Loading