Class: CommonGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- CommonGenerator
- 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
- #add_locale_entries ⇒ Object
- #add_routes ⇒ Object
- #create_controller ⇒ Object
- #create_request_spec ⇒ Object
- #create_serializer ⇒ Object
- #update_model_with_ransackable_methods ⇒ Object
- #validate_generator_options ⇒ Object
- #validate_options! ⇒ Object
Instance Method Details
#add_locale_entries ⇒ Object
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_routes ⇒ Object
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_controller ⇒ Object
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_spec ⇒ Object
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_serializer ⇒ Object
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_methods ⇒ Object
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_options ⇒ Object
24 25 26 |
# File 'lib/generators/common/common_generator.rb', line 24 def end |
#validate_options! ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/generators/common/common_generator.rb', line 28 def invalid_only = [:only] - VALID_COMPONENTS invalid_skip = [: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 |