Class: ActiveOutbox::Generators::ModelGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
AdapterHelper, Rails::Generators::Migration
Defined in:
lib/generators/active_outbox/model/model_generator.rb

Instance Method Summary collapse

Methods included from AdapterHelper

bigint_type, json_type, mysql?, postgres?, uuid_type

Instance Method Details

#aggregate_identifier_typeObject



69
70
71
# File 'lib/generators/active_outbox/model/model_generator.rb', line 69

def aggregate_identifier_type
  options['uuid'].present? ? ActiveOutbox::AdapterHelper.uuid_type : ActiveOutbox::AdapterHelper.bigint_type
end

#create_migration_fileObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/active_outbox/model/model_generator.rb', line 31

def create_migration_file
  migration_path = "#{root_path}/db/migrate"
  migration_template(
    'migration.rb',
    "#{migration_path}/active_outbox_create_#{table_name}.rb",
    migration_version: migration_version
  )

  template('model.rb', "#{root_path}/app/models/#{path_name}.rb")
end

#migration_versionObject



47
48
49
# File 'lib/generators/active_outbox/model/model_generator.rb', line 47

def migration_version
  "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end

#path_nameObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/active_outbox/model/model_generator.rb', line 58

def path_name
  name = ''
  *namespace = model_name.downcase.split('/')
  if (model_name.include?('/') && model_name.last != '/' && namespace.length > 1) || !model_name.include?('/')
    name = namespace.pop
  end
  name = name.blank? ? 'outbox' : "#{name}_outbox"
  namespace = namespace.join('/')
  namespace.blank? ? name : "#{namespace}/#{name}"
end

#root_pathObject



42
43
44
45
# File 'lib/generators/active_outbox/model/model_generator.rb', line 42

def root_path
  path = options['component_path'].blank? ? '' : "/#{options['component_path']}"
  "#{Rails.root}#{path}"
end

#table_nameObject



51
52
53
54
55
56
# File 'lib/generators/active_outbox/model/model_generator.rb', line 51

def table_name
  *namespace, name = model_name.downcase.split('/')
  name = name.blank? ? 'outboxes' : "#{name}_outboxes"
  namespace = namespace.join('_')
  namespace.blank? ? name : "#{namespace}_#{name}"
end