auto_wizzard.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. module AutoWizard
  2. =begin
  3. creates or updates Users, EmailAddresses and sets Settings based on the 'auto_wizard.json' file placed in the root directory.
  4. there is an example file 'contrib/auto_wizard_example.json'
  5. AutoWizard.setup
  6. returns
  7. the first created User if a 'auto_wizard.json' file was found and processed, containing at least one entry in Users
  8. the User with id 1 (NULL) if a 'auto_wizard.json' file was found and processed, containing no Users
  9. nil if no 'auto_wizard.json' file was found
  10. =end
  11. def self.setup
  12. auto_wizard_file_name = 'auto_wizard.json'
  13. auto_wizard_file_name = "#{Rails.root.to_s}/#{auto_wizard_file_name}"
  14. return if !File.file?(auto_wizard_file_name)
  15. auto_wizard_file = File.read(auto_wizard_file_name)
  16. auto_wizard_hash = JSON.parse(auto_wizard_file)
  17. admin_user = User.find( 1 )
  18. # create Users
  19. if auto_wizard_hash['Users']
  20. roles = Role.where( name: ['Agent', 'Admin'] )
  21. groups = Group.all
  22. auto_wizard_hash['Users'].each { |user_data|
  23. user_data_symbolized = user_data.symbolize_keys
  24. user_data_symbolized = user_data_symbolized.merge(
  25. {
  26. active: true,
  27. roles: roles,
  28. groups: groups,
  29. updated_by_id: admin_user.id,
  30. created_by_id: admin_user.id
  31. }
  32. )
  33. created_user = User.create_or_update(
  34. user_data_symbolized
  35. )
  36. # use first created user as admin
  37. next if admin_user.id != 1
  38. admin_user = created_user
  39. }
  40. end
  41. # set Settings
  42. if auto_wizard_hash['Settings']
  43. auto_wizard_hash['Settings'].each { |setting_data|
  44. Setting.set( setting_data['name'], setting_data['value'] )
  45. }
  46. end
  47. # add EmailAddresses
  48. if auto_wizard_hash['EmailAddresses']
  49. auto_wizard_hash['EmailAddresses'].each { |email_address_data|
  50. email_address_data_symbolized = email_address_data.symbolize_keys
  51. email_address_data_symbolized = email_address_data_symbolized.merge(
  52. {
  53. updated_by_id: admin_user.id,
  54. created_by_id: admin_user.id
  55. }
  56. )
  57. EmailAddress.create_if_not_exists(
  58. email_address_data_symbolized
  59. )
  60. }
  61. end
  62. admin_user
  63. end
  64. end