Class: Nextgen::RailsOptions

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nextgen/rails_options.rb

Constant Summary collapse

TEST_FRAMEWORKS =
%i[minitest rspec].freeze
FRAMEWORKS =
%i[
  action_mailer
  action_mailbox
  action_text
  active_job
  active_storage
  action_cable
  hotwire
  jbuilder
].freeze
JS_PACKAGE_MANAGERS =
%i[npm yarn].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:) ⇒ RailsOptions

Returns a new instance of RailsOptions.



28
29
30
31
32
33
34
35
36
37
# File 'lib/nextgen/rails_options.rb', line 28

def initialize(version:)
  @version = version
  @api = false
  @vite = false
  @enable_features = []
  @skip_features = []
  @skip_system_test = false
  @test_framework = :minitest
  @js_package_manager = :yarn
end

Instance Attribute Details

#asset_pipelineObject

Returns the value of attribute asset_pipeline.



24
25
26
# File 'lib/nextgen/rails_options.rb', line 24

def asset_pipeline
  @asset_pipeline
end

#cssObject

Returns the value of attribute css.



24
25
26
# File 'lib/nextgen/rails_options.rb', line 24

def css
  @css
end

#databaseObject

Returns the value of attribute database.



24
25
26
# File 'lib/nextgen/rails_options.rb', line 24

def database
  @database
end

#javascriptObject

Returns the value of attribute javascript.



24
25
26
# File 'lib/nextgen/rails_options.rb', line 24

def javascript
  @javascript
end

#js_package_managerObject

Returns the value of attribute js_package_manager.



24
25
26
# File 'lib/nextgen/rails_options.rb', line 24

def js_package_manager
  @js_package_manager
end

#test_frameworkObject

Returns the value of attribute test_framework.



24
25
26
# File 'lib/nextgen/rails_options.rb', line 24

def test_framework
  @test_framework
end

Instance Method Details

#action_mailer?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/nextgen/rails_options.rb', line 170

def action_mailer?
  !skip_default_feature?(:action_mailer)
end

#active_job?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/nextgen/rails_options.rb', line 174

def active_job?
  !skip_default_feature?(:active_job)
end

#active_record?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/nextgen/rails_options.rb', line 142

def active_record?
  !skip_active_record?
end

#api!Object

Raises:

  • (ArgumentError)


111
112
113
114
115
116
# File 'lib/nextgen/rails_options.rb', line 111

def api!
  raise ArgumentError, "Can't specify API mode if css is already specified" if css
  raise ArgumentError, "Can't specify API mode if javascript is already specified" if javascript

  @api = true
end

#api?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/nextgen/rails_options.rb', line 118

def api?
  @api
end

#devcontainer!Object



107
108
109
# File 'lib/nextgen/rails_options.rb', line 107

def devcontainer!
  @devcontainer = true
end

#enable_optional_feature!(feature) ⇒ Object

Raises:

  • (ArgumentError)


187
188
189
190
191
# File 'lib/nextgen/rails_options.rb', line 187

def enable_optional_feature!(feature)
  raise ArgumentError, "Unknown feature: #{feature}" unless optional_features.include?(feature)

  enable_features << feature
end

#frontend?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/nextgen/rails_options.rb', line 122

def frontend?
  !api?
end

#minitest?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/nextgen/rails_options.rb', line 130

def minitest?
  @test_framework == :minitest
end

#npm?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/nextgen/rails_options.rb', line 69

def npm?
  js_package_manager == :npm
end

#postgresql?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/nextgen/rails_options.rb', line 93

def postgresql?
  database == :postgresql
end

#requires_node?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/nextgen/rails_options.rb', line 126

def requires_node?
  %w[bootstrap bulma postcss sass].include?(css) || %w[webpack esbuild rollup].include?(javascript)
end

#rspec?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/nextgen/rails_options.rb', line 134

def rspec?
  @test_framework == :rspec
end

#rubocop?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/nextgen/rails_options.rb', line 138

def rubocop?
  !skip_default_feature?(:rubocop)
end

#skip_active_record?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/nextgen/rails_options.rb', line 146

def skip_active_record?
  defined?(@database) && @database.nil?
end

#skip_asset_pipeline?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/nextgen/rails_options.rb', line 150

def skip_asset_pipeline?
  defined?(@asset_pipeline) && @asset_pipeline.nil?
end

#skip_default_feature!(feature) ⇒ Object

Raises:

  • (ArgumentError)


193
194
195
196
197
# File 'lib/nextgen/rails_options.rb', line 193

def skip_default_feature!(feature)
  raise ArgumentError, "Unknown feature: #{feature}" unless skippable_features.include?(feature)

  skip_features << feature
end

#skip_default_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


199
200
201
202
203
# File 'lib/nextgen/rails_options.rb', line 199

def skip_default_feature?(feature)
  raise ArgumentError, "Unknown feature: #{feature}" unless skippable_features.include?(feature)

  skip_features.include?(feature)
end

#skip_javascript?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/nextgen/rails_options.rb', line 83

def skip_javascript?
  defined?(@javascript) && @javascript.nil?
end

#skip_kamal?Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/nextgen/rails_options.rb', line 178

def skip_kamal?
  # Depending on the Rails version, kamal may not exist, in which case we can consider it "skipped".
  !skippable_features.include?(:kamal) || skip_default_feature?(:kamal)
end

#skip_solid?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/nextgen/rails_options.rb', line 183

def skip_solid?
  !skippable_features.include?(:solid) || skip_default_feature?(:solid)
end

#skip_system_test!Object



154
155
156
# File 'lib/nextgen/rails_options.rb', line 154

def skip_system_test!
  @skip_system_test = true
end

#skip_system_test?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/nextgen/rails_options.rb', line 158

def skip_system_test?
  @skip_system_test
end

#skip_test?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/nextgen/rails_options.rb', line 162

def skip_test?
  defined?(@test_framework) && [nil, :rspec].include?(@test_framework)
end

#system_testing?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/nextgen/rails_options.rb', line 166

def system_testing?
  !(api? || test_framework.nil? || skip_system_test?)
end

#test_framework?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/nextgen/rails_options.rb', line 103

def test_framework?
  !!@test_framework
end

#to_argsObject

rubocop:disable Metrics/PerceivedComplexity



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/nextgen/rails_options.rb', line 205

def to_args # rubocop:disable Metrics/PerceivedComplexity
  [].tap do |args|
    args.push(*version.args)
    args << "--api" if api?
    args << "--skip-active-record" if skip_active_record?
    args << "--skip-asset-pipeline" if skip_asset_pipeline?
    args << "--skip-javascript" if skip_javascript?
    args << "--skip-test" if skip_test?
    args << "--skip-system-test" if skip_system_test?
    args << "--asset-pipeline=#{asset_pipeline}" if asset_pipeline && asset_pipeline != asset_pipelines.keys.first
    args << "--database=#{database}" if database
    args << "--css=#{css}" if css
    args << "--javascript=#{javascript}" if javascript
    args.push(*enable_features.map { "--#{_1.to_s.tr("_", "-")}" })
    args.push(*skip_features.map { "--skip-#{_1.to_s.tr("_", "-")}" })
  end
end

#version_labelObject



39
40
41
# File 'lib/nextgen/rails_options.rb', line 39

def version_label
  version.label
end

#vite!Object



73
74
75
76
77
# File 'lib/nextgen/rails_options.rb', line 73

def vite!
  self.asset_pipeline = nil
  @javascript = "esbuild"
  @vite = true
end

#vite?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/nextgen/rails_options.rb', line 79

def vite?
  @vite
end