template.rb 5.7 KB

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