Guest User

Untitled

a guest
Jan 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. class UserSeeder
  2. PASSWORD = 'Fri3nd0!'
  3.  
  4. def puts_indent(indent, str)
  5. puts((' ' * indent) + str)
  6. end
  7.  
  8. def add_license(student)
  9. contract = student.institution.institution_contracts.first
  10. License.where(
  11. user_id: student.id,
  12. institution_contract_id: contract.id,
  13. first_activation_date: contract.first_activation_date
  14. ).first_or_create!.activate!
  15. end
  16.  
  17. def tenant_content_packages(tenant)
  18. {
  19. 'richardson' => [1651, 1953, 1273, 1456, 1455],
  20. 'cliffsnotes' => [1197, 1141, 1140, 1142, 1133],
  21. 'ppagu' => [743],
  22. 'eatright' => [944],
  23. 'act' => [1099],
  24. 'naviance' => [1183, 976, 855, 879, 923],
  25. 'sasb' => [815],
  26. 'ins' => [1033],
  27. 'hrci' => [1051, 1624, 1623, 1403, 744],
  28. 'hrcitests' => [938, 934, 937, 1225, 933],
  29. 'benchprep' => [1196, 1511],
  30. 'ocl' => [1506, 1378, 1381, 1619, 1373],
  31. 'cfainstitute' => [1357, 1358, 1359, 1964, 1962],
  32. 'gmac' => [1296, 1605, 1618],
  33. 'comptia' => [1458, 1228, 1305, 1532, 1759],
  34. 'actworkkeys' => [1336, 1413, 1412, 1451, 1452],
  35. 'ism' => [1579, 1578, 1526],
  36. 'aamc' => [1692, 1657, 1658, 1625, 1663],
  37. 'mhprofessional' => [1340, 1321, 1337, 1339, 1335],
  38. 'becker' => [1440],
  39. 'petersons' => [1836, 1824, 1763, 1853, 1832],
  40. 'aia' => [1991, 2028],
  41. 'nrp' => [1598, 1599, 1600, 1601],
  42. 'ascm' => [1782],
  43. 'hpe' => [1652, 1994, 1995, 1998],
  44. 'aba' => [1997],
  45. 'ncbe' => [2094, 2064, 2063, 2057, 2058],
  46. 'dalton' => [2069, 2079],
  47. 'abms' => [3157]
  48. }[tenant.slug]
  49. end
  50.  
  51. def add_enrollments(student)
  52. tenant_content_packages(student.tenant).each do |course_id|
  53. Enrollment.where(
  54. user_id: student.id,
  55. content_package_id: course_id,
  56. license: student.licenses.active.last
  57. ).first_or_create!.activate!
  58. end
  59. end
  60.  
  61. def add_student(institution, group, number)
  62. description = [
  63. 'Good Student',
  64. 'Bad Student',
  65. 'Random Student',
  66. 'Logged-in Student',
  67. 'Non-logged-in Student'
  68. ][number - 1]
  69.  
  70. group_code = "+grp#{group.name.match(/(\d)/).captures.first}" if group
  71. institution_code = "+inst#{institution.name.match(/(\d)/).captures.first}"
  72. district_code = "+dist#{institution.district.name.match(/(\d)/).captures.first}"
  73.  
  74. student = User.where(
  75. name: "Student #{number}: #{description}",
  76. tenant_id: institution.tenant.id,
  77. institution_id: institution.id,
  78. district_id: institution.district.id,
  79. email: "qa+b#{institution.tenant.slug}+stu#{number}#{district_code}#{institution_code}#{group_code}@benchprep.com"
  80. ).first_or_initialize
  81.  
  82. student.password = PASSWORD
  83. student.save!
  84.  
  85. add_license(student)
  86. add_enrollments(student)
  87.  
  88. group.join_as(student.id, UserGroup::MemberType::Student) if group
  89. puts_indent (group ? 6 : 3), "- #{student.name} (#{student.email}) [#{student.id}]"
  90. end
  91.  
  92. def add_instructor(institution, instructor_number)
  93. instructor = User.where(
  94. name: "QA #{institution.tenant.name} Instructor #{instructor_number}",
  95. tenant_id: institution.tenant.id,
  96. institution_id: institution.id,
  97. district_id: institution.district.id,
  98. email: "qa+b#{institution.tenant.slug}+instr#{instructor_number}@benchprep.com"
  99. ).first_or_initialize
  100.  
  101. instructor.password = PASSWORD
  102. instructor.save!
  103. puts_indent 3, "- #{instructor.name} [#{instructor.id}]"
  104. instructor
  105. end
  106.  
  107. def add_group(institution, instructor, number)
  108. group = Group.where(
  109. name: "Group #{number}",
  110. tenant_id: institution.tenant.id,
  111. institution_id: institution.id
  112. ).first_or_create!
  113. group.join_as(instructor.id, UserGroup::MemberType::Instructor)
  114.  
  115. puts_indent 4, "- #{group.name} [#{group.id}]"
  116. (1..5).each do |student_number|
  117. add_student(institution, group, student_number)
  118. end
  119. end
  120.  
  121. def add_institution_contract(institution)
  122. start_date = Date.parse('2018-01-01')
  123. end_date = start_date + 100.years
  124.  
  125. contract = InstitutionContract.where(
  126. institution_id: institution.id,
  127. contract_type: 'limited_seat',
  128. start_date: start_date,
  129. first_activation_date: start_date,
  130. last_activation_date: end_date,
  131. end_date: end_date,
  132. admin_access_expiration_date: end_date,
  133. name: "#{institution.name} Contract",
  134. max_loaded_count: 1000,
  135. ).first_or_initialize
  136. contract.content_package_ids = tenant_content_packages(institution.tenant)
  137. contract.save!
  138. end
  139.  
  140. def add_institution_supervisor(institution, institution_number)
  141. supervisor = User.where(
  142. name: "#{institution.name} Supervisor",
  143. tenant_id: institution.tenant.id,
  144. institution_id: institution.id,
  145. email: "qa+b#{institution.tenant.slug}+inst+sprvsr#{institution_number}@benchprep.com"
  146. ).first_or_initialize
  147.  
  148. supervisor.password = PASSWORD
  149. supervisor.save!
  150. supervisor.add_role(:institution_supervisor, full_access: true)
  151. puts_indent 3, "- #{supervisor.name} [#{supervisor.id}]"
  152. end
  153.  
  154. def add_institution(district, number)
  155. institution = Institution.where(
  156. name: "QA #{district.tenant.name} Institution #{number}",
  157. tenant_id: district.tenant.id,
  158. district_id: district.id
  159. ).first_or_create!
  160.  
  161. add_institution_contract(institution)
  162.  
  163. puts_indent 2, "- #{institution.name} [#{institution.id}]"
  164. instructor = add_instructor(institution, number)
  165. add_institution_supervisor(institution, number)
  166.  
  167. (1..2).each do |group_number|
  168. add_group(institution, instructor, group_number)
  169. end
  170. (1..5).each do |student_number|
  171. add_student(institution, nil, student_number)
  172. end
  173. institution.update_counters
  174. end
  175.  
  176. def add_district_supervisor(district, district_number)
  177. supervisor = User.where(
  178. name: "#{district.name} Supervisor",
  179. tenant_id: district.tenant.id,
  180. district_id: district.id,
  181. email: "qa+b#{district.tenant.slug}+dst+sprvsr#{district_number}@benchprep.com"
  182. ).first_or_initialize
  183.  
  184. supervisor.password = PASSWORD
  185. supervisor.save!
  186. supervisor.add_role(:district_supervisor, full_access: true)
  187. puts_indent 2, "- #{supervisor.name} [#{supervisor.id}]"
  188. end
  189.  
  190. def add_district(tenant, district_number)
  191. district = District.where(
  192. name: "QA #{tenant.name} District #{district_number}",
  193. tenant_id: tenant.id
  194. ).first_or_create!
  195.  
  196. puts_indent 1, "- #{district.name} [#{district.id}]"
  197. add_district_supervisor(district, district_number)
  198.  
  199. (1..2).each do |institution_number|
  200. add_institution(district, (district_number - 1) * 2 + institution_number)
  201. end
  202. district
  203. end
  204.  
  205. def add_tenant_data(tenant)
  206. puts_indent 0, "- #{tenant.name}"
  207. (1..2).map do |district_number|
  208. add_district(tenant, district_number)
  209. end
  210. end
  211. end
  212.  
  213. tenants = %w[
  214. richardson
  215. cliffsnotes
  216. ppagu
  217. eatright
  218. act
  219. naviance
  220. sasb
  221. ins
  222. hrci
  223. hrcitests
  224. benchprep
  225. ocl
  226. cfainstitute
  227. gmac
  228. comptia
  229. actworkkeys
  230. ism
  231. aamc
  232. mhprofessional
  233. becker
  234. petersons
  235. aia
  236. nrp
  237. ascm
  238. hpe
  239. aba
  240. ncbe
  241. dalton
  242. abms
  243. ]
  244.  
  245. seeder = UserSeeder.new
  246.  
  247. ActiveRecord::Base.transaction do
  248. tenants.each do |tenant|
  249. seeder.add_tenant_data(Tenant.find_by_slug(tenant))
  250. end
  251. end
Add Comment
Please, Sign In to add comment