template.rb 5.0 KB

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