Top Level Namespace

Defined Under Namespace

Modules: GakuSample

Instance Method Summary collapse

Instance Method Details

#batch_create(count) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shared_sample_data.rb', line 26

def batch_create(count)
  ActiveRecord::Base.transaction do
    if Rails.env.development?
      bar = RakeProgressbar.new(count)
      count.times do
        bar.inc
        yield
      end
      bar.finished
    else
      count.times { yield }
    end
  end
end

#create_non_active_student(predefined_student = nil) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/shared_sample_data.rb', line 117

def create_non_active_student(predefined_student = nil)
  if predefined_student
    Gaku::Student.where(predefined_student).first_or_create!
  else
    random_student = random_person.merge(enrollment_status_code: @enrollment_status_applicant)
    Gaku::Student.where(random_student).first_or_create!
  end
end

#create_student_with_full_info(predefined_student = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/shared_sample_data.rb', line 90

def create_student_with_full_info(predefined_student = nil)
  if predefined_student
    student = Gaku::Student.where(predefined_student).first_or_create!
  else
    random_student = random_person.merge(enrollment_status_code: @enrollment_status)
    student = Gaku::Student.where(random_student).first_or_create!
  end

  student.addresses.where(random_address).first_or_create!

  [random_email, random_home_phone, random_mobile_phone].each do |params|
    Gaku::ContactCreation.new(params.merge(contactable: student)).save!
  end

  student.notes.where(random_note).first_or_create!
  student.notes.where(random_note).first_or_create!

  guardian = Gaku::Guardian.where(random_person).first_or_create!
  guardian.addresses.where(random_address).first_or_create!

  [random_email, random_home_phone, random_mobile_phone].each do |params|
    Gaku::ContactCreation.new(params.merge(contactable: guardian)).save!
  end

  student.guardians << guardian
end

#create_teacher_with_full_info(predefined_teacher = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/shared_sample_data.rb', line 126

def create_teacher_with_full_info(predefined_teacher = nil)
  if predefined_teacher
    teacher = Gaku::Teacher.where(predefined_teacher).first_or_create!
  else
    random_teacher = random_person
    teacher = Gaku::Teacher.where(random_teacher).first_or_create!
  end

  teacher.addresses.create!(random_address)

  [random_email, random_home_phone, random_mobile_phone].each do |params|
    Gaku::ContactCreation.new(params.merge(contactable: teacher)).save!
  end

  teacher.notes.create!(random_note)
  teacher.notes.create!(random_note)
end

#random_addressObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shared_sample_data.rb', line 78

def random_address
  {
    address1: FFaker::Address.street_address,
    address2: FFaker::Address.street_address,
    title: 'Home address',
    zipcode: '452-0813',
    city: 'Nagoya',
    country: @country,
    state: @state
  }
end

#random_emailObject



64
65
66
67
68
69
# File 'lib/shared_sample_data.rb', line 64

def random_email
  {
    data: FFaker::Internet.email,
    contact_type_id: @email.id
  }
end

#random_home_phoneObject



50
51
52
53
54
55
# File 'lib/shared_sample_data.rb', line 50

def random_home_phone
  {
    data: FFaker::PhoneNumber.phone_number,
    contact_type_id: @home_phone.id
  }
end

#random_mobile_phoneObject



57
58
59
60
61
62
# File 'lib/shared_sample_data.rb', line 57

def random_mobile_phone
  {
    data: FFaker::PhoneNumber.phone_number,
    contact_type_id: @mobile_phone.id
  }
end

#random_noteObject



71
72
73
74
75
76
# File 'lib/shared_sample_data.rb', line 71

def random_note
  {
    title: FFaker::Lorem.word,
    content: FFaker::Lorem.sentence
  }
end

#random_personObject



41
42
43
44
45
46
47
48
# File 'lib/shared_sample_data.rb', line 41

def random_person
  {
    name: FFaker::Name.first_name,
    middle_name: FFaker::Name.first_name,
    surname: FFaker::Name.last_name,
    birth_date: Date.today - rand(1000)
  }
end