Created
May 5, 2015 11:55
-
-
Save joker1007/328ecb38bee3801ab28f to your computer and use it in GitHub Desktop.
FactoryGirlのtransientとtraitのサンプル
This file contains 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 CreateUsers < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :name | |
t.references :user_account | |
t.timestamps null: false | |
end | |
end | |
end |
This file contains 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 CreateUserAccounts < ActiveRecord::Migration | |
def change | |
create_table :user_accounts do |t| | |
t.string :email | |
t.string :encrypted_password | |
t.boolean :admin, null: false, default: false | |
t.boolean :inactive, null: false, default: false | |
t.timestamps null: false | |
end | |
create_table :login_histories do |t| | |
t.references :user_account | |
t.timestamps null: false | |
end | |
end | |
end |
This file contains 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 'active_record' | |
require 'factory_girl' | |
ActiveRecord::Base.establish_connection( | |
adapter: "sqlite3", | |
database: ":memory:" | |
) | |
class UserAccount < ActiveRecord::Base | |
has_many :login_histories | |
end | |
class LoginHistory < ActiveRecord::Base | |
belongs_to :user_account | |
end | |
class User < ActiveRecord::Base | |
belongs_to :user_account | |
delegate :email, to: :user_account | |
end | |
# ActiveRecord::Migration.verbose = false | |
ActiveRecord::Migrator.migrate File.expand_path("../db/migrate", __FILE__), nil | |
FactoryGirl.define do | |
factory :user do | |
name "joker1007" | |
transient do | |
email nil | |
encrypted_password nil | |
user_account_trait [] | |
end | |
user_account do | |
attributes = {email: email, encrypted_password: encrypted_password}.reject do |_, v| | |
v.nil? | |
end | |
FactoryGirl.create(:user_account, *user_account_trait, attributes) | |
end | |
trait :admin do | |
transient do | |
user_account_trait [:admin] | |
end | |
end | |
end | |
factory :user_account do | |
email "hoge@example.com" | |
encrypted_password "abcdefg12345" | |
trait :admin do | |
admin true | |
end | |
trait :with_login_histories do | |
transient do | |
login_count 5 | |
end | |
after(:create) do |user_account, evaluator| | |
evaluator.login_count.times do | |
user_account.login_histories.create! | |
end | |
end | |
end | |
end | |
end | |
p FactoryGirl.create(:user) | |
p FactoryGirl.create(:user).user_account | |
p FactoryGirl.create(:user, email: "foo@example.com").user_account | |
p FactoryGirl.create(:user, email: "bar@example.com", user_account_trait: [:with_login_histories]) | |
.user_account | |
.login_histories | |
p FactoryGirl.create(:user, :admin, email: "admin@example.com").user_account |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment