Skip to content

Commit

Permalink
Merge pull request #117 from Shopify/kc/values-yaml
Browse files Browse the repository at this point in the history
Add values.yml and update seeding to use it
  • Loading branch information
KComrade53 committed Mar 25, 2024
2 parents fdbde72 + 4ffd46a commit 39abb58
Show file tree
Hide file tree
Showing 34 changed files with 108,585 additions and 83,343 deletions.
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)
Loading

0 comments on commit 39abb58

Please sign in to comment.