Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Cuong Lam thanhcuong1990

🏠
Working from home
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 (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 / 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 / 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 / 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"
@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
View topic(3).rb
# /app/models/topic.rb
class Topic < ActiveRecord::Base
scope :trending, lambda { |num| where('started_trending > ?', 1.day.ago).
order('mentions desc').
limit(num) }
# ...
end
View topic(1).rb
# /app/models/topic.rb
class Topic < ActiveRecord::Base
scope :trending, where('started_trending > ?', 1.day.ago).
order('mentions desc')
end
View posts_controller(10).rb
current_user.posts.create(
:title => "This is the title",
:content => "This is the content of post"
)
You can’t perform that action at this time.