Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
jhjguxin / Gemfile
Created July 11, 2012 08:29
Rails 3.2 Use debug Base on ruby 1.9.3p194
# To use debugger
group :development do
gem 'linecache19', '0.5.13'
gem 'ruby-debug-base19', '0.11.26'
gem 'ruby-debug19', :require => 'ruby-debug'
#curl -OL http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
#curl -OL http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
#gem install linecache19-0.5.13.gem -- --with-ruby-include="${MY_RUBY_HOME/rubies/src}"
#gem install ruby-debug-base19-0.11.26 -- --with-ruby-include="${MY_RUBY_HOME/rubies/src}"
@jhjguxin
jhjguxin / ability.rb
Created July 12, 2012 18:54
How BBTangCMS use cancan include (Role Based Authorization, Non RESTful Controllers Authorization, Admin Namespace Action Aliases, Abilities in Database)
class Ability
include CanCan::Ability
def initialize(user, controller_namespace)
alias_action :index, :show, :to => :read
alias_action :new, :to => :create
alias_action :edit, :to => :update
#destroy, update ,create
@user = user || User.new # for guest
@jhjguxin
jhjguxin / Nested-Resources.markdown
Created July 13, 2012 07:19
cancan Nested Resources

nested resources 一般用来搭配关联的 resources, cancan 可以根据父类实例来生成,或者过滤子类从而达到权限控制。 如果 在model 没有直接的 belongs_to or has_many 关联cancan 是不会正常工作的 除非, 提供了 parent.children parent.children.built 这样的方法给 cancan 调用, 一个解决方案是, 在父类中创建 一个 children 方法返回 一个 nest 的 resources 的 list。 但是在 bbtangcms 中 我使用的是单表继承 因此 会有一些 问题。

如果使用不到 对父类的认证 那么 完全可以不去使用 through 选项,

class Tag::TimelinesController < Tag::TagBaseController
  load_and_authorize_resource
end
@jhjguxin
jhjguxin / application.rb
Created July 19, 2012 09:19
a glob area page number recorder on rails, so that user can back to recent vist page
#depend same pagination libraries, eg 'will_paginate'
class ApplicationController < ActionController::Base
protect_from_forgery
#before_filter :authenticate_user!
#before_filter :add_initial_breadcrumbs
#before_filter :create_profile
before_filter :set_page_number
#rewrite default_url_options to add 'page' to every url link
def default_url_options(options = {})
@jhjguxin
jhjguxin / _children.html.erb
Created July 19, 2012 09:28
bootstrap-modal with ajax base on BBTangCMS
<ul>
<% item.children.each do |child| %>
<li id='category_<%=child.id %>'>
<!--%- item_list = @model_class.tagged_with([child.name]).paginate(:page => params[:page], :per_page => 10).order('id DESC')%-->
<%- item_list = @model_class.tagged_with([child.name]).order('id DESC')%>
<span><%= child.name %>(<%= item_list.count%>)</span>
<% if item_list.present? %>
<!--%= link_to "More info",archive_item_list_path(:model => @model_class, :item_id => child.id), :remote => true, :class => "btn btn-mini", "data-toggle" => "modal", :id => ("archive_item_list_"+child.id.to_s)%-->
<!--%= link_to "More info", archive_item_list_path(:model => @model_class, :item_id => child.id), :remote => true, :class => "btn btn-mini", "data-target" => "##{child.id}", "data-toggle" => "modal", :id => ("archive_item_list_"+child.id.to_s)%-->
<%= link_to "More info", archive_item_list_path(:model => @model_class, :item_id => child.id), :remote => true, :class => "btn btn-mini", "data-tog
@jhjguxin
jhjguxin / rails-install-ubuntu.sh
Created July 19, 2012 14:15 — forked from paav-o/rails-install-ubuntu.sh
rails-install-ubuntu
echo "Updates packages. Asks for your password."
sudo apt-get update -y
echo "Installs packages. Give your password when asked."
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev -y
echo "Installs ImageMagick for image processing"
sudo apt-get install imagemagick --fix-missing -y
echo "Installs RVM (Ruby Version Manager) for handling Ruby installation"
@jhjguxin
jhjguxin / gist:3144247
Created July 19, 2012 14:17 — forked from jrochkind/gist:2161449
A Capistrano Rails Guide

A Capistrano Rails Guide

by Jonathan Rochkind, http://bibwild.wordpress.com

why cap?

Capistrano automates pushing out a new version of your application to a deployment location.

I've been writing and deploying Rails apps for a while, but I avoided using Capistrano until recently. I've got a pretty simple one-host deployment, and even though everyone said Capistrano was great, every time I tried to get started I just got snowed under not being able to figure out exactly what I wanted to do, and figured I wasn't having that much trouble doing it "manually".

@jhjguxin
jhjguxin / Gemfile
Created July 20, 2012 10:26
Rails 3, prototype and multi-select dependencies base on BBTangCMS
gem "prototype-rails", "~> 3.2.1"
@jhjguxin
jhjguxin / _categories.html.erb
Created July 23, 2012 06:58
Rails 3, jQuery and multi-select dependencies base on BBTangCMS
<%= collection_select('question', "category_list", categories, :id, :name,
{:prompt => "Select a category", :selected => (@question_categories.map(&:id) if @question_categories.present?)}) %>
@jhjguxin
jhjguxin / application.html.erb
Created July 23, 2012 08:38
LESS & automatic CSS cacheing
<script src="http://lesscss.googlecode.com/files/less-1.0.18.min.js"></script>
<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application", :media => "all", :id => "application_css"%>