Class: CommonGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/common/common_generator.rb

Constant Summary collapse

VALID_COMPONENTS =
%w[
  controller
  serializer
  request_spec
  routes
  locales
  model
].freeze

Instance Method Summary collapse

Instance Method Details

#add_locale_entriesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/generators/common/common_generator.rb', line 101

def add_locale_entries
  return unless should_generate?("locales")

  locale_entries = generate_locale_entries
  indented_entries = indent_locale_block(locale_entries, 2)

  if File.exist?(locale_file_path)
    model_key = model_class_name.underscore
    append_to_file locale_file_path, "\n\n#{indented_entries}" unless File.read(locale_file_path).include?("#{model_key}:")
  else
    create_file locale_file_path, <<~LOCALE
        en:
      #{indented_entries}
    LOCALE
  end
end

#add_routesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/common/common_generator.rb', line 73

def add_routes
  return unless should_generate?("routes")

  route_definition = "  resources :#{route_name}"

  if File.exist?(routes_file_path)
    # Check if route already exists
    unless File.read(routes_file_path).include?("resources :#{route_name}")
      # Try to inject after engine routes draw or Rails routes draw
      routes_content = File.read(routes_file_path)
      if routes_content.include?("Engine.routes.draw do")
        inject_into_file routes_file_path, "#{route_definition}\n", after: /\.routes\.draw do\n/
      elsif routes_content.include?("Rails.application.routes.draw do")
        inject_into_file routes_file_path, "#{route_definition}\n", after: /Rails\.application\.routes\.draw do\n/
      else
        # Fallback: append to the end before the last 'end'
        inject_into_file routes_file_path, "#{route_definition}\n", before: /^end\s*$/
      end
    end
  else
    create_file routes_file_path, <<~ROUTES
      Rails.application.routes.draw do
      #{route_definition}
      end
    ROUTES
  end
end

#create_controllerObject



43
44
45
46
47
# File 'lib/generators/common/common_generator.rb', line 43

def create_controller
  return unless should_generate?("controller")

  template "controller.rb.erb", controller_file_path
end

#create_request_specObject



59
60
61
62
63
# File 'lib/generators/common/common_generator.rb', line 59

def create_request_spec
  return unless should_generate?("request_spec")

  template "request_spec.rb.erb", request_spec_file_path
end

#create_serializerObject



49
50
51
52
53
54
55
56
57
# File 'lib/generators/common/common_generator.rb', line 49

def create_serializer
  return unless should_generate?("serializer")
  return unless model_exists?

  # Only check for missing serializers when creating (not destroying) and not in recursive generation
  check_and_prompt_for_missing_serializers unless behavior == :revoke || recursive_generation?

  template "serializer.rb.erb", serializer_file_path
end

#update_model_with_ransackable_methodsObject



65
66
67
68
69
70
71
# File 'lib/generators/common/common_generator.rb', line 65

def update_model_with_ransackable_methods
  return unless should_generate?("model")
  return unless model_exists?
  return unless model_file_exists?

  add_ransackable_methods_to_model
end

#validate_generator_optionsObject



24
25
26
# File 'lib/generators/common/common_generator.rb', line 24

def validate_generator_options
  validate_options!
end

#validate_options!Object

Raises:

  • (Thor::Error)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/common/common_generator.rb', line 28

def validate_options!
  invalid_only = options[:only] - VALID_COMPONENTS
  invalid_skip = options[:skip] - VALID_COMPONENTS

  if invalid_only.any?
    raise Thor::Error,
          "Invalid values for --only: #{invalid_only.join(', ')}. Allowed values: #{VALID_COMPONENTS.join(', ')}"
  end

  return unless invalid_skip.any?

  raise Thor::Error,
        "Invalid values for --skip: #{invalid_skip.join(', ')}. Allowed values: #{VALID_COMPONENTS.join(', ')}"
end