activerecord-dbt 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +213 -72
  3. data/lib/active_record/dbt/column/{test.rb → data_test.rb} +16 -16
  4. data/lib/active_record/dbt/column/{testable/accepted_values_testable.rb → data_testable/accepted_values_data_testable.rb} +2 -2
  5. data/lib/active_record/dbt/column/{testable/not_null_testable.rb → data_testable/not_null_data_testable.rb} +9 -3
  6. data/lib/active_record/dbt/column/{testable/relationships_testable.rb → data_testable/relationships_data_testable.rb} +3 -3
  7. data/lib/active_record/dbt/column/{testable/unique_testable.rb → data_testable/unique_data_testable.rb} +2 -2
  8. data/lib/active_record/dbt/column/{column.rb → yml.rb} +9 -20
  9. data/lib/active_record/dbt/config.rb +2 -30
  10. data/lib/active_record/dbt/configuration/i18n_configuration.rb +14 -0
  11. data/lib/active_record/dbt/configuration/source.rb +40 -0
  12. data/lib/active_record/dbt/data_type/mapper.rb +81 -14
  13. data/lib/active_record/dbt/dbt_package/dbt_utils/table/{testable/unique_combination_of_columns_testable.rb → data_testable/unique_combination_of_columns_data_testable.rb} +2 -2
  14. data/lib/active_record/dbt/dbt_package/dbterd/column/{testable → data_testable}/relationships_meta_relationship_type.rb +4 -5
  15. data/lib/active_record/dbt/factory/columns/yml_factory.rb +33 -0
  16. data/lib/active_record/dbt/factory/model/staging/yml_factory.rb +24 -0
  17. data/lib/active_record/dbt/factory/source/yml_factory.rb +17 -0
  18. data/lib/active_record/dbt/factory/table/yml_factory.rb +18 -0
  19. data/lib/active_record/dbt/factory/tables/yml_factory.rb +17 -0
  20. data/lib/active_record/dbt/i18n_wrapper/translate.rb +34 -0
  21. data/lib/active_record/dbt/model/staging/base.rb +1 -1
  22. data/lib/active_record/dbt/model/staging/yml.rb +10 -6
  23. data/lib/active_record/dbt/seed/enum/base.rb +48 -0
  24. data/lib/active_record/dbt/seed/enum/csv.rb +85 -0
  25. data/lib/active_record/dbt/seed/enum/yml.rb +128 -0
  26. data/lib/active_record/dbt/source/yml.rb +13 -5
  27. data/lib/active_record/dbt/table/{test.rb → data_test.rb} +3 -3
  28. data/lib/active_record/dbt/table/yml.rb +7 -10
  29. data/lib/active_record/dbt/version.rb +1 -1
  30. data/lib/generators/active_record/dbt/config/templates/source_config.yml.tt +5 -2
  31. data/lib/generators/active_record/dbt/enum/USAGE +9 -0
  32. data/lib/generators/active_record/dbt/enum/enum_generator.rb +31 -0
  33. data/lib/generators/active_record/dbt/initializer/templates/dbt.rb +9 -7
  34. data/lib/generators/active_record/dbt/source/USAGE +1 -1
  35. data/lib/generators/active_record/dbt/source/source_generator.rb +3 -4
  36. data/lib/generators/active_record/dbt/staging_model/USAGE +2 -2
  37. data/lib/generators/active_record/dbt/staging_model/staging_model_generator.rb +2 -2
  38. metadata +24 -21
  39. data/lib/active_record/dbt/data_type/dwh_platform/apache_spark.rb +0 -27
  40. data/lib/active_record/dbt/data_type/dwh_platform/bigquery.rb +0 -26
  41. data/lib/active_record/dbt/data_type/dwh_platform/postgre_sql.rb +0 -27
  42. data/lib/active_record/dbt/data_type/dwh_platform/redshift.rb +0 -27
  43. data/lib/active_record/dbt/data_type/dwh_platform/snowflake.rb +0 -27
  44. data/lib/active_record/dbt/factory/columns_factory.rb +0 -31
  45. data/lib/active_record/dbt/factory/model/staging_factory.rb +0 -22
  46. data/lib/active_record/dbt/factory/source_factory.rb +0 -16
  47. data/lib/active_record/dbt/factory/table_factory.rb +0 -16
  48. data/lib/active_record/dbt/factory/tables_factory.rb +0 -15
@@ -3,11 +3,11 @@
3
3
  module ActiveRecord
4
4
  module Dbt
5
5
  module Column
6
- class Test
7
- include ActiveRecord::Dbt::Column::Testable::AcceptedValuesTestable
8
- include ActiveRecord::Dbt::Column::Testable::NotNullTestable
9
- include ActiveRecord::Dbt::Column::Testable::RelationshipsTestable
10
- include ActiveRecord::Dbt::Column::Testable::UniqueTestable
6
+ class DataTest
7
+ include ActiveRecord::Dbt::Column::DataTestable::AcceptedValuesDataTestable
8
+ include ActiveRecord::Dbt::Column::DataTestable::NotNullDataTestable
9
+ include ActiveRecord::Dbt::Column::DataTestable::RelationshipsDataTestable
10
+ include ActiveRecord::Dbt::Column::DataTestable::UniqueDataTestable
11
11
 
12
12
  attr_reader :table_name, :column, :primary_keys, :foreign_keys
13
13
 
@@ -22,15 +22,15 @@ module ActiveRecord
22
22
  @config = ActiveRecord::Dbt::Config.instance
23
23
  end
24
24
 
25
- def config
26
- (tests.keys | tests_overrides_hash.keys).map do |key|
27
- tests_overrides_hash[key] || tests[key]
25
+ def properties
26
+ (data_tests.keys | data_tests_overrides_hash.keys).map do |key|
27
+ data_tests_overrides_hash[key] || data_tests[key]
28
28
  end.presence
29
29
  end
30
30
 
31
31
  private
32
32
 
33
- def tests
33
+ def data_tests
34
34
  {
35
35
  'unique_test' => unique_test,
36
36
  'not_null_test' => not_null_test,
@@ -39,10 +39,10 @@ module ActiveRecord
39
39
  }.compact
40
40
  end
41
41
 
42
- def tests_overrides_hash
43
- @tests_overrides_hash ||=
44
- tests_overrides.index_by do |tests_override|
45
- "#{extract_key(tests_override)}_test"
42
+ def data_tests_overrides_hash
43
+ @data_tests_overrides_hash ||=
44
+ data_tests_overrides.index_by do |data_tests_override|
45
+ "#{extract_key(data_tests_override)}_test"
46
46
  end
47
47
  end
48
48
 
@@ -50,9 +50,9 @@ module ActiveRecord
50
50
  item.is_a?(Hash) ? item.keys.first : item
51
51
  end
52
52
 
53
- def tests_overrides
54
- @tests_overrides ||=
55
- source_config.dig(:table_overrides, table_name, :columns, column_name, :tests) ||
53
+ def data_tests_overrides
54
+ @data_tests_overrides ||=
55
+ source_config.dig(:table_overrides, table_name, :columns, column_name, :data_tests) ||
56
56
  []
57
57
  end
58
58
  end
@@ -3,8 +3,8 @@
3
3
  module ActiveRecord
4
4
  module Dbt
5
5
  module Column
6
- module Testable
7
- module AcceptedValuesTestable
6
+ module DataTestable
7
+ module AcceptedValuesDataTestable
8
8
  extend ActiveRecord::Dbt::RequiredMethods
9
9
 
10
10
  define_required_methods :@config, :column, :table_name, :column_name
@@ -3,14 +3,20 @@
3
3
  module ActiveRecord
4
4
  module Dbt
5
5
  module Column
6
- module Testable
7
- module NotNullTestable
6
+ module DataTestable
7
+ module NotNullDataTestable
8
8
  extend ActiveRecord::Dbt::RequiredMethods
9
9
 
10
10
  define_required_methods :column
11
11
 
12
12
  def not_null_test
13
- column.null == true ? nil : 'not_null'
13
+ null? ? nil : 'not_null'
14
+ end
15
+
16
+ private
17
+
18
+ def null?
19
+ column.null == true
14
20
  end
15
21
  end
16
22
  end
@@ -3,9 +3,9 @@
3
3
  module ActiveRecord
4
4
  module Dbt
5
5
  module Column
6
- module Testable
7
- module RelationshipsTestable
8
- include ActiveRecord::Dbt::DbtPackage::Dbterd::Column::Testable::RelationshipsMetaRelationshipType
6
+ module DataTestable
7
+ module RelationshipsDataTestable
8
+ include ActiveRecord::Dbt::DbtPackage::Dbterd::Column::DataTestable::RelationshipsMetaRelationshipType
9
9
 
10
10
  extend ActiveRecord::Dbt::RequiredMethods
11
11
 
@@ -3,8 +3,8 @@
3
3
  module ActiveRecord
4
4
  module Dbt
5
5
  module Column
6
- module Testable
7
- module UniqueTestable
6
+ module DataTestable
7
+ module UniqueDataTestable
8
8
  extend ActiveRecord::Dbt::RequiredMethods
9
9
 
10
10
  define_required_methods :table_name, :column_name, :primary_keys
@@ -3,29 +3,30 @@
3
3
  module ActiveRecord
4
4
  module Dbt
5
5
  module Column
6
- class Column
6
+ class Yml
7
7
  include ActiveRecord::Dbt::DataType::Mapper
8
+ include ActiveRecord::Dbt::I18nWrapper::Translate
8
9
 
9
- attr_reader :table_name, :column, :column_test, :primary_keys
10
+ attr_reader :table_name, :column, :column_data_test, :primary_keys
10
11
 
11
12
  delegate :name, :comment, to: :column, prefix: true
12
13
  delegate :source_config, to: :@config
13
14
 
14
- def initialize(table_name, column, column_test, primary_keys: [])
15
+ def initialize(table_name, column, column_data_test, primary_keys: [])
15
16
  @table_name = table_name
16
17
  @column = column
17
- @column_test = column_test
18
+ @column_data_test = column_data_test
18
19
  @primary_keys = primary_keys
19
20
  @config = ActiveRecord::Dbt::Config.instance
20
21
  end
21
22
 
22
- def config
23
+ def properties
23
24
  {
24
25
  'name' => column_name,
25
26
  'description' => description,
26
- 'data_type' => data_type,
27
- **column_overrides.except(:tests),
28
- 'tests' => column_test.config
27
+ 'data_type' => data_type(column.type),
28
+ **column_overrides.except(:data_tests),
29
+ 'data_tests' => column_data_test.properties
29
30
  }.compact
30
31
  end
31
32
 
@@ -45,18 +46,6 @@ module ActiveRecord
45
46
  source_config.dig(:table_descriptions, table_name, :columns, column_name)
46
47
  end
47
48
 
48
- def translated_attribute_name
49
- translated_column_name || translated_default_column_name
50
- end
51
-
52
- def translated_column_name
53
- I18n.t("activerecord.attributes.#{table_name.singularize}.#{column_name}", default: nil)
54
- end
55
-
56
- def translated_default_column_name
57
- I18n.t("attributes.#{column_name}", default: nil)
58
- end
59
-
60
49
  def key_column_name
61
50
  column_name if primary_key? || foreign_key?
62
51
  end
@@ -9,38 +9,10 @@ module ActiveRecord
9
9
 
10
10
  include ActiveRecord::Dbt::Configuration::DataSync
11
11
  include ActiveRecord::Dbt::Configuration::DwhPlatform
12
+ include ActiveRecord::Dbt::Configuration::I18nConfiguration
12
13
  include ActiveRecord::Dbt::Configuration::Logger
13
- include ActiveRecord::Dbt::Configuration::Parser
14
+ include ActiveRecord::Dbt::Configuration::Source
14
15
  include ActiveRecord::Dbt::Configuration::UsedDbtPackage
15
-
16
- DEFAULT_CONFIG_DIRECTORY_PATH = 'lib/dbt'
17
- DEFAULT_EXPORT_DIRECTORY_PATH = 'doc/dbt'
18
-
19
- attr_writer :config_directory_path, :export_directory_path
20
-
21
- def source_config_path
22
- @source_config_path ||= "#{config_directory_path}/source_config.yml"
23
- end
24
-
25
- def source_config
26
- @source_config ||= parse_yaml(source_config_path)
27
- end
28
-
29
- def source_name
30
- @source_name ||= source_config.dig(:sources, :name).tap do |source_name|
31
- raise SourceNameIsNullError, "'sources.name' is required in '#{source_config_path}'." if source_name.nil?
32
- end
33
- end
34
-
35
- def config_directory_path
36
- @config_directory_path ||= DEFAULT_CONFIG_DIRECTORY_PATH
37
- end
38
-
39
- def export_directory_path
40
- @export_directory_path ||= DEFAULT_EXPORT_DIRECTORY_PATH
41
- end
42
-
43
- class SourceNameIsNullError < StandardError; end
44
16
  end
45
17
  end
46
18
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Configuration
6
+ module I18nConfiguration
7
+ def locale=(locale = I18n.locale)
8
+ I18n.load_path += Rails.root.glob('config/locales/**/*.{rb,yml}')
9
+ I18n.locale = locale
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Configuration
6
+ module Source
7
+ include ActiveRecord::Dbt::Configuration::Parser
8
+
9
+ DEFAULT_CONFIG_DIRECTORY_PATH = 'lib/dbt'
10
+ DEFAULT_EXPORT_DIRECTORY_PATH = 'doc/dbt'
11
+
12
+ attr_writer :config_directory_path, :export_directory_path
13
+
14
+ def config_directory_path
15
+ @config_directory_path ||= DEFAULT_CONFIG_DIRECTORY_PATH
16
+ end
17
+
18
+ def export_directory_path
19
+ @export_directory_path ||= DEFAULT_EXPORT_DIRECTORY_PATH
20
+ end
21
+
22
+ def source_config_path
23
+ @source_config_path ||= "#{config_directory_path}/source_config.yml"
24
+ end
25
+
26
+ def source_config
27
+ @source_config ||= parse_yaml(source_config_path)
28
+ end
29
+
30
+ def source_name
31
+ @source_name ||= source_config.dig(:sources, :name).tap do |source_name|
32
+ raise SourceNameIsNullError, "'sources.name' is required in '#{source_config_path}'." if source_name.nil?
33
+ end
34
+ end
35
+
36
+ class SourceNameIsNullError < StandardError; end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -4,31 +4,98 @@ module ActiveRecord
4
4
  module Dbt
5
5
  module DataType
6
6
  module Mapper
7
- include ActiveRecord::Dbt::DataType::DwhPlatform::ApacheSpark
8
- include ActiveRecord::Dbt::DataType::DwhPlatform::Bigquery
9
- include ActiveRecord::Dbt::DataType::DwhPlatform::PostgreSql
10
- include ActiveRecord::Dbt::DataType::DwhPlatform::Redshift
11
- include ActiveRecord::Dbt::DataType::DwhPlatform::Snowflake
12
-
13
7
  extend ActiveRecord::Dbt::RequiredMethods
14
8
 
15
9
  # [Platform-specific data types | dbt Developer Hub](https://docs.getdbt.com/reference/resource-properties/data-types)
16
10
  RUBY_TO_DWH_PLATFORM_TYPE_MAP = {
17
- 'bigquery' => RUBY_TO_BIGQUERY_TYPES,
18
- 'postgres' => RUBY_TO_POSTGRES_TYPES,
19
- 'redshift' => RUBY_TO_REDSHIFT_TYPES,
20
- 'snowflake' => RUBY_TO_SNOWFLAKE_TYPES,
21
- 'spark' => RUBY_TO_SPARK_TYPES
11
+ # [Data types  |  BigQuery  |  Google Cloud](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#data_type_list)
12
+ 'bigquery' => {
13
+ binary: 'bytes',
14
+ boolean: 'bool',
15
+ date: 'date',
16
+ datetime: 'datetime',
17
+ decimal: 'int64',
18
+ float: 'float64',
19
+ integer: 'int64',
20
+ json: 'json',
21
+ string: 'string',
22
+ text: 'string',
23
+ time: 'time'
24
+ },
25
+
26
+ # TODO: I have not tried it. I don't know if this is the correct data_type.
27
+ # [PostgreSQL: Documentation: 16: Chapter 8. Data Types](https://www.postgresql.org/docs/current/datatype.html)
28
+ 'postgres' => {
29
+ binary: 'bytea',
30
+ boolean: 'boolean',
31
+ date: 'date',
32
+ datetime: 'timestamp',
33
+ decimal: 'bigint',
34
+ float: 'double precision',
35
+ integer: 'bigint',
36
+ json: 'json',
37
+ string: 'text',
38
+ text: 'text',
39
+ time: 'time'
40
+ },
41
+
42
+ # TODO: I have not tried it. I don't know if this is the correct data_type.
43
+ # [Data types - Amazon Redshift](https://docs.aws.amazon.com/redshift/latest/dg/c_Supported_data_types.html)
44
+ 'redshift' => {
45
+ binary: 'varbyte',
46
+ boolean: 'bool',
47
+ date: 'date',
48
+ datetime: 'timestamp',
49
+ decimal: 'decimal',
50
+ float: 'double precision',
51
+ integer: 'integer',
52
+ json: 'super',
53
+ string: 'varchar',
54
+ text: 'varchar',
55
+ time: 'time'
56
+ },
57
+
58
+ # TODO: I have not tried it. I don't know if this is the correct data_type.
59
+ # [Summary of data types | Snowflake Documentation](https://docs.snowflake.com/en/sql-reference/intro-summary-data-types)
60
+ 'snowflake' => {
61
+ binary: 'binary',
62
+ boolean: 'boolean',
63
+ date: 'date',
64
+ datetime: 'datetime',
65
+ decimal: 'decimal',
66
+ float: 'float',
67
+ integer: 'integer',
68
+ json: 'variant',
69
+ string: 'varchar',
70
+ text: 'varchar',
71
+ time: 'time'
72
+ },
73
+
74
+ # TODO: I have not tried it. I don't know if this is the correct data_type.
75
+ # [Data Types - Spark 3.5.1 Documentation](https://spark.apache.org/docs/latest/sql-ref-datatypes.html)
76
+ 'spark' => {
77
+ binary: 'binary',
78
+ boolean: 'boolean',
79
+ date: 'date',
80
+ datetime: 'timestamp',
81
+ decimal: 'decimal',
82
+ float: 'float',
83
+ integer: 'integer',
84
+ json: 'string',
85
+ string: 'string',
86
+ text: 'string',
87
+ time: 'string'
88
+ }
22
89
  }.freeze
23
90
 
24
- define_required_methods :column, :@config
91
+ define_required_methods :@config
25
92
 
26
93
  delegate :dwh_platform, to: :@config
27
94
 
28
95
  private
29
96
 
30
- def data_type
31
- RUBY_TO_DWH_PLATFORM_TYPE_MAP[dwh_platform].fetch(column.type, 'unknown')
97
+ def data_type(type)
98
+ RUBY_TO_DWH_PLATFORM_TYPE_MAP[dwh_platform].fetch(type, 'unknown')
32
99
  end
33
100
  end
34
101
  end
@@ -5,8 +5,8 @@ module ActiveRecord
5
5
  module DbtPackage
6
6
  module DbtUtils
7
7
  module Table
8
- module Testable
9
- module UniqueCombinationOfColumnsTestable
8
+ module DataTestable
9
+ module UniqueCombinationOfColumnsDataTestable
10
10
  extend ActiveRecord::Dbt::RequiredMethods
11
11
 
12
12
  define_required_methods :table_name, :@config
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  module DbtPackage
6
6
  module Dbterd
7
7
  module Column
8
- module Testable
8
+ module DataTestable
9
9
  module RelationshipsMetaRelationshipType
10
10
  extend ActiveRecord::Dbt::RequiredMethods
11
11
 
@@ -14,13 +14,12 @@ module ActiveRecord
14
14
  delegate :used_dbterd?, :add_log, to: :@config
15
15
 
16
16
  def relationships_meta_relationship_type
17
- return nil unless used_dbterd?
18
- return nil if no_relationship?
17
+ return nil if !used_dbterd? || no_relationship?
19
18
 
20
19
  {
21
20
  'relationship_type' => relationship_type
22
21
  }
23
- rescue NotSpecifiedOrNotInvalidIdError, StandardError => e
22
+ rescue NotSpecifiedOrNotInvalidIdError, NameError => e
24
23
  relationships_meta_relationship_type_with_active_record_dbt_error(e)
25
24
  end
26
25
 
@@ -90,7 +89,7 @@ module ActiveRecord
90
89
  end
91
90
 
92
91
  def association_klass(association)
93
- association.klass
92
+ association.class_name.constantize
94
93
  rescue NoMethodError
95
94
  association.options.fetch(:through).to_s.classify.constantize
96
95
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Factory
6
+ module Columns
7
+ module YmlFactory
8
+ def self.build(
9
+ table_name,
10
+ primary_keys: ActiveRecord::Base.connection.primary_keys(table_name),
11
+ foreign_keys: ActiveRecord::Base.connection.foreign_keys(table_name)
12
+ )
13
+ ActiveRecord::Base.connection.columns(table_name).map do |column|
14
+ column_data_test = ActiveRecord::Dbt::Column::DataTest.new(
15
+ table_name,
16
+ column,
17
+ primary_keys: primary_keys,
18
+ foreign_keys: foreign_keys
19
+ )
20
+
21
+ ActiveRecord::Dbt::Column::Yml.new(
22
+ table_name,
23
+ column,
24
+ column_data_test,
25
+ primary_keys: primary_keys
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Factory
6
+ module Model
7
+ module Staging
8
+ module YmlFactory
9
+ def self.build(table_name)
10
+ table_factory = ActiveRecord::Dbt::Factory::Table::YmlFactory.build(table_name)
11
+ yml = ActiveRecord::Dbt::Model::Staging::Yml.new(table_factory)
12
+ struct = Struct.new(:export_path, :dump, keyword_init: true)
13
+
14
+ struct.new(
15
+ export_path: yml.export_path,
16
+ dump: yml.dump
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Factory
6
+ module Source
7
+ module YmlFactory
8
+ def self.build
9
+ tables_factory = ActiveRecord::Dbt::Factory::Tables::YmlFactory.build
10
+
11
+ ActiveRecord::Dbt::Source::Yml.new(tables_factory)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Factory
6
+ module Table
7
+ module YmlFactory
8
+ def self.build(table_name)
9
+ table_data_test = ActiveRecord::Dbt::Table::DataTest.new(table_name)
10
+ columns = ActiveRecord::Dbt::Factory::Columns::YmlFactory.build(table_name)
11
+
12
+ ActiveRecord::Dbt::Table::Yml.new(table_name, table_data_test, columns)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module Factory
6
+ module Tables
7
+ module YmlFactory
8
+ def self.build
9
+ ActiveRecord::Base.connection.tables.sort.map do |table_name|
10
+ ActiveRecord::Dbt::Factory::Table::YmlFactory.build(table_name)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Dbt
5
+ module I18nWrapper
6
+ module Translate
7
+ extend ActiveRecord::Dbt::RequiredMethods
8
+
9
+ define_required_methods :table_name
10
+
11
+ delegate :singularize, to: :table_name, prefix: true
12
+
13
+ def translated_table_name
14
+ I18n.t("activerecord.models.#{table_name_singularize}", default: nil)
15
+ end
16
+
17
+ def translated_attribute_name
18
+ @translated_attribute_name ||=
19
+ translated_column_name || translated_default_column_name
20
+ end
21
+
22
+ private
23
+
24
+ def translated_column_name
25
+ I18n.t("activerecord.attributes.#{table_name_singularize}.#{column_name}", default: nil)
26
+ end
27
+
28
+ def translated_default_column_name
29
+ I18n.t("attributes.#{column_name}", default: nil)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -36,7 +36,7 @@ module ActiveRecord
36
36
  private
37
37
 
38
38
  def basename
39
- "#{export_directory_path}/#{model_name}"
39
+ "#{export_directory_path}/models/staging/#{source_name}/#{model_name}"
40
40
  end
41
41
 
42
42
  def model_name
@@ -21,23 +21,27 @@ module ActiveRecord
21
21
  "#{basename}.yml"
22
22
  end
23
23
 
24
- def config
24
+ def dump
25
+ YAML.dump(properties.deep_stringify_keys)
26
+ end
27
+
28
+ private
29
+
30
+ def properties
25
31
  {
26
32
  'version' => 2,
27
33
  'models' => [
28
34
  {
29
35
  'name' => model_name,
30
- **table.config.except('name', 'columns'),
36
+ **table.properties.except('name', 'columns'),
31
37
  'columns' => override_columns
32
38
  }
33
39
  ]
34
40
  }
35
41
  end
36
42
 
37
- private
38
-
39
43
  def columns
40
- @columns ||= sort_columns(table.config['columns'])
44
+ @columns ||= sort_columns(table.properties['columns'])
41
45
  end
42
46
 
43
47
  def sort_columns(columns)
@@ -75,7 +79,7 @@ module ActiveRecord
75
79
  end
76
80
 
77
81
  def add_relationship_test(column)
78
- column['tests'].push(relationships_test(column['name']))
82
+ column['data_tests'].push(relationships_test(column['name']))
79
83
  end
80
84
 
81
85
  def rename_primary_id_in_column(column)