Skip to content

Commit

Permalink
refactor: fields mapping refactor (#3595)
Browse files Browse the repository at this point in the history
* 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 <julian@julianrubisch.at>
  • Loading branch information
Paul-Bob and julianrubisch authored Jan 20, 2025
1 parent 3dfd9e7 commit 041d00f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 113 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ brakeman_results.html

.env
.env.test
*~
2 changes: 1 addition & 1 deletion db/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
113 changes: 113 additions & 0 deletions lib/avo/mappings.rb
Original file line number Diff line number Diff line change
@@ -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
115 changes: 3 additions & 112 deletions lib/generators/avo/resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 041d00f

Please sign in to comment.