Class: DryModuleGenerator::Installer
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- DryModuleGenerator::Installer
- Defined in:
- lib/dry_module_generator/install/installer.rb
Instance Method Summary collapse
- #create_application_service ⇒ Object
- #create_constraint_error ⇒ Object
- #create_initializers ⇒ Object
- #create_javascripts ⇒ Object
- #create_utils ⇒ Object
- #update_application ⇒ Object
- #update_application_controller ⇒ Object
- #update_application_helper ⇒ Object
- #update_application_record ⇒ Object
Instance Method Details
#create_application_service ⇒ Object
58 59 60 |
# File 'lib/dry_module_generator/install/installer.rb', line 58 def create_application_service template("services/application_service.rb", File.join("app/services/application_service.rb")) end |
#create_constraint_error ⇒ Object
62 63 64 |
# File 'lib/dry_module_generator/install/installer.rb', line 62 def create_constraint_error template("errors/constraint_error.rb", File.join("app/errors/constraint_error.rb")) end |
#create_initializers ⇒ Object
66 67 68 69 70 71 |
# File 'lib/dry_module_generator/install/installer.rb', line 66 def create_initializers template("initializers/container.rb", File.join("config/initializers/container.rb")) template("initializers/dependency_injection.rb", File.join("config/initializers/dependency_injection.rb")) template("initializers/dry_struct_generator.rb", File.join("config/initializers/dry_struct_generator.rb")) template("initializers/routes.rb", File.join("config/initializers/routes.rb")) end |
#create_javascripts ⇒ Object
141 142 143 |
# File 'lib/dry_module_generator/install/installer.rb', line 141 def create_javascripts template("javascript/controllers/form_controller.js", File.join("app/javascript/controllers/form_controller.js")) end |
#create_utils ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dry_module_generator/install/installer.rb', line 46 def create_utils template("utils/contract_validator.rb", File.join("lib/utils/contract_validator.rb")) template("utils/types.rb", File.join("lib/utils/types.rb")) template("utils/application_contract.rb", File.join("lib/utils/application_contract.rb")) template("utils/application_struct.rb", File.join("lib/utils/application_struct.rb")) template("utils/application_read_struct.rb", File.join("lib/utils/application_read_struct.rb")) template( "utils/injection/controller_resolve_strategy.rb", File.join("lib/utils/injection/controller_resolve_strategy.rb") ) end |
#update_application ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/dry_module_generator/install/installer.rb', line 73 def update_application inject_into_file "config/application.rb" do " Dir[File.join(Rails.root, '*', 'lib', '*', 'infra', 'config', 'application.rb')].each do |file| require File.join(File.dirname(file), File.basename(file, File.extname(file))) end " end end |
#update_application_controller ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/dry_module_generator/install/installer.rb', line 83 def update_application_controller file_path = "app/controllers/application_controller.rb" file_content = File.read(file_path) return if file_content.include?("ConstraintError") inject_into_class file_path, "ApplicationController" do " include Import.inject[validator: 'contract_validator'] rescue_from(ConstraintError) do |e| @form = e.validator if action_name == 'create' render :new, status: :unprocessable_entity elsif action_name == 'update' render :edit, status: :unprocessable_entity end end " end end |
#update_application_helper ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/dry_module_generator/install/installer.rb', line 105 def update_application_helper file_path = "app/helpers/application_helper.rb" file_content = File.read(file_path) return if file_content.include?("def show_error") inject_into_module file_path, "ApplicationHelper" do <<-"CODE" def show_error(validator, keys) return unless validator.errors keys = [keys] unless keys.is_a?(Array) field_name = keys.last if validator.errors.any? result = find_value(validator.errors, keys) return "\#{field_name.to_s.humanize} \#{result.join(', ')}" unless result.blank? end end def find_value(hash, keys) result = hash keys.each do |key| if result.is_a?(Hash) && result.key?(key) result = result[key] elsif result.is_a?(Array) result = result.map { |item| item.is_a?(Hash) ? item[key] : nil }.compact result = result.first if result.length == 1 # If there's only one element in the array else return nil end end result end CODE end end |
#update_application_record ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dry_module_generator/install/installer.rb', line 8 def update_application_record file_path = "app/models/application_record.rb" class_name = "ApplicationRecord" # Read the existing content of the file file_content = File.read(file_path) # Define the code you want to add additional_code = " def self.save(record) record.tap(&:save) end def self.save!(record) record.tap(&:save!) end def self.delete!(record) record.tap(&:destroy!) end" # Check if the class is present in the file if file_content.include?("class #{class_name}") && !file_content.include?("def self.save!") # If the class is present, find the end of the class definition and add the code there class_definition_end = file_content.index("end", file_content.index("class #{class_name}")) if class_definition_end inject_code_position = class_definition_end - 1 file_content.insert(inject_code_position, additional_code) File.write(file_path, file_content) else puts "Error: Unable to find the end of the class definition in #{file_path}." end else puts "Error: Unable to find the class definition for #{class_name} in #{file_path}." end end |