Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add values.yml and update seeding to use it #117

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ RUN_CMD = printf "\e[%sm>> %-21s\e[0;1m\n" "1;34" # bold blue text with a >> pr
# INPUTS

CATEGORIES_DATA = $(shell find data/categories)
ATTRIBUTES_DATA = $(shell find data/attributes)
ATTRIBUTES_DATA = $(shell find data/attributes.yml)
VALUES_DATA = $(shell find data/values.yml)

###############################################################################
# TARGETS
Expand Down Expand Up @@ -69,12 +70,12 @@ build: $(DIST_GENERATED_SENTINEL) \
$(ATTRIBUTES_DATA_CUE)
.PHONY: build

$(DOCS_GENERATED_SENTINEL): $(LOCAL_DB) $(CATEGORIES_DATA) $(ATTRIBUTES_DATA)
$(DOCS_GENERATED_SENTINEL): $(LOCAL_DB) $(CATEGORIES_DATA) $(ATTRIBUTES_DATA) $(VALUES_DATA)
@$(GENERATE) "Building Docs" "$(GENERATED_DOCS_PATH)/*"
$(V)./bin/generate_docs $(VARG)
$(V)touch $@

$(DIST_GENERATED_SENTINEL): $(LOCAL_DB) $(CATEGORIES_DATA) $(ATTRIBUTES_DATA)
$(DIST_GENERATED_SENTINEL): $(LOCAL_DB) $(CATEGORIES_DATA) $(ATTRIBUTES_DATA) $(VALUES_DATA)
@$(GENERATE) "Building Dist" "$(GENERATED_DIST_PATH)/*.[json|txt]"
$(V)bin/generate_dist $(VARG)
$(V)touch $@
Expand Down Expand Up @@ -102,7 +103,7 @@ release: build
# CLEAN
clean:
@$(NUKE) "Cleaning dev db" $(LOCAL_DB)
$(V)rm -f $(LOCAL_DB)
$(V)rm -f $(LOCAL_DB)*
@$(NUKE) "Cleaning Generated Docs" $(GENERATED_DOCS_PATH)
$(V)rm -f $(DOCS_GENERATED_SENTINEL)
$(V)rm -rf $(GENERATED_DOCS_PATH)
Expand Down
5 changes: 5 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class Category < ApplicationRecord

has_many :categories_properties, dependent: :destroy
has_many :properties, through: :categories_properties, foreign_key: :property_friendly_id

def property_friendly_ids
properties.pluck(:friendly_id)
end

def property_friendly_ids=(ids)
self.properties = Property.where(friendly_id: ids)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/properties_property_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class PropertiesPropertyValue < ApplicationRecord
belongs_to :property
belongs_to :property_value
belongs_to :property_value, foreign_key: :property_value_friendly_id, primary_key: :friendly_id
end
10 changes: 9 additions & 1 deletion app/models/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ class Property < ApplicationRecord
has_many :categories, through: :categories_properties

has_many :properties_property_values, dependent: :destroy
has_many :property_values, through: :properties_property_values
has_many :property_values, through: :properties_property_values, foreign_key: :property_value_friendly_id

def property_value_friendly_ids
property_values.pluck(:friendly_id)
end

def property_value_friendly_ids=(ids)
self.property_values = PropertyValue.where(friendly_id: ids)
end

validates :name, presence: true

Expand Down
8 changes: 6 additions & 2 deletions app/models/property_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
class PropertyValue < ApplicationRecord
default_scope { order(Arel.sql("CASE WHEN name = 'Other' THEN 1 ELSE 0 END, name")) }

has_and_belongs_to_many :properties,
join_table: :properties_property_values
has_many :properties_property_values,
dependent: :destroy,
foreign_key: :property_value_friendly_id,
primary_key: :friendly_id,
inverse_of: :property_value
has_many :properties, through: :properties_property_values

validates :name, presence: true

Expand Down
46 changes: 31 additions & 15 deletions app/serializers/source_data/property_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,49 @@ def serialize(property)
end

def deserialize(hash)
Property.new(
id: hash["id"],
friendly_id: hash["friendly_id"],
name: hash["name"],
property_value_ids: hash["values"].map { _1["id"] },
)
Property.new(**attributes_from(hash)).tap do |property|
property.property_value_friendly_ids = if hash["values_from"].present?
Property.find_by!(friendly_id: hash["values_from"]).property_value_friendly_ids
else
hash["values"]
end
end
end

def deserialize_for_insert_all(array)
array.map do |hash|
{
id: hash["id"],
friendly_id: hash["friendly_id"],
name: hash["name"],
}
end
array.map { attributes_from(_1) }
end

def deserialize_for_join_insert_all(array)
array.flat_map do |hash|
hash["values"].map do |value_hash|
if hash["values_from"].present?
property = Property.find_by!(friendly_id: hash["values_from"])

next property.property_values.map do |value|
{
property_id: hash["id"],
property_value_friendly_id: value.friendly_id,
}
end
end

hash["values"].map do |value_friendly_id|
{
property_id: hash["id"],
property_value_id: value_hash["id"],
property_value_friendly_id: value_friendly_id,
}
end
end
end

private

def attributes_from(hash)
{
id: hash["id"],
friendly_id: hash["friendly_id"],
name: hash["name"],
}
end
end
end
2 changes: 2 additions & 0 deletions app/serializers/source_data/property_value_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def serialize(property_value)
{
"id" => property_value.id,
"name" => property_value.name,
"friendly_id": property_value.friendly_id,
}
end

Expand All @@ -27,6 +28,7 @@ def attributes_from(hash)
{
id: hash["id"],
name: hash["name"],
friendly_id: hash["friendly_id"],
}
end
end
Expand Down
4 changes: 3 additions & 1 deletion bin/seed
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ cli.parse!(ARGV)
puts("Seeding local database...")
cli.options_status

attributes_data = cli.parse_yaml("data/attributes/attributes.yml")
values_data = cli.parse_yaml("data/values.yml")
attributes_data = cli.parse_yaml("data/attributes.yml")
category_files = Dir.glob("#{CLI.root}/data/categories/*.yml")
verticals_data = category_files.map { cli.parse_yaml(_1) }

Application.establish_db_connection!
Application.load_and_reset_schema!

seed = DB::Seed.new(verbose: cli.options.verbose)
seed.values_from(values_data)
seed.attributes_from(attributes_data)
seed.categories_from(verticals_data)