Skip to content

Instantly share code, notes, and snippets.

View thanhcuong1990's full-sized avatar
🏠
Working from home

Cuong Lam thanhcuong1990

🏠
Working from home
View GitHub Profile
@thanhcuong1990
thanhcuong1990 / posts_controller(3).rb
Last active August 29, 2015 14:04
Scope examples
# /app/controllers/posts_controller.rb
def index
@posts = current_user.posts.
order('created_at desc').
limit(10)
#...
end
@thanhcuong1990
thanhcuong1990 / posts_controller(2).rb
Last active January 11, 2016 19:07
Scope examples
# /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)
# ...
# /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],
@thanhcuong1990
thanhcuong1990 / post.rb
Last active August 29, 2015 14:04
Fat model & Skinny controller examples
# /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"
@thanhcuong1990
thanhcuong1990 / posts_controller(2).rb
Last active August 29, 2015 14:04
Fat model & Skinny controller examples
# /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 / posts_controller(1).rb
Last active August 29, 2015 14:04
Fat model & Skinny controller examples
# /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 / Fat model & Skinny controller (3).rb
Created July 20, 2014 09:43
Fat model & Skinny controller examples
# 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 / Fat model & Skinny controller (2).rb
Last active August 29, 2015 14:04
Fat model & Skinny controller examples
# posts_controller.rb
def index
@published_posts = Post.all_published
@unpublished_posts = Post.all_unpublished
end
@thanhcuong1990
thanhcuong1990 / Fat model & Skinny controller (1).rb
Last active August 29, 2015 14:04
Fat model & Skinny controller example
# 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 / Nginx Management Script
Created May 12, 2014 09:30
Control start, stop, restart nginx passenger with init.d
#! /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