Skip to content

Instantly share code, notes, and snippets.

Avatar

Atsuko Mori amyroi

View GitHub Profile
@amyroi
amyroi / active_model_serializer_for_activemodel.rb
Created Jul 20, 2018
ActiveModel::Serializer for ActiveModel::Model
View active_model_serializer_for_activemodel.rb
# ActiveModel::SerializerをActiveModel::Modelで使う
class Model < ApplicationRecord
end
# Modelの操作をするクラス
class Dummy
include ActiveModel::Serialization
include ActiveModel::Model
attr_accessor :model
View allow_helper_double.rb
# Helper spec内でrequestのmockをする
let(:request) { instance_double("request", methods) }
let(:devices) { %w(android iphone pc) }
let(:methods) {
{}.tap do |t|
devices.map do |device|
t.merge!({ "from_#{device}?": false })
end
end
}
@amyroi
amyroi / search_dependency_rspec.rb
Created Aug 29, 2017
rspec config.radom 依存を探す
View search_dependency_rspec.rb
# first CIで落ちたseedを指定
$ bin/spring rspec spec -fd --order rand:40433
# second 対象のspec前のspecでデータ周り、前提条件で依存している為ディレクトリずつ実行してみる。
$ bin/spring rspec spec/models/ spec/features/failer_spec.rb -fd --order rand:40433 --fail-fast
# third ディレクトリを変えて絞っていく
$ bin/spring rspec spec/lib/ spec/features/failer_spec.rb -fd --order rand:40433 --fail-fast
# loop
View enum_i18n.rb
module EnumI18n
extend ActiveSupport::Concern
included do
after_initialize :set_enum_text
end
def set_enum_text
name = (self.class == ::User) ? 'user' : model_name&.element
defined_enums.each do |enum|
enum_column = enum.first
@amyroi
amyroi / regrex_array_spec.rb
Created Jul 19, 2017
Match an array of strings by regex Ask
View regrex_array_spec.rb
let(:methods) { %w(content array sub_class) }
expect(methods).not_to include(match /_text/)
@amyroi
amyroi / Guardfile_target_submodule.rb
Created May 19, 2017
Guardfile target submodule
View Guardfile_target_submodule.rb
# Sample Guardfile use options chdir
paths = %w(spec submodule/spec)
dirs = %w(./ submodule/)
dirs.each do |dir|
guard :rspec, cmd: 'bin/spring rspec', chdir: dir do
require 'guard/rspec/dsl'
dsl = Guard::RSpec::Dsl.new(self)
# RSpec files
@amyroi
amyroi / instance_method_hook.rb
Created Mar 16, 2016
インスタンスメソッドへHookの設定
View instance_method_hook.rb
module Hook
def increment
super
expire
end
end
class Ranking
prepend Hook
@amyroi
amyroi / partitioning.rb
Created Dec 7, 2015
Railsでパーティショニング
View partitioning.rb
# Gem
gem 'composite_primary_keys'
# 複合keyの設定の為、idのprimary_keyを解除して、複合keyでindex追加
class CreatePageviews < ActiveRecord::Migration
def change
create_table :pageviews, id: false do |t|
t.integer :id, null: false
t.integer :article_id, null: false
t.integer :category_id, null: false
@amyroi
amyroi / app_models_syllabus.rb
Last active Sep 29, 2015
ActiveModel:FactoryGirl.rb
View app_models_syllabus.rb
# app/models/syllabus.rb
class Syllabus
include ActiveModel::Model
attr_accessor :records, :academic_year, :course
validates :academic_year, presence: true
def initialize(academic_year, course = nil)
@academic_year = academic_year
@course = course
@amyroi
amyroi / active_model_account.rb
Last active Sep 8, 2015
SimpleForm + ActiveModel:validation
View active_model_account.rb
# app/models/account.rb
class Account
include ActiveModel::Model
validates :name, presence: true
def initialize
# ~
end
end