diff options
author | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2018-10-05 14:28:15 -0400 |
---|---|---|
committer | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2018-10-05 17:57:55 -0400 |
commit | ee5ab2228be88caa9a1dcdd8841488847cb43c07 (patch) | |
tree | 81d290a0ce663d497f49d77b085b146c4ce5d10f | |
parent | ea4b52ab3cd375d314b0d9d74724cdb49b4fcdd4 (diff) | |
download | rails-ee5ab2228be88caa9a1dcdd8841488847cb43c07.tar.gz rails-ee5ab2228be88caa9a1dcdd8841488847cb43c07.tar.bz2 rails-ee5ab2228be88caa9a1dcdd8841488847cb43c07.zip |
Introduce FixtureSet::ModelMetadata
-rw-r--r-- | activerecord/lib/active_record/fixture_set/model_metadata.rb | 40 | ||||
-rw-r--r-- | activerecord/lib/active_record/fixtures.rb | 38 |
2 files changed, 49 insertions, 29 deletions
diff --git a/activerecord/lib/active_record/fixture_set/model_metadata.rb b/activerecord/lib/active_record/fixture_set/model_metadata.rb new file mode 100644 index 0000000000..edc03939c8 --- /dev/null +++ b/activerecord/lib/active_record/fixture_set/model_metadata.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module ActiveRecord + class FixtureSet + class ModelMetadata # :nodoc: + def initialize(model_class, table_name) + @model_class = model_class + @table_name = table_name + end + + def primary_key_name + @primary_key_name ||= @model_class && @model_class.primary_key + end + + def primary_key_type + @primary_key_type ||= @model_class && @model_class.type_for_attribute(@model_class.primary_key).type + end + + def has_primary_key_column? + @has_primary_key_column ||= primary_key_name && + @model_class.columns.any? { |col| col.name == primary_key_name } + end + + def timestamp_column_names + @timestamp_column_names ||= + %w(created_at created_on updated_at updated_on) & column_names + end + + def inheritance_column_name + @inheritance_column_name ||= @model_class && @model_class.inheritance_column + end + + private + + def column_names + @column_names ||= @model_class.connection.columns(@table_name).collect(&:name) + end + end + end +end diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index d3300426cb..30fbc2a84a 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -8,6 +8,7 @@ require "active_support/dependencies" require "active_support/core_ext/digest/uuid" require "active_record/fixture_set/file" require "active_record/fixture_set/render_context" +require "active_record/fixture_set/model_metadata" require "active_record/test_fixtures" require "active_record/errors" @@ -669,7 +670,7 @@ module ActiveRecord if model_class # fill in timestamp columns if they aren't specified and the model is set to record_timestamps if model_class.record_timestamps - timestamp_column_names.each do |c_name| + model_metadata.timestamp_column_names.each do |c_name| row[c_name] = now unless row.key?(c_name) end end @@ -680,8 +681,8 @@ module ActiveRecord end # generate a primary key if necessary - if has_primary_key_column? && !row.include?(primary_key_name) - row[primary_key_name] = ActiveRecord::FixtureSet.identify(label, primary_key_type) + if model_metadata.has_primary_key_column? && !row.include?(model_metadata.primary_key_name) + row[model_metadata.primary_key_name] = ActiveRecord::FixtureSet.identify(label, model_metadata.primary_key_type) end # Resolve enums @@ -693,8 +694,8 @@ module ActiveRecord # If STI is used, find the correct subclass for association reflection reflection_class = - if row.include?(inheritance_column_name) - row[inheritance_column_name].constantize rescue model_class + if row.include?(model_metadata.inheritance_column_name) + row[model_metadata.inheritance_column_name].constantize rescue model_class else model_class end @@ -760,13 +761,6 @@ module ActiveRecord end private - def primary_key_name - @primary_key_name ||= model_class && model_class.primary_key - end - - def primary_key_type - @primary_key_type ||= model_class && model_class.type_for_attribute(model_class.primary_key).type - end def add_join_records(rows, row, association) # This is the case when the join table has no fixtures file @@ -778,28 +772,14 @@ module ActiveRecord targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) rows[table_name].concat targets.map { |target| - { lhs_key => row[primary_key_name], + { lhs_key => row[model_metadata.primary_key_name], rhs_key => ActiveRecord::FixtureSet.identify(target, column_type) } } end end - def has_primary_key_column? - @has_primary_key_column ||= primary_key_name && - model_class.columns.any? { |c| c.name == primary_key_name } - end - - def timestamp_column_names - @timestamp_column_names ||= - %w(created_at created_on updated_at updated_on) & column_names - end - - def inheritance_column_name - @inheritance_column_name ||= model_class && model_class.inheritance_column - end - - def column_names - @column_names ||= @connection.columns(@table_name).collect(&:name) + def model_metadata + @model_metadata ||= ModelMetadata.new(model_class, table_name) end def model_class=(class_name) |