From 041d00fd5a5ad1141814ad4f5cf02fe1db11760a Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Mon, 20 Jan 2025 17:03:11 +0200 Subject: [PATCH] refactor: fields mapping refactor (#3595) * Add .ruby-version file * chore: Set up Avo Meta * test: Write system test for happy path of creating a new schema entry * chore: Extract Avo::Mappings module with constants * chore: Reflect changes from avo-meta (look up field type) * chore: Test meta default values * chore: Test backfilling meta default values * wip * schema * lint --------- Co-authored-by: Julian Rubisch --- .gitignore | 1 + db/factories.rb | 2 +- lib/avo/mappings.rb | 113 ++++++++++++++++++++++ lib/generators/avo/resource_generator.rb | 115 +---------------------- 4 files changed, 118 insertions(+), 113 deletions(-) create mode 100644 lib/avo/mappings.rb diff --git a/.gitignore b/.gitignore index 8e5fdcceb..72c37ca3c 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,4 @@ brakeman_results.html .env .env.test +*~ diff --git a/db/factories.rb b/db/factories.rb index a9541adbd..15cc6c10d 100644 --- a/db/factories.rb +++ b/db/factories.rb @@ -37,7 +37,7 @@ factory :project do name { Faker::App.name } - status { ['closed', :rejected, :failed, 'loading', :running, :waiting].sample } + status { ["closed", :rejected, :failed, "loading", :running, :waiting].sample } stage { ["Discovery", "Idea", "Done", "On hold", "Cancelled", "Drafting"].sample } budget { Faker::Number.decimal(l_digits: 4) } country { Faker::Address.country_code } diff --git a/lib/avo/mappings.rb b/lib/avo/mappings.rb new file mode 100644 index 000000000..8723320b6 --- /dev/null +++ b/lib/avo/mappings.rb @@ -0,0 +1,113 @@ +module Avo + module Mappings + unless defined?(ASSOCIATIONS_MAPPING) + ASSOCIATIONS_MAPPING = { + ActiveRecord::Reflection::BelongsToReflection => { + field: "belongs_to" + }, + ActiveRecord::Reflection::HasOneReflection => { + field: "has_one" + }, + ActiveRecord::Reflection::HasManyReflection => { + field: "has_many" + }, + ActiveRecord::Reflection::HasAndBelongsToManyReflection => { + field: "has_and_belongs_to_many" + } + }.freeze + end + + unless defined?(ATTACHMENTS_MAPPING) + ATTACHMENTS_MAPPING = { + ActiveRecord::Reflection::HasOneReflection => { + field: "file" + }, + ActiveRecord::Reflection::HasManyReflection => { + field: "files" + } + }.freeze + end + + unless defined?(FIELDS_MAPPING) + FIELDS_MAPPING = { + primary_key: { + field: "id" + }, + string: { + field: "text" + }, + text: { + field: "textarea" + }, + integer: { + field: "number" + }, + float: { + field: "number" + }, + decimal: { + field: "number" + }, + datetime: { + field: "date_time" + }, + timestamp: { + field: "date_time" + }, + time: { + field: "date_time" + }, + date: { + field: "date" + }, + binary: { + field: "number" + }, + boolean: { + field: "boolean" + }, + references: { + field: "belongs_to" + }, + json: { + field: "code" + } + }.freeze + end + + unless defined?(NAMES_MAPPING) + NAMES_MAPPING = { + id: { + field: "id" + }, + description: { + field: "textarea" + }, + gravatar: { + field: "gravatar" + }, + email: { + field: "text" + }, + password: { + field: "password" + }, + password_confirmation: { + field: "password" + }, + stage: { + field: "select" + }, + budget: { + field: "currency" + }, + money: { + field: "currency" + }, + country: { + field: "country" + } + }.freeze + end + end +end diff --git a/lib/generators/avo/resource_generator.rb b/lib/generators/avo/resource_generator.rb index d67559bbf..160aa1970 100644 --- a/lib/generators/avo/resource_generator.rb +++ b/lib/generators/avo/resource_generator.rb @@ -175,7 +175,7 @@ def fields_from_model_associations fields[name] = if association.is_a? ActiveRecord::Reflection::ThroughReflection field_from_through_association(association) else - associations_mapping[association.class] + ::Avo::Mappings::ASSOCIATIONS_MAPPING[association.class] end end end @@ -198,7 +198,7 @@ def field_from_through_association(association) def fields_from_model_attachements attachments.each do |name, attachment| - fields[remove_last_word_from name] = attachments_mapping[attachment.class] + fields[remove_last_word_from name] = ::Avo::Mappings::ATTACHMENTS_MAPPING[attachment.class] end end @@ -228,115 +228,6 @@ def fields_from_model_db_columns end end - def associations_mapping - { - ActiveRecord::Reflection::BelongsToReflection => { - field: "belongs_to" - }, - ActiveRecord::Reflection::HasOneReflection => { - field: "has_one" - }, - ActiveRecord::Reflection::HasManyReflection => { - field: "has_many" - }, - ActiveRecord::Reflection::HasAndBelongsToManyReflection => { - field: "has_and_belongs_to_many" - } - } - end - - def attachments_mapping - { - ActiveRecord::Reflection::HasOneReflection => { - field: "file" - }, - ActiveRecord::Reflection::HasManyReflection => { - field: "files" - } - } - end - - def names_mapping - { - id: { - field: "id" - }, - description: { - field: "textarea" - }, - gravatar: { - field: "gravatar" - }, - email: { - field: "text" - }, - password: { - field: "password" - }, - password_confirmation: { - field: "password" - }, - stage: { - field: "select" - }, - budget: { - field: "currency" - }, - money: { - field: "currency" - }, - country: { - field: "country" - } - } - end - - def fields_mapping - { - primary_key: { - field: "id" - }, - string: { - field: "text" - }, - text: { - field: "textarea" - }, - integer: { - field: "number" - }, - float: { - field: "number" - }, - decimal: { - field: "number" - }, - datetime: { - field: "date_time" - }, - timestamp: { - field: "date_time" - }, - time: { - field: "date_time" - }, - date: { - field: "date" - }, - binary: { - field: "number" - }, - boolean: { - field: "boolean" - }, - references: { - field: "belongs_to" - }, - json: { - field: "code" - } - } - end def generate_fields return generate_fields_from_args if invoked_by_model_generator? return unless can_connect_to_the_database? @@ -385,7 +276,7 @@ def generate_fields_from_args end def field(name, type) - names_mapping[name.to_sym] || fields_mapping[type&.to_sym] || {field: "text"} + ::Avo::Mappings::NAMES_MAPPING[name.to_sym] || ::Avo::Mappings::FIELDS_MAPPING[type&.to_sym] || {field: "text"} end end end