Class: ActiveRecord::Generators::EventGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/active_record/event/event_generator.rb

Constant Summary collapse

MACRO_OPTIONS =
%w[object field_type skip_scopes].freeze

Instance Method Summary collapse

Instance Method Details

#generate_migration_fileObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/active_record/event/event_generator.rb', line 23

def generate_migration_file
  naming = ActiveRecord::Events::Naming.new(event_name, options)

  table_name = model_name.tableize
  field_name = naming.field
  field_type = options[:field_type] || 'datetime'

  migration_name = "add_#{field_name}_to_#{table_name}"
  attributes = "#{field_name}:#{field_type}"

  invoke 'active_record:migration', [migration_name, attributes]
end

#update_model_fileObject



36
37
38
39
40
41
42
43
# File 'lib/generators/active_record/event/event_generator.rb', line 36

def update_model_file
  return unless model_file_exists?

  macro_options = options.slice(*MACRO_OPTIONS)
  macro = ActiveRecord::Events::Macro.new(event_name, macro_options)

  inject_into_file model_file_path, "\s\s#{macro}\n", after: /class.+\n/
end