Skip to content

Instantly share code, notes, and snippets.

@tkawa
tkawa / subscriptions_controller.rb
Created December 27, 2011 03:38
Using subscription_fu
class SubscriptionsController < ApplicationController
before_filter :authenticate_user!
def show
@page_css = 'user'
@subscription = current_user.active_subscription
@upcoming = current_user.upcoming_subscription
@latest = current_user.latest_valid_subscription
end
# skip validates (summary)
class SomeModel < ParentModel # < ActiveRecord::Base
_callback = _validate_callbacks.find {|c|
c.raw_filter.is_a?(ActiveModel::Validations::InclusionValidator) && # class name
c.raw_filter.instance_variable_get(:@attributes).try(:include?, :attribute_name) }
skip_callback(:validate, _callback.kind, _callback.filter)
end
class GroupsController < AppController
resources_path 'users/groups', 'my/groups'
def index
end
def show
end
def update
@tkawa
tkawa / hatebu_mix_twitter_comment.user.js
Created August 28, 2012 10:32
はてブのコメントリストにTwitterコメントを合成表示する userscript
// ==UserScript==
// @name Hatebu Mix Twitter Comment
// @namespace http://www.4bit.net/
// @include http://b.hatena.ne.jp/entry/*
// @description はてブのコメントリストにTwitterコメントを合成表示
// @author tkawa
// @version 1
// ==/UserScript==
(function(d, func) {
@tkawa
tkawa / i18n_exception_handler.rb
Created February 3, 2013 12:36
Railsでlocaleの翻訳が見つからなかったとき、“translation missing”はログに出して、viewにはhumanizeした文字列を出す
# config/initializers/i18n_exception_handler.rb
I18n.exception_handler = ->(exception, locale, key, options) do
if exception.is_a?(I18n::MissingTranslation)
Rails.logger.warn exception.message if Rails.env.development?
key.to_s.split('.').last.humanize
else
raise exception
end
end
@tkawa
tkawa / index.html.haml
Last active December 12, 2015 08:39
http://example.com/posts?date=2013-01-01&q=search_word&page=2&per=20 のように、クエリパラメータでフィルタするような場合に、 strong parameters と併用して、 filter_params に default とか validate とか書けるgemを作成中ですがどうでしょう? https://github.com/tkawa/collection_filter/tree/auto-params
-# クエリパラメータもform_forと同じように書ける
= filter_form do |f|
= f.label :date
= f.text_field :date
= f.label :q
= f.search_field :q
= f.button
@tkawa
tkawa / user.rb
Last active December 16, 2015 17:40
class User < ActiveRecord::Base
def postal_code1
postal_code.split('-', 2).first || ''
end
def postal_code2
postal_code.split('-', 2).second || ''
end
def postal_code1=(value)
splitted = postal_code.split('-', 2)
splitted[0] = value
# config/routes.rb
# question and answers that the user has
# /users/:user_id/questions
# /users/:user_id/answers
resources :users do
resources :questions
resources :answers
end
@tkawa
tkawa / Gemfile
Last active December 19, 2015 03:39 — forked from mislav/example.rb
Fetch full Atom feed from Google Reader
source 'https://rubygems.org'
gem 'curb'
gem 'nokogiri'
gem 'addressable'
@tkawa
tkawa / filter_params.rb
Created July 11, 2013 07:57
https://gist.github.com/tkawa/4745785 の件、FormBuilder(form_forみたいなやつ)がいらなければ、gemなくてもこんなふうに書けばすむというメモ。
class FilterParams < ActionController::Parameters
include ActiveModel::Model
validates :date, format: /\A\d{4}-\d{2}-\d{2}\Z/
validates :q, length: { maximum: 20 }
def initialize(params = {})
params.reverse_merge!(per: '10')
super(params)
end