template.rb 5.8 KB

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