Class: EventSourcedRecord::EventGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/event_sourced_record/event_generator.rb

Instance Method Summary collapse

Instance Method Details

#attributes_with_indexObject



34
35
36
# File 'lib/generators/event_sourced_record/event_generator.rb', line 34

def attributes_with_index
  attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
end

#create_migration_fileObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/generators/event_sourced_record/event_generator.rb', line 8

def create_migration_file
  ar_major_version = ActiveRecord::VERSION::MAJOR
  if ar_major_version >= 4
    attributes_str = attributes.map { |attr|
      attr_banner = attr.name
      attr_banner << ":#{attr.type}" if attr.type
      attr_banner << ':index' if attr.has_index?
      attr_banner
    }.join(' ')
    generate(
      "migration", "create_#{event_table_name} #{attributes_str}"
    )
  else
    migration_template(
      "event_migration.ar3.rb", "db/migrate/create_#{event_table_name}.rb"
    )
  end
end

#create_model_fileObject



27
28
29
30
31
32
# File 'lib/generators/event_sourced_record/event_generator.rb', line 27

def create_model_file
  template(
    'event_model.rb', 
    File.join('app/models', class_path, "#{event_file_name}.rb")
  )
end