class ActiveRecord::Generators::EventGenerator

Constants

MACRO_OPTIONS

Public Instance Methods

generate_migration_file() click to toggle source
# File lib/generators/active_record/event/event_generator.rb, line 25
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_file() click to toggle source
# File lib/generators/active_record/event/event_generator.rb, line 38
def update_model_file
  return unless model_file_exists?

  macro_options = options.slice(*MACRO_OPTIONS)
  macro = ActiveRecord::Events::Macro.new(event_name, macro_options)
  pattern = /^\s*class\s.+\n/

  inject_into_file model_file_path, "\s\s#{macro}\n", after: pattern
end

Private Instance Methods

model_file_exists?() click to toggle source
# File lib/generators/active_record/event/event_generator.rb, line 50
def model_file_exists?
  File.exist?(model_file_path)
end
model_file_name() click to toggle source
# File lib/generators/active_record/event/event_generator.rb, line 58
def model_file_name
  "#{model_name.underscore.singularize}.rb"
end
model_file_path() click to toggle source
# File lib/generators/active_record/event/event_generator.rb, line 54
def model_file_path
  File.expand_path("app/models/#{model_file_name}", destination_root)
end