Skip to content

Commit

Permalink
Merge branch 'avo-resource-meta' into avo-resource_meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Jan 20, 2025
2 parents 3dfd9e7 + e5d06f7 commit bc4b976
Show file tree
Hide file tree
Showing 5 changed files with 111 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
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 ||= {
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 ||= {
ActiveRecord::Reflection::HasOneReflection => {
field: "file"
},
ActiveRecord::Reflection::HasManyReflection => {
field: "files"
}
}.freeze

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 ||= {
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
1 change: 1 addition & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
t.datetime "updated_at", null: false
t.boolean "active", default: true
t.string "slug"
t.json "meta"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["slug"], name: "index_users_on_slug", unique: true
Expand Down

0 comments on commit bc4b976

Please sign in to comment.