template.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. def source_paths
  2. [File.expand_path(File.dirname(__FILE__))]
  3. end
  4. def add_gems
  5. gem 'administrate', '~> 0.8.1'
  6. gem 'devise', '~> 4.4.3'
  7. gem 'devise-bootstrapped', github: 'excid3/devise-bootstrapped', branch: 'bootstrap4'
  8. gem 'devise_masquerade', '~> 0.6.0'
  9. gem 'font-awesome-sass', '~> 4.7'
  10. gem 'gravatar_image_tag', github: 'mdeering/gravatar_image_tag'
  11. gem 'jquery-rails', '~> 4.3.1'
  12. gem 'bootstrap', '~> 4.0.0.beta'
  13. gem 'webpacker', '~> 3.0'
  14. gem 'sidekiq', '~> 5.0'
  15. gem 'foreman', '~> 0.84.0'
  16. gem 'omniauth-facebook', '~> 4.0'
  17. gem 'omniauth-twitter', '~> 1.4'
  18. gem 'omniauth-github', '~> 1.3'
  19. gem 'whenever', require: false
  20. end
  21. def add_users
  22. # Install Devise
  23. generate "devise:install"
  24. # Configure Devise
  25. environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }",
  26. env: 'development'
  27. route "root to: 'home#index'"
  28. # Devise notices are installed via Bootstrap
  29. generate "devise:views:bootstrapped"
  30. # Create Devise User
  31. generate :devise, "User",
  32. "name",
  33. "announcements_last_read_at:datetime",
  34. "admin:boolean"
  35. # Set admin default to false
  36. in_root do
  37. migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) }
  38. gsub_file migration, /:admin/, ":admin, default: false"
  39. end
  40. requirement = Gem::Requirement.new("> 5.2")
  41. rails_version = Gem::Version.new(Rails::VERSION::STRING)
  42. if requirement.satisfied_by? rails_version
  43. gsub_file "config/initializers/devise.rb",
  44. / # config.secret_key = .+/,
  45. " config.secret_key = Rails.application.credentials.secret_key_base"
  46. end
  47. # Add Devise masqueradable to users
  48. inject_into_file("app/models/user.rb", "omniauthable, :masqueradable, :", after: "devise :")
  49. end
  50. def add_bootstrap
  51. # Remove Application CSS
  52. run "rm app/assets/stylesheets/application.css"
  53. # Add Bootstrap JS
  54. insert_into_file(
  55. "app/assets/javascripts/application.js",
  56. "\n//= require jquery\n//= require popper\n//= require bootstrap",
  57. after: "//= require rails-ujs"
  58. )
  59. end
  60. def copy_templates
  61. directory "app", force: true
  62. directory "config", force: true
  63. route "get '/terms', to: 'home#terms'"
  64. route "get '/privacy', to: 'home#privacy'"
  65. end
  66. def add_webpack
  67. rails_command 'webpacker:install'
  68. end
  69. def add_sidekiq
  70. environment "config.active_job.queue_adapter = :sidekiq"
  71. insert_into_file "config/routes.rb",
  72. "require 'sidekiq/web'\n\n",
  73. before: "Rails.application.routes.draw do"
  74. insert_into_file "config/routes.rb",
  75. " authenticate :user, lambda { |u| u.admin? } do\n mount Sidekiq::Web => '/sidekiq'\n end\n\n",
  76. after: "Rails.application.routes.draw do\n"
  77. end
  78. def add_foreman
  79. copy_file "Procfile"
  80. end
  81. def add_announcements
  82. generate "model Announcement published_at:datetime announcement_type name description:text"
  83. route "resources :announcements, only: [:index]"
  84. end
  85. def add_notifications
  86. generate "model Notification recipient_id:integer actor_id:integer read_at:datetime action:string notifiable_id:integer notifiable_type:string"
  87. route "resources :notifications, only: [:index]"
  88. end
  89. def add_administrate
  90. generate "administrate:install"
  91. gsub_file "app/dashboards/announcement_dashboard.rb",
  92. /announcement_type: Field::String/,
  93. "announcement_type: Field::Select.with_options(collection: Announcement::TYPES)"
  94. gsub_file "app/controllers/admin/application_controller.rb",
  95. /# TODO Add authentication logic here\./,
  96. "redirect_to '/', alert: 'Not authorized.' unless user_signed_in? && current_user.admin?"
  97. end
  98. def add_multiple_authentication
  99. insert_into_file "config/routes.rb",
  100. ', controllers: { omniauth_callbacks: "users/omniauth_callbacks" }',
  101. after: " devise_for :users"
  102. generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
  103. template = """
  104. if Rails.application.secrets.facebook_app_id.present? && Rails.application.secrets.facebook_app_secret.present?
  105. config.omniauth :facebook, Rails.application.secrets.facebook_app_id, Rails.application.secrets.facebook_app_secret, scope: 'email,user_posts'
  106. end
  107. if Rails.application.secrets.twitter_app_id.present? && Rails.application.secrets.twitter_app_secret.present?
  108. config.omniauth :twitter, Rails.application.secrets.twitter_app_id, Rails.application.secrets.twitter_app_secret
  109. end
  110. if Rails.application.secrets.github_app_id.present? && Rails.application.secrets.github_app_secret.present?
  111. config.omniauth :github, Rails.application.secrets.github_app_id, Rails.application.secrets.github_app_secret
  112. end
  113. """.strip
  114. insert_into_file "config/initializers/devise.rb", " " + template + "\n\n",
  115. before: " # ==> Warden configuration"
  116. end
  117. def add_whenever
  118. run "wheneverize ."
  119. end
  120. # Main setup
  121. add_gems
  122. after_bundle do
  123. add_users
  124. add_bootstrap
  125. add_sidekiq
  126. add_foreman
  127. add_webpack
  128. add_announcements
  129. add_notifications
  130. add_multiple_authentication
  131. copy_templates
  132. # Migrate
  133. rails_command "db:create"
  134. rails_command "db:migrate"
  135. # Migrations must be done before this
  136. add_administrate
  137. add_whenever
  138. git :init
  139. git add: "."
  140. git commit: %Q{ -m 'Initial commit' }
  141. end