Class: PgRls::Generators::ActiveRecordGenerator

Inherits:
ActiveRecord::Generators::ModelGenerator
  • Object
show all
Includes:
Base
Defined in:
lib/generators/pg_rls/active_record/active_record_generator.rb

Overview

Active Record Generator

Instance Method Summary collapse

Instance Method Details

#check_class_collisionObject



14
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 14

def check_class_collision; end

#convert_file_sub_nameObject



88
89
90
91
92
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 88

def convert_file_sub_name
  return 'pg_rls_convert_tenant' if installation_in_progress?

  'pg_rls_convert'
end

#convert_migration_template_pathObject



70
71
72
73
74
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 70

def convert_migration_template_path
  return 'init_convert_migration.rb.tt' if installation_in_progress?

  'convert_migration.rb.tt'
end

#convert_tenant_migration_fileObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 30

def convert_tenant_migration_file
  unless creating?
    migration_template(convert_migration_template_path,
                       "#{migration_path}/#{convert_file_sub_name}_#{table_name}.rb",
                       migration_version:)
  end

  if installation_in_progress?
    migration_template('convert_migration_backport.rb.tt',
                       "#{migration_path}/pg_rls_backport_#{table_name}.rb",
                       migration_version:)
  end
end

#create_file_sub_nameObject



82
83
84
85
86
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 82

def create_file_sub_name
  return 'pg_rls_create_tenant' if installation_in_progress?

  'pg_rls_create'
end

#create_migration_fileObject



16
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 16

def create_migration_file; end

#create_migration_template_pathObject



64
65
66
67
68
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 64

def create_migration_template_path
  return 'init_migration.rb.tt' if installation_in_progress?

  'migration.rb.tt'
end

#create_model_fileObject



44
45
46
47
48
49
50
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 44

def create_model_file
  return if migration_exist?

  generate_abstract_class if database && !parent

  template model_template_path, model_file
end

#create_tenant_migration_fileObject



22
23
24
25
26
27
28
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 22

def create_tenant_migration_file
  if creating?
    migration_template(create_migration_template_path,
                       "#{migration_path}/#{create_file_sub_name}_#{table_name}.rb",
                       migration_version:)
  end
end

#creating?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 106

def creating?
  @creating ||= !migration_exist?
end

#inject_method_to_modelObject



52
53
54
55
56
57
58
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 52

def inject_method_to_model
  return unless installation_in_progress?

  gsub_file(model_file, /Class #{class_name} < #{parent_class_name.classify}/mi) do |match|
    "#{match}\n  def self.current\n    PgRls::Tenant.fetch\n  end\n"
  end
end

#installation_in_progress?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 94

def installation_in_progress?
  shell.base.class.name.include?('Install')
end

#migration_exist?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 18

def migration_exist?
  @migration_exist ||= Dir.glob("#{migration_path}/*create_#{table_name}.rb").present?
end

#migration_pathObject



102
103
104
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 102

def migration_path
  db_migrate_path
end

#migration_versionObject



98
99
100
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 98

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

#model_fileObject



60
61
62
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 60

def model_file
  File.join('app/models', class_path, "#{file_name}.rb")
end

#model_template_pathObject



76
77
78
79
80
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 76

def model_template_path
  return 'init_model.rb.tt' if installation_in_progress?

  'model.rb.tt'
end