Skip to content

Commit

Permalink
chore: Extract Avo::Mappings module with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Nov 27, 2024
1 parent a63b966 commit 9f8e33c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 112 deletions.
105 changes: 105 additions & 0 deletions lib/avo/mappings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module Avo
module Mappings
ASSOCIATIONS_MAPPING ||= {

Check failure on line 3 in lib/avo/mappings.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. Raw Output: lib/avo/mappings.rb:3:26: W: [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. 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

ATTACHMENTS_MAPPING ||= {

Check failure on line 18 in lib/avo/mappings.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. Raw Output: lib/avo/mappings.rb:18:25: W: [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. ATTACHMENTS_MAPPING ||= { ^^^
ActiveRecord::Reflection::HasOneReflection => {
field: "file"
},
ActiveRecord::Reflection::HasManyReflection => {
field: "files"
}
}.freeze

FIELDS_MAPPING ||= {

Check failure on line 27 in lib/avo/mappings.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. Raw Output: lib/avo/mappings.rb:27:20: W: [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. 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

NAMES_MAPPING ||= {

Check failure on line 72 in lib/avo/mappings.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. Raw Output: lib/avo/mappings.rb:72:19: W: [Correctable] Lint/OrAssignmentToConstant: Avoid using or-assignment with constants. 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
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 9f8e33c

Please sign in to comment.