This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# こんなテーブルがあるとします | |
create_table :users do |t| | |
t.string :name | |
t.string :email | |
end | |
create_table :belongings do |t| | |
t.integer :user_id | |
t.string :company_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails3 | |
class Course < ActiveRecord::Base | |
has_many :students, order: :name, include: [:parents, :brothers], dependent: :destroy | |
end | |
# Rails4 | |
class Course < ActiveRecord::Base | |
has_many :students, ->{ order(:name).includes(:parents, :brothers) }, dependent: :destroy | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AssocOpts | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def assoc_opts(options) | |
return [options] if Gem::Version.new(Rails.version) < Gem::Version.new('4.0.0') | |
order_option = options.delete(:order) | |
include_option = options.delete(:include) | |
if order_option && include_option |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/gem_tasks' | |
require 'rake/testtask' | |
Rake::TestTask.new do |t| | |
t.libs << 'test' | |
t.test_files = FileList['test/my_gem/*_test.rb'] | |
t.verbose = true | |
end | |
task default: :test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
attr_reader :x, :y | |
def initialize(x, y) | |
@x = x | |
@y = y | |
end | |
def ==(other) | |
@x == other.x && @y == other.y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user = User.first | |
# association で定義された属性の名前を取得 | |
user.reflections.keys | |
# belongs_to で定義された属性の名前を取得 | |
user.reflections.select { |_, ref| ref.macro == :belongs_to }.keys | |
# through 経由で定義された属性の名前を取得 | |
user.reflections.select { |_, ref| ref.is_a?(ActiveRecord::Reflection::ThroughReflection) }.keys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zip' | |
class ZipForWin | |
def zip_files(directory) | |
Zip::File.open("#{directory}.zip", Zip::File::CREATE) do |zipfile| | |
# 再帰的にサブディレクトリも格納する | |
Dir[File.join(directory, '**', '**')].each do |entry| | |
# 相対パスで格納 | |
entry_path = entry.sub(directory, '') | |
# windows で解凍できるように Shift_JIS へ変換 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyPlugin::Model | |
extend ActiveSupport::Concern | |
included do | |
__send__(:include, ActiveModel::Conversion) | |
__send__(:include, ActiveModel::Validations) | |
extend ActiveModel::Naming | |
extend ActiveModel::Translation | |
class << self | |
alias_method_chain :i18n_scope, :my_plugin_model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ja: | |
my_plugin_model: | |
errors: | |
messages: | |
inclusion: "は一覧にありません。" | |
exclusion: "は予約されています。" | |
invalid: "は不正な値です。" | |
confirmation: "が一致しません。" | |
accepted: "を受諾してください。" | |
empty: "を入力してください。" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ja: | |
activemodel: | |
errors: | |
messages: | |
inclusion: "は一覧にありません。" | |
exclusion: "は予約されています。" | |
invalid: "は不正な値です。" | |
confirmation: "が一致しません。" | |
accepted: "を受諾してください。" | |
empty: "を入力してください。" |