Last active
September 29, 2015 05:11
-
-
Save amyroi/7f2b2ec6aab4ff6bf868 to your computer and use it in GitHub Desktop.
ActiveModel:FactoryGirl.rb
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
# 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 |
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
# 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 |
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
# 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 |
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
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 |
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
# 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