Class: Decidim::Generators::AppGenerator

Inherits:
Rails::Generators::AppGenerator
  • Object
show all
Defined in:
decidim-generators/lib/decidim/generators/app_generator.rb

Overview

Generates a Rails app and installs decidim to it. Uses the default Rails generator for most of the work.

Remember that, for how generators work, actions are executed based on the definition order of the public methods.

Instance Method Summary collapse

Instance Method Details

#add_ignore_uploadsObject


224
225
226
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 224

def add_ignore_uploads
  append_file ".gitignore", "\n# Ignore public uploads\npublic/uploads" unless options["skip_git"]
end

#add_production_gems(&block) ⇒ Object


189
190
191
192
193
194
195
196
197
198
199
200
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 189

def add_production_gems(&block)
  return if options[:skip_gemfile]

  if block
    @production_gems ||= []
    @production_gems << block
  elsif @production_gems.present?
    gem_group :production do
      @production_gems.map(&:call)
    end
  end
end

#add_queue_adapterObject


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 164

def add_queue_adapter
  adapter = options[:queue]

  abort("#{adapter} is not supported as a queue adapter, please use sidekiq for the moment") unless adapter.in?(["", "sidekiq"])

  return unless adapter == "sidekiq"

  template "sidekiq.yml.erb", "config/sidekiq.yml", force: true

  gsub_file "config/environments/production.rb",
            /# config.active_job.queue_adapter     = :resque/,
            "config.active_job.queue_adapter = ENV['QUEUE_ADAPTER'] if ENV['QUEUE_ADAPTER'].present?"

  prepend_file "config/routes.rb", "require \"sidekiq/web\"\n\n"
  route <<~RUBY
    authenticate :user, ->(u) { u.admin? } do
      mount Sidekiq::Web => "/sidekiq"
    end
  RUBY

  add_production_gems do
    gem "sidekiq"
  end
end

#add_storage_providerObject


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 147

def add_storage_provider
  template "storage.yml.erb", "config/storage.yml", force: true

  providers = options[:storage].split(",")

  abort("#{providers} is not supported as storage provider, please use local, s3, gcs or azure") unless (providers - %w(local s3 gcs azure)).empty?
  gsub_file "config/environments/production.rb",
            /config.active_storage.service = :local/,
            "config.active_storage.service = Rails.application.secrets.dig(:storage, :provider) || :local"

  add_production_gems do
    gem "aws-sdk-s3", require: false if providers.include?("s3")
    gem "azure-storage-blob", require: false if providers.include?("azure")
    gem "google-cloud-storage", "~> 1.11", require: false if providers.include?("gcs")
  end
end

#authorization_handlerObject


259
260
261
262
263
264
265
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 259

def authorization_handler
  return unless options[:demo]

  copy_file "dummy_authorization_handler.rb", "app/services/dummy_authorization_handler.rb"
  copy_file "another_dummy_authorization_handler.rb", "app/services/another_dummy_authorization_handler.rb"
  copy_file "verifications_initializer.rb", "config/initializers/decidim_verifications.rb"
end

#budgets_workflowsObject


275
276
277
278
279
280
281
282
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 275

def budgets_workflows
  return unless options[:demo]

  copy_file "budgets_workflow_random.rb", "lib/budgets_workflow_random.rb"
  copy_file "budgets_workflow_random.en.yml", "config/locales/budgets_workflow_random.en.yml"

  copy_file "budgets_initializer.rb", "config/initializers/decidim_budgets.rb"
end

#cable_ymlObject


109
110
111
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 109

def cable_yml
  template "cable.yml.erb", "config/cable.yml", force: true
end

#database_ymlObject


92
93
94
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 92

def database_yml
  template "database.yml.erb", "config/database.yml", force: true
end

#decidim_controllerObject


96
97
98
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 96

def decidim_controller
  template "decidim_controller.rb.erb", "app/controllers/decidim_controller.rb", force: true
end

#decidim_initializerObject


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 233

def decidim_initializer
  copy_file "initializer.rb", "config/initializers/decidim.rb"

  gsub_file "config/environments/production.rb",
            /config.log_level = :info/,
            "config.log_level = %w(debug info warn error fatal).include?(ENV['RAILS_LOG_LEVEL']) ? ENV['RAILS_LOG_LEVEL'] : :info"

  gsub_file "config/environments/production.rb",
            %r{# config.asset_host = 'http://assets.example.com'},
            "config.asset_host = ENV['RAILS_ASSET_HOST'] if ENV['RAILS_ASSET_HOST'].present?"

  if options[:force_ssl] == "false"
    gsub_file "config/initializers/decidim.rb",
              /# config.force_ssl = true/,
              "config.force_ssl = false"
  end
  return if options[:locales].blank?

  gsub_file "config/initializers/decidim.rb",
            /#{Regexp.escape("# config.available_locales = %w(en ca es)")}/,
            "config.available_locales = %w(#{options[:locales].gsub(",", " ")})"
  gsub_file "config/initializers/decidim.rb",
            /#{Regexp.escape("config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]")}/,
            "# config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]"
end

#dockerObject


100
101
102
103
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 100

def docker
  template "Dockerfile.erb", "Dockerfile"
  template "docker-compose.yml.erb", "docker-compose.yml"
end

#etherpadObject


105
106
107
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 105

def etherpad
  template "docker-compose-etherpad.yml", "docker-compose-etherpad.yml"
end

#gemfileObject


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 121

def gemfile
  return if options[:skip_gemfile]

  if branch.present?
    get target_gemfile, "Gemfile", force: true
    get "#{target_gemfile}.lock", "Gemfile.lock", force: true
  else
    copy_file target_gemfile, "Gemfile", force: true
    copy_file "#{target_gemfile}.lock", "Gemfile.lock", force: true
  end

  gsub_file "Gemfile", /gem "#{current_gem}".*/, "gem \"#{current_gem}\", #{gem_modifier}"

  return unless current_gem == "decidim"

  gsub_file "Gemfile", /gem "decidim-dev".*/, "gem \"decidim-dev\", #{gem_modifier}"

  %w(conferences consultations elections initiatives templates).each do |component|
    if options[:demo]
      gsub_file "Gemfile", /gem "decidim-#{component}".*/, "gem \"decidim-#{component}\", #{gem_modifier}"
    else
      gsub_file "Gemfile", /gem "decidim-#{component}".*/, "# gem \"decidim-#{component}\", #{gem_modifier}"
    end
  end
end

#installObject


308
309
310
311
312
313
314
315
316
317
318
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 308

def install
  Decidim::Generators::InstallGenerator.start(
    [
      "--recreate_db=#{options[:recreate_db]}",
      "--seed_db=#{options[:seed_db]}",
      "--skip_gemfile=#{options[:skip_gemfile]}",
      "--app_name=#{app_name}",
      "--profiling=#{options[:profiling]}"
    ]
  )
end

#licenseObject


117
118
119
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 117

def license
  template "LICENSE-AGPLv3.txt", "LICENSE-AGPLv3.txt"
end

#machine_translation_serviceObject


300
301
302
303
304
305
306
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 300

def machine_translation_service
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.machine_translation_service = "MyTranslationService"/,
            "config.machine_translation_service = 'Decidim::Dev::DummyTranslator'"
end

#pdf_signature_serviceObject


292
293
294
295
296
297
298
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 292

def pdf_signature_service
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.pdf_signature_service = "MyPDFSignatureService"/,
            "config.pdf_signature_service = \"Decidim::Initiatives::PdfSignatureExample\""
end

#readmeObject


113
114
115
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 113

def readme
  template "README.md.erb", "README.md", force: true
end

#remove_default_error_pagesObject


228
229
230
231
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 228

def remove_default_error_pages
  remove_file "public/404.html"
  remove_file "public/500.html"
end

#sms_gatewayObject


267
268
269
270
271
272
273
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 267

def sms_gateway
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.sms_gateway_service = "MySMSGatewayService"/,
            "config.sms_gateway_service = 'Decidim::Verifications::Sms::ExampleGateway'"
end

#source_pathsObject


19
20
21
22
23
24
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 19

def source_paths
  [
    self.class.source_root,
    Rails::Generators::AppGenerator.source_root
  ]
end

#timestamp_serviceObject


284
285
286
287
288
289
290
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 284

def timestamp_service
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.timestamp_service = "MyTimestampService"/,
            "config.timestamp_service = \"Decidim::Initiatives::DummyTimestamp\""
end

#tweak_bootsnapObject


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 202

def tweak_bootsnap
  gsub_file "config/boot.rb", %r{require 'bootsnap/setup'.*$}, <<~RUBY.rstrip
    require "bootsnap"

    env = ENV["RAILS_ENV"] || "development"

    Bootsnap.setup(
      cache_dir: File.expand_path(File.join("..", "tmp", "cache"), __dir__),
      development_mode: env == "development",
      load_path_cache: true,
      compile_cache_iseq: !ENV["SIMPLECOV"],
      compile_cache_yaml: true
    )
  RUBY
end

#tweak_springObject


218
219
220
221
222
# File 'decidim-generators/lib/decidim/generators/app_generator.rb', line 218

def tweak_spring
  return unless File.exist?("config/spring.rb")

  prepend_to_file "config/spring.rb", "require \"decidim/spring\"\n\n"
end