Skip to content

Instantly share code, notes, and snippets.

View mataki's full-sized avatar

Akihiro Matsumura mataki

  • SonicGarden
  • Kanagawa, Japan
  • X @mat_aki
View GitHub Profile
# Security pacth for Rails 2.3.9
# put this file on ROOT/config/initializers
# http://weblog.rubyonrails.org/2010/10/15/security-vulnerability-in-nested-attributes-code-in-ruby-on-rails-2-3-9-and-3-0-0
# http://weblog.rubyonrails.org/assets/2010/10/15/2-3-nested_attributes.patch
module ActiveRecord
module NestedAttributes #:nodoc:
def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
# Group -has_many-> Participation
# Group.joined ==> defined named_scope
# Group.user_id_not_null ==> genarated named_scope by searchlogic
g = Group.find(1)
# SQL) SELECT * FROM `groups` WHERE (`groups`.`id` = 1)
g.participations
# SQL) SELECT * FROM `participations` WHERE (`participations`.group_id = 1)
require "net/http"
require "uri"
http = Net::HTTP.new("www.youroom.local", "3000")
http.send_request('CONNECT', "/")
@mataki
mataki / heroku_autoscalling.rb
Created December 2, 2010 06:33
Auto scalling dynos on heroku using NewRelic
=begin
Need to install gems heroku, newrelic_rpm
$ gem install heroku newrelic_rpm
Set your apps setting
app_name : heroku's app_name of auto scaling
license_key : NewRelic api key. You can get heroku's NewRelic admin console. "App setting" and "Agent configuration"
execute with cron every minutes
$ ruby ./adjust_dynos_with_newrelic.rb
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
@mataki
mataki / rails2311_csrf_path.rb
Created February 9, 2011 07:11
path for csrf risk on rails 2.3.11
module ActionController
module RequestForgeryProtection
protected
def verify_authenticity_token
verified_request? || handle_unverified_request
end
# Process logout
def handle_unverified_request
reset_session
@mataki
mataki / facebook_test_user.rb
Created June 27, 2011 06:03
FbGraphでテストユーザ作成
# -*- coding: utf-8 -*-
require "rubygems"
require "fb_graph"
app_id = ""
csecret = ""
app = FbGraph::Application.new(app_id, :secret => csecret)
# ユーザ作って友達にするとか
require "rubygems"
require "capybara-webkit"
require "mini_magick"
class WebkitScreenshot
def initialize
@browser = Capybara::Driver::Webkit::Browser.new
end
def browser
@mataki
mataki / factory_girl_massassignment.rb
Created June 5, 2013 05:36
factory girl は mass assignment を回避してデータを作れるか? YES
require "active_record"
require "sqlite3"
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
ActiveRecord::Base.establish_connection('test')
class CreateAllTables < ActiveRecord::Migration
def self.up
create_table :locales do |t|
t.string :name
@mataki
mataki / file0.rb
Last active December 18, 2015 13:48
AWS の S3 のファイルを別の bucket にコピーする方法 ref: http://qiita.com/mat_aki/items/90205144c3f93758e357
require 'right_aws'
aws_access_key_id, aws_secret_access_key = "key_id", "access_key"
old_bucket_name = 'from-bucket'
new_bucket_name = "to-bucket"
s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
bucket = s3.bucket(old_bucket_name)
puts "Copy S3 files from '#{old_bucket_name}' to '#{new_bucket_name}'"