Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Cuong Lam thanhcuong1990

🏠
Working from home
Block or report user

Report or block thanhcuong1990

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@thanhcuong1990
thanhcuong1990 / Nginx Management Script
Created May 12, 2014
Control start, stop, restart nginx passenger with init.d
View Nginx Management Script
#! /bin/sh
### BEGIN INIT INFO
#
# Source https://raw.github.com/jnstq/rails-nginx-passenger-ubuntu/master/nginx/nginx
#
# Provides: nginx
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
@thanhcuong1990
thanhcuong1990 / Fat model & Skinny controller (1).rb
Last active Aug 29, 2015
Fat model & Skinny controller example
View Fat model & Skinny controller (1).rb
# posts_controller.rb
def index
@published_posts = Post.all :conditions => {['published_at <= ?', Time.now]}
@unpublished_posts = Post.all :conditions => {['published_at IS NULL OR published_at > ?', Time.now]}
end
@thanhcuong1990
thanhcuong1990 / Fat model & Skinny controller (2).rb
Last active Aug 29, 2015
Fat model & Skinny controller examples
View Fat model & Skinny controller (2).rb
# posts_controller.rb
def index
@published_posts = Post.all_published
@unpublished_posts = Post.all_unpublished
end
@thanhcuong1990
thanhcuong1990 / Fat model & Skinny controller (3).rb
Created Jul 20, 2014
Fat model & Skinny controller examples
View Fat model & Skinny controller (3).rb
# Post.rb
def self.all_published
all :conditions => {['published_at <= ?', Time.now]}
end
def self.all_unpublished
all :conditions => {['published_at IS NULL OR published_at > ?', Time.now]}
end
@thanhcuong1990
thanhcuong1990 / posts_controller(1).rb
Last active Aug 29, 2015
Fat model & Skinny controller examples
View posts_controller(1).rb
# /app/controllers/posts_controller.rb
class PostController < ApplicationController
def repost
post = Post.find(params[:id])
if post.user == current_user
flash[:notice] = "Sorry, you can't repost your own posts"
elsif post.reposts.where(:user_id => current_user.id).present?
flash[:notice] = "You already reposted!"
else
# ...
@thanhcuong1990
thanhcuong1990 / posts_controller(2).rb
Last active Aug 29, 2015
Fat model & Skinny controller examples
View posts_controller(2).rb
# /app/controllers/posts_controller.rb
class PostsController < ApplicationController
def repost
post = Post.find(params[:id])
flash[:notice] = post.repost_by(current_user)
redirect_to post
end
end
@thanhcuong1990
thanhcuong1990 / post.rb
Last active Aug 29, 2015
Fat model & Skinny controller examples
View post.rb
# /app/models/post.rb
class Post< ActiveRecord::Base
def repost_by(repost)
if self.user == retweeter
"Sorry, you can't retpost your own posts"
elsif
self.retposts.where(:user_id => repost.id).present? "You already reposted!"
else
# ...
"Succesfully reposted"
View posts_controller(1).rb
# /app/controllers/posts_controller.rb
def index
@posts = Post.find(
:all,
:conditions => {:user_id => current_user.id}, :order => 'created_at desc',
:limit => 10
)
@trending = Topic.find(
:all,
:conditions => ["started_trending > ?", 1.day.ago],
View posts_controller(2).rb
# /app/controllers/posts_controller.rb
def index
@posts = Post.where(:user_id => current_user.id).
order('created_at desc').
limit(10)
@trending = Topic.where('started_trending > ?', 1.day.ago).
order('mentions desc').
limit(7)
# ...
View posts_controller(3).rb
# /app/controllers/posts_controller.rb
def index
@posts = current_user.posts.
order('created_at desc').
limit(10)
#...
end
You can’t perform that action at this time.