Generator commands delegate Rails::Generator::Base and implement a standard set of actions. Their behavior is defined by the way they respond to these actions: Create brings life; Destroy brings death; List passively observes.
Commands are invoked by replaying (or rewinding) the generator‘s manifest of actions. See Rails::Generator::Manifest and Rails::Generator::Base#manifest method that generator subclasses are required to override.
Commands allows generators to "plug in" invocation behavior, which corresponds to the GoF Strategy pattern.
- class_collisions
- current_migration_number
- dependency
- existing_migrations
- gsub_file
- invoke!
- migration_directory
- migration_exists?
- next_migration_number
- next_migration_string
- readme
Does nothing for all commands except Create.
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 53
53: def class_collisions(*class_names)
54: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 46
46: def dependency(generator_name, args, runtime_options = {})
47: logger.dependency(generator_name) do
48: self.class.new(instance(generator_name, args, full_options(runtime_options))).invoke!
49: end
50: end
Replay action manifest. RewindBase subclass rewinds manifest.
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 41
41: def invoke!
42: manifest.replay(self)
43: after_generate
44: end
Does nothing for all commands except Create.
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 57
57: def readme(*args)
58: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 61
61: def current_migration_number
62: Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path|
63: n = File.basename(file_path).split('_', 2).first.to_i
64: if n > max then n else max end
65: end
66: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 76
76: def existing_migrations(file_name)
77: Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/)
78: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 92
92: def gsub_file(relative_destination, regexp, *args, &block)
93: path = destination_path(relative_destination)
94: content = File.read(path).gsub(regexp, *args, &block)
95: File.open(path, 'wb') { |file| file.write(content) }
96: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 72
72: def migration_directory(relative_path)
73: directory(@migration_directory = relative_path)
74: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 80
80: def migration_exists?(file_name)
81: not existing_migrations(file_name).empty?
82: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 68
68: def next_migration_number
69: current_migration_number + 1
70: end
[ show source ]
# File railties/lib/rails_generator/commands.rb, line 84
84: def next_migration_string(padding = 3)
85: if ActiveRecord::Base.timestamped_migrations
86: Time.now.utc.strftime("%Y%m%d%H%M%S")
87: else
88: "%.#{padding}d" % next_migration_number
89: end
90: end