Skip to content

Instantly share code, notes, and snippets.

@amyroi
Last active September 29, 2015 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amyroi/7f2b2ec6aab4ff6bf868 to your computer and use it in GitHub Desktop.
Save amyroi/7f2b2ec6aab4ff6bf868 to your computer and use it in GitHub Desktop.
ActiveModel:FactoryGirl.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
end
end
# app/models/syllabus/record.rb
class Syllabus
class Record
attr_reader :format, :item, :contents, :course
def initialize(format, course = nil)
@format = format
@item = item
@course = course
@contents = contents
end
end
# ActiveModel Syllabus用 factory
# build専用 
# コンテンツなし build(:syllabus)
# コンテンツあり build(:syllabus, :has_content).build
FactoryGirl.define do
factory :syllabus do
academic_year factory: :academic_year, strategy: :build
course factory: :course, strategy: :build
initialize_with { new academic_year }
end
trait :has_content do
after(:build) do |syllabus|
records = create_list :syllabus_record, 5
end
end
end
FactoryGirl.define do
factory :syllabus_format do
association :academic_year, factory: :academic_year, strategy: :build
association :syllabus_item, factory: :syllabus_item, strategy: :build
display_order 1
length 1
end
trait :with_content do
after(:create) do |syllabus_format|
create :syllabus_item
create_list :syllabus_content, 5, syllabus_format: syllabus_format
end
end
end
# ActiveRecord Syllabus::Record用
# build専用 build(:syllabus_record)
FactoryGirl.define do
factory :syllabus_record, class: Syllabus::Record do
course factory: :course, strategy: :build
initialize_with { new create(:syllabus_format, :with_content), course }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment