Module: RailsForge::FeatureGenerator

Defined in:
lib/railsforge/feature_generator.rb

Overview

FeatureGenerator module handles generating multiple related files for a feature

Defined Under Namespace

Classes: InvalidFeatureNameError

Class Method Summary collapse

Class Method Details

.find_rails_app_pathObject



231
232
233
234
235
236
237
238
239
240
# File 'lib/railsforge/feature_generator.rb', line 231

def self.find_rails_app_path
  path = Dir.pwd
  10.times do
    return path if File.exist?(File.join(path, "config", "application.rb"))
    parent = File.dirname(path)
    break if parent == path
    path = parent
  end
  nil
end

.form_spec_template(feature_name) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/railsforge/feature_generator.rb', line 457

def self.form_spec_template(feature_name)
  <<~RUBY
    require 'rails_helper'

    RSpec.describe #{feature_name}Form do
      let(:params) { {} }
      subject { described_class.new(#{feature_name.underscore}_params: params) }

      describe '#valid?' do
        it 'is valid with valid params' do
          expect(subject.valid?).to be_truthy
        end
      end

      describe '#save' do
        it 'saves successfully' do
          expect(subject.save).to be_truthy
        end
      end
    end
  RUBY
end

.form_template(feature_name) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/railsforge/feature_generator.rb', line 294

def self.form_template(feature_name)
  <<~RUBY
    # Form class for #{feature_name}
    # Encapsulates form validation logic
    #
    # Usage:
    #   form = #{feature_name}Form.new(params)
    #   if form.valid?
    #     form.save
    #   end
    class #{feature_name}Form
      include ActiveModel::Model

      attr_accessor :#{feature_name.underscore}_attributes

      validate :validate_#{feature_name.underscore}

      def save
        return false unless valid?

        # TODO: Implement save logic
        true
      end

      private

      def validate_#{feature_name.underscore}
        # TODO: Add validations
      end
    end
  RUBY
end

.generate(feature_name, with_spec: true) ⇒ Object

Generates multiple related files for a feature



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/railsforge/feature_generator.rb', line 22

def self.generate(feature_name, with_spec: true)
  validate_feature_name(feature_name)

  base_path = find_rails_app_path
  unless base_path
    raise "Not in a Rails application directory"
  end

  results = []

  # Generate Service
  results << generate_service(base_path, feature_name)

  # Generate Query
  results << generate_query(base_path, feature_name)

  # Generate Form
  results << generate_form(base_path, feature_name)

  # Generate Presenter
  results << generate_presenter(base_path, feature_name)

  # Generate Policy
  results << generate_policy(base_path, feature_name)

  # Generate Serializer
  results << generate_serializer(base_path, feature_name)

  if with_spec
    results << generate_service_spec(base_path, feature_name)
    results << generate_query_spec(base_path, feature_name)
    results << generate_form_spec(base_path, feature_name)
    results << generate_presenter_spec(base_path, feature_name)
    results << generate_policy_spec(base_path, feature_name)
    results << generate_serializer_spec(base_path, feature_name)
  end

  "Feature '#{feature_name}' generated successfully with #{results.count} files!"
end

.generate_form(base_path, feature_name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/railsforge/feature_generator.rb', line 90

def self.generate_form(base_path, feature_name)
  form_dir = File.join(base_path, "app", "forms")
  FileUtils.mkdir_p(form_dir)

  file_name = "#{feature_name.underscore}_form.rb"
  file_path = File.join(form_dir, file_name)

  return "  Skipping form (already exists)" if File.exist?(file_path)

  File.write(file_path, form_template(feature_name))
  puts "  Created app/forms/#{file_name}"
  file_path
end

.generate_form_spec(base_path, feature_name) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/railsforge/feature_generator.rb', line 175

def self.generate_form_spec(base_path, feature_name)
  spec_dir = File.join(base_path, "spec", "forms")
  FileUtils.mkdir_p(spec_dir)

  file_name = "#{feature_name.underscore}_form_spec.rb"
  file_path = File.join(spec_dir, file_name)

  return "  Skipping form spec (already exists)" if File.exist?(file_path)

  File.write(file_path, form_spec_template(feature_name))
  puts "  Created spec/forms/#{file_name}"
  file_path
end

.generate_policy(base_path, feature_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/railsforge/feature_generator.rb', line 118

def self.generate_policy(base_path, feature_name)
  policy_dir = File.join(base_path, "app", "policies")
  FileUtils.mkdir_p(policy_dir)

  file_name = "#{feature_name.underscore}_policy.rb"
  file_path = File.join(policy_dir, file_name)

  return "  Skipping policy (already exists)" if File.exist?(file_path)

  File.write(file_path, policy_template(feature_name))
  puts "  Created app/policies/#{file_name}"
  file_path
end

.generate_policy_spec(base_path, feature_name) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/railsforge/feature_generator.rb', line 203

def self.generate_policy_spec(base_path, feature_name)
  spec_dir = File.join(base_path, "spec", "policies")
  FileUtils.mkdir_p(spec_dir)

  file_name = "#{feature_name.underscore}_policy_spec.rb"
  file_path = File.join(spec_dir, file_name)

  return "  Skipping policy spec (already exists)" if File.exist?(file_path)

  File.write(file_path, policy_spec_template(feature_name))
  puts "  Created spec/policies/#{file_name}"
  file_path
end

.generate_presenter(base_path, feature_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/railsforge/feature_generator.rb', line 104

def self.generate_presenter(base_path, feature_name)
  presenter_dir = File.join(base_path, "app", "presenters")
  FileUtils.mkdir_p(presenter_dir)

  file_name = "#{feature_name.underscore}_presenter.rb"
  file_path = File.join(presenter_dir, file_name)

  return "  Skipping presenter (already exists)" if File.exist?(file_path)

  File.write(file_path, presenter_template(feature_name))
  puts "  Created app/presenters/#{file_name}"
  file_path
end

.generate_presenter_spec(base_path, feature_name) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/railsforge/feature_generator.rb', line 189

def self.generate_presenter_spec(base_path, feature_name)
  spec_dir = File.join(base_path, "spec", "presenters")
  FileUtils.mkdir_p(spec_dir)

  file_name = "#{feature_name.underscore}_presenter_spec.rb"
  file_path = File.join(spec_dir, file_name)

  return "  Skipping presenter spec (already exists)" if File.exist?(file_path)

  File.write(file_path, presenter_spec_template(feature_name))
  puts "  Created spec/presenters/#{file_name}"
  file_path
end

.generate_query(base_path, feature_name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/railsforge/feature_generator.rb', line 76

def self.generate_query(base_path, feature_name)
  query_dir = File.join(base_path, "app", "queries")
  FileUtils.mkdir_p(query_dir)

  file_name = "find_#{feature_name.underscore}.rb"
  file_path = File.join(query_dir, file_name)

  return "  Skipping query (already exists)" if File.exist?(file_path)

  File.write(file_path, query_template(feature_name))
  puts "  Created app/queries/#{file_name}"
  file_path
end

.generate_query_spec(base_path, feature_name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/railsforge/feature_generator.rb', line 161

def self.generate_query_spec(base_path, feature_name)
  spec_dir = File.join(base_path, "spec", "queries")
  FileUtils.mkdir_p(spec_dir)

  file_name = "find_#{feature_name.underscore}_spec.rb"
  file_path = File.join(spec_dir, file_name)

  return "  Skipping query spec (already exists)" if File.exist?(file_path)

  File.write(file_path, query_spec_template(feature_name))
  puts "  Created spec/queries/#{file_name}"
  file_path
end

.generate_serializer(base_path, feature_name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/railsforge/feature_generator.rb', line 132

def self.generate_serializer(base_path, feature_name)
  serializer_dir = File.join(base_path, "app", "serializers")
  FileUtils.mkdir_p(serializer_dir)

  file_name = "#{feature_name.underscore}_serializer.rb"
  file_path = File.join(serializer_dir, file_name)

  return "  Skipping serializer (already exists)" if File.exist?(file_path)

  File.write(file_path, serializer_template(feature_name))
  puts "  Created app/serializers/#{file_name}"
  file_path
end

.generate_serializer_spec(base_path, feature_name) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/railsforge/feature_generator.rb', line 217

def self.generate_serializer_spec(base_path, feature_name)
  spec_dir = File.join(base_path, "spec", "serializers")
  FileUtils.mkdir_p(spec_dir)

  file_name = "#{feature_name.underscore}_serializer_spec.rb"
  file_path = File.join(spec_dir, file_name)

  return "  Skipping serializer spec (already exists)" if File.exist?(file_path)

  File.write(file_path, serializer_spec_template(feature_name))
  puts "  Created spec/serializers/#{file_name}"
  file_path
end

.generate_service(base_path, feature_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/railsforge/feature_generator.rb', line 62

def self.generate_service(base_path, feature_name)
  service_dir = File.join(base_path, "app", "services")
  FileUtils.mkdir_p(service_dir)

  file_name = "#{feature_name.underscore}_service.rb"
  file_path = File.join(service_dir, file_name)

  return "  Skipping service (already exists)" if File.exist?(file_path)

  File.write(file_path, service_template(feature_name))
  puts "  Created app/services/#{file_name}"
  file_path
end

.generate_service_spec(base_path, feature_name) ⇒ Object

Spec file generators



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/railsforge/feature_generator.rb', line 147

def self.generate_service_spec(base_path, feature_name)
  spec_dir = File.join(base_path, "spec", "services")
  FileUtils.mkdir_p(spec_dir)

  file_name = "#{feature_name.underscore}_service_spec.rb"
  file_path = File.join(spec_dir, file_name)

  return "  Skipping service spec (already exists)" if File.exist?(file_path)

  File.write(file_path, service_spec_template(feature_name))
  puts "  Created spec/services/#{file_name}"
  file_path
end

.policy_spec_template(feature_name) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/railsforge/feature_generator.rb', line 497

def self.policy_spec_template(feature_name)
  <<~RUBY
    require 'rails_helper'

    RSpec.describe #{feature_name}Policy do
      let(:user) { nil }
      let(:record) { #{feature_name}.new }
      subject { described_class.new(user, record) }

      describe '#index?' do
        it 'allows everyone' do
          expect(subject.index?).to be_truthy
        end
      end

      describe '#show?' do
        it 'allows everyone' do
          expect(subject.show?).to be_truthy
        end
      end

      describe '#create?' do
        it 'requires user' do
          expect(subject.create?).to be_falsey
        end

        it 'allows authenticated user' do
          user = User.new
          expect(described_class.new(user, record).create?).to be_truthy
        end
      end

      describe 'Scope' do
        let(:scope) { #{feature_name}.all }
        subject { described_class::Scope.new(user, scope) }

        describe '#resolve' do
          it 'returns all records' do
            expect(subject.resolve).to eq(scope.all)
          end
        end
      end
    end
  RUBY
end

.policy_template(feature_name) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/railsforge/feature_generator.rb', line 345

def self.policy_template(feature_name)
  <<~RUBY
    # Policy class for #{feature_name}
    # Handles authorization logic
    #
    # Usage:
    #   authorize #{feature_name.underscore}
    #   policy_scope(#{feature_name.underscore})
    class #{feature_name}Policy
      attr_reader :user, :record

      def initialize(user, record)
        @user = user
        @record = record
      end

      def index?
        true
      end

      def show?
        true
      end

      def create?
        user.present?
      end

      def new?
        create?
      end

      def update?
        user.present?
      end

      def edit?
        update?
      end

      def destroy?
        user.present?
      end

      class Scope
        def initialize(user, scope)
          @user = user
          @scope = scope
        end

        def resolve
          scope.all
        end

        private

        attr_reader :user, :scope
      end
    end
  RUBY
end

.presenter_spec_template(feature_name) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/railsforge/feature_generator.rb', line 480

def self.presenter_spec_template(feature_name)
  <<~RUBY
    require 'rails_helper'

    RSpec.describe #{feature_name}Presenter do
      let(:#{feature_name.underscore}) { #{feature_name}.new }
      subject { described_class.new(#{feature_name.underscore}) }

      describe 'initialization' do
        it 'initializes successfully' do
          expect(subject).to be_a(described_class)
        end
      end
    end
  RUBY
end

.presenter_template(feature_name) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/railsforge/feature_generator.rb', line 327

def self.presenter_template(feature_name)
  <<~RUBY
    # Presenter class for #{feature_name}
    # Handles view presentation logic
    #
    # Usage:
    #   presenter = #{feature_name}Presenter.new(@#{feature_name.underscore})
    #   presenter.display_name
    class #{feature_name}Presenter
      def initialize(#{feature_name.underscore})
        @#{feature_name.underscore} = #{feature_name.underscore}
      end

      # TODO: Add presenter methods
    end
  RUBY
end

.query_spec_template(feature_name) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/railsforge/feature_generator.rb', line 440

def self.query_spec_template(feature_name)
  <<~RUBY
    require 'rails_helper'

    RSpec.describe Find#{feature_name} do
      let(:scope) { #{feature_name}.all }
      subject { described_class.new(scope: scope) }

      describe '#call' do
        it 'returns scope' do
          expect(subject.call).to eq(scope)
        end
      end
    end
  RUBY
end

.query_template(feature_name) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/railsforge/feature_generator.rb', line 273

def self.query_template(feature_name)
  <<~RUBY
    # Query class for #{feature_name}
    # Encapsulates database queries
    #
    # Usage:
    #   result = #{feature_name}Query.call
    #   result = #{feature_name}Query.call(scope: #{feature_name}.active)
    class Find#{feature_name}
      def initialize(scope: nil)
        @scope = scope || #{feature_name}.all
      end

      def call
        # TODO: Implement query logic
        @scope
      end
    end
  RUBY
end

.serializer_spec_template(feature_name) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/railsforge/feature_generator.rb', line 543

def self.serializer_spec_template(feature_name)
  <<~RUBY
    require 'rails_helper'

    RSpec.describe #{feature_name}Serializer do
      let(:#{feature_name.underscore}) { #{feature_name}.new }
      subject { described_class.new(#{feature_name.underscore}) }

      describe 'serialization' do
        it 'includes id' do
          expect(subject.as_json[:id]).to eq(#{feature_name.underscore}.id)
        end
      end
    end
  RUBY
end

.serializer_template(feature_name) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/railsforge/feature_generator.rb', line 407

def self.serializer_template(feature_name)
  <<~RUBY
    # Serializer class for #{feature_name}
    # Handles JSON serialization
    #
    # Usage:
    #   render json: #{feature_name}Serializer.new(#{feature_name.underscore})
    class #{feature_name}Serializer < ApplicationSerializer
      attributes :id

      # TODO: Add custom methods
    end
  RUBY
end

.service_spec_template(feature_name) ⇒ Object

Spec templates



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/railsforge/feature_generator.rb', line 423

def self.service_spec_template(feature_name)
  <<~RUBY
    require 'rails_helper'

    RSpec.describe #{feature_name}Service do
      let(:params) { {} }
      subject { described_class.new(params) }

      describe '#call' do
        it 'returns successful result' do
          expect(subject.call).to be_truthy
        end
      end
    end
  RUBY
end

.service_template(feature_name) ⇒ Object

Templates



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/railsforge/feature_generator.rb', line 243

def self.service_template(feature_name)
  <<~RUBY
    # Service class for #{feature_name}
    # Encapsulates business logic
    #
    # Usage:
    #   result = #{feature_name}Service.call(params)
    #
    # @example Basic service
    #   class Create#{feature_name}Service
    #     def initialize(#{feature_name.underscore}_params)
    #       @#{feature_name.underscore}_params = #{feature_name.underscore}_params
    #     end
    #
    #     def call
    #       #{feature_name}.create!(@#{feature_name.underscore}_params)
    #     end
    #   end
    class #{feature_name}Service
      def initialize(#{feature_name.underscore}_params = {})
        @params = #{feature_name.underscore}_params
      end

      def call
        # TODO: Implement service logic
      end
    end
  RUBY
end

.validate_feature_name(name) ⇒ Object

Validates the feature name to ensure it’s valid Ruby class name



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/railsforge/feature_generator.rb', line 8

def self.validate_feature_name(name)
  if name.nil? || name.strip.empty?
    raise InvalidFeatureNameError, "Feature name cannot be empty"
  end

  # Don't validate names starting with dashes (they're likely flags)
  return if name.start_with?("-")

  unless name =~ /\A[A-Z][a-zA-Z0-9]*\z/
    raise InvalidFeatureNameError, "Feature name must be in PascalCase (e.g., User)"
  end
end