Skip to content

Instantly share code, notes, and snippets.

@kinopyo
kinopyo / gist:2957917
Created June 20, 2012 03:03
Chanko active_if condition..
# original
active_if any(:staff_beta?, :qa_ext_target?)
# In addition to above, I want to active extension when controller is `professional_users`
active_if do |context, options|
user = context.instance_variable_get("@login_user")
next unless user
next true if user.staff?
next true if user.release_target?(ReleaseTarget::QA_EXT_RELEASE_GROUP)
@kinopyo
kinopyo / gist:2687749
Created May 13, 2012 10:53
Rails local format validation
validates_format_of :locale, with: /^[a-z]{2}(?:-[A-Z]{2})?$/, allow_blank: true
# valid: 'ja-JP', 'en'
@kinopyo
kinopyo / gist:2426829
Created April 20, 2012 07:23
Ruby: Get module name or class name without module
n = Notification::Answer.new
n.class.name.split('::').last || ''
# => "Answer"
n.class.name.split('::').first || ''
# => "Notification"
@kinopyo
kinopyo / gist:2426570
Created April 20, 2012 06:27
Rails ActiveRecord, includes can specify multiple models
Topic.includes(:tag,:user).paginate(:page => params[:page], :per_page => 15)
config.autoload_paths += %W( #{ config.root }/lib/middleware )
@kinopyo
kinopyo / user.rb
Created April 20, 2012 02:10
Rails validates nickname length, uniqueness and format. using format: { without: /../ } syntax.
validates :nickname, presence: true, length: 3..10, uniqueness: true, format: { without: /[!-\/\@\^\~\`\(\)\[\]\>\<\=]/ }
@kinopyo
kinopyo / redirect_referrer_or_default.rb
Created April 10, 2012 14:47
rails redirect referrer, redirect back with default
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
def redirect_referrer_or_default(default)
redirect_to(request.referrer || default)
end
@kinopyo
kinopyo / gist:2350570
Created April 10, 2012 11:16
.gitignore => ignoring temporary files generated by your text editor # or operating system
git config --global core.excludesfile ~/.gitignore_global
@kinopyo
kinopyo / gist:2343176
Created April 9, 2012 12:37
Override Devise RegistrationsController, override redirect path after updating user

Override Devise RegistrationsController

Overview

Here I'll show you

  • How to override devise registrations_controller(related to create/update user account)
  • How to change redirect path after updating user

Override RegistrationsController

@kinopyo
kinopyo / database.yml
Created March 29, 2012 03:10
Rails: Turn off rspec postgres warnings "NOTICE: CREATE TABLE will create implicit sequence ..."
# add min_messages to test env
test:
adapter: postgresql
encoding: unicode
database: homu_test
pool: 5
username: postgres
password:
min_messages: warning