20240729100324_add_checklists.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class AddChecklists < ActiveRecord::Migration[5.0]
  3. def up
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. create_tables
  7. create_template_tables
  8. create_setting
  9. create_permission
  10. end
  11. def create_permission
  12. Permission.create_if_not_exists(
  13. name: 'admin.checklist',
  14. label: 'Checklists',
  15. description: 'Manage ticket checklists of your system.',
  16. preferences: { prio: 1465 }
  17. )
  18. end
  19. def create_setting
  20. Setting.create_if_not_exists(
  21. title: 'Checklists',
  22. name: 'checklist',
  23. area: 'Web::Base',
  24. description: 'Enable checklists.',
  25. options: {
  26. form: [
  27. {
  28. display: '',
  29. null: true,
  30. name: 'checklist',
  31. tag: 'boolean',
  32. options: {
  33. true => 'yes',
  34. false => 'no',
  35. },
  36. },
  37. ],
  38. },
  39. preferences: {
  40. authentication: true,
  41. permission: ['admin.checklist'],
  42. },
  43. state: true,
  44. frontend: true
  45. )
  46. end
  47. def create_tables
  48. create_table :checklists do |t|
  49. t.string :name, limit: 250, null: false
  50. t.boolean :active, default: true, null: false
  51. if Rails.application.config.db_column_array
  52. t.string :sorted_item_ids, null: false, array: true, default: []
  53. else
  54. t.json :sorted_item_ids, null: false
  55. end
  56. t.references :created_by, null: false, foreign_key: { to_table: :users }
  57. t.references :updated_by, null: false, foreign_key: { to_table: :users }
  58. t.references :ticket, null: true, foreign_key: true, index: { unique: true }
  59. t.timestamps limit: 3, null: false
  60. end
  61. create_table :checklist_items do |t|
  62. t.text :text, null: false
  63. t.boolean :checked, null: false, default: false
  64. t.references :checklist, null: false, foreign_key: true
  65. t.references :created_by, null: false, foreign_key: { to_table: :users }
  66. t.references :updated_by, null: false, foreign_key: { to_table: :users }
  67. t.timestamps limit: 3, null: false
  68. end
  69. end
  70. def create_template_tables
  71. create_table :checklist_templates do |t|
  72. t.string :name, limit: 250, null: false
  73. t.boolean :active, default: true, null: false
  74. if Rails.application.config.db_column_array
  75. t.string :sorted_item_ids, null: false, array: true, default: []
  76. else
  77. t.json :sorted_item_ids, null: false
  78. end
  79. t.references :created_by, null: false, foreign_key: { to_table: :users }
  80. t.references :updated_by, null: false, foreign_key: { to_table: :users }
  81. t.timestamps limit: 3, null: false
  82. end
  83. create_table :checklist_template_items do |t|
  84. t.text :text, null: false
  85. t.references :checklist_template, null: false, foreign_key: true
  86. t.references :created_by, null: false, foreign_key: { to_table: :users }
  87. t.references :updated_by, null: false, foreign_key: { to_table: :users }
  88. t.timestamps limit: 3, null: false
  89. end
  90. end
  91. end