Class: PgRls::Generators::ActiveRecordGenerator
- Inherits:
-
ActiveRecord::Generators::ModelGenerator
- Object
- ActiveRecord::Generators::ModelGenerator
- PgRls::Generators::ActiveRecordGenerator
- Includes:
- Base
- Defined in:
- lib/generators/pg_rls/active_record/active_record_generator.rb
Overview
Active Record Generator
Instance Method Summary collapse
- #check_class_collision ⇒ Object
- #convert_file_sub_name ⇒ Object
- #convert_migration_template_path ⇒ Object
- #convert_tenant_migration_file ⇒ Object
- #create_file_sub_name ⇒ Object
- #create_migration_file ⇒ Object
- #create_migration_template_path ⇒ Object
- #create_model_file ⇒ Object
- #create_tenant_migration_file ⇒ Object
- #creating? ⇒ Boolean
- #inject_method_to_model ⇒ Object
- #installation_in_progress? ⇒ Boolean
- #migration_exist? ⇒ Boolean
- #migration_path ⇒ Object
- #migration_version ⇒ Object
- #model_file ⇒ Object
- #model_template_path ⇒ Object
Instance Method Details
#check_class_collision ⇒ Object
14 |
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 14 def check_class_collision; end |
#convert_file_sub_name ⇒ Object
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_path ⇒ Object
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_file ⇒ Object
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 return unless installation_in_progress? migration_template('convert_migration_backport.rb.tt', "#{migration_path}/pg_rls_backport_#{table_name}.rb", migration_version:) end |
#create_file_sub_name ⇒ Object
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_file ⇒ Object
16 |
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 16 def create_migration_file; end |
#create_migration_template_path ⇒ Object
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_file ⇒ Object
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_file ⇒ Object
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 return unless creating? migration_template(create_migration_template_path, "#{migration_path}/#{create_file_sub_name}_#{table_name}.rb", migration_version:) end |
#creating? ⇒ 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_model ⇒ Object
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
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
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_path ⇒ Object
102 103 104 |
# File 'lib/generators/pg_rls/active_record/active_record_generator.rb', line 102 def migration_path db_migrate_path end |
#migration_version ⇒ Object
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_file ⇒ Object
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_path ⇒ Object
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 |