template.rb 5.9 KB

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