Skip to content

Commit

Permalink
Add sync_variations to Document model
Browse files Browse the repository at this point in the history
  • Loading branch information
janwerkhoven committed Sep 24, 2023
1 parent b30a555 commit 2f57747
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/cdn_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class CdnFile < ApplicationRecord

# The owner can be an Image, Video or Document record.
def owner
return image if image.present?
return video if video.present?
# return image if image.present?
# return video if video.present?
return document if document.present?

nil
Expand Down
38 changes: 36 additions & 2 deletions app/models/document.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
# create_table "documents", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
# t.string "path", null: false
# t.string "name"
# t.string "language_id"
# t.string "document_category_id"
# t.datetime "created_at", null: false
# t.datetime "updated_at", null: false
# t.string "variations"
# t.boolean "public", default: false
# end

class Document < ApplicationRecord
belongs_to :document_category, optional: true
alias_attribute :category, :document_category

has_many :cdn_files
has_many :cdn_files, dependent: :destroy
alias_attribute :files, :cdn_files

has_many :product_documents
has_many :product_documents, dependent: :destroy
has_many :products, through: :product_documents, source: :product

def sync_variations
return if files.nil?
return if files.none?

# documents/products/Flexsol-903/TD-Flexsol-903-EN.pdf
# documents/products/Flexsol-903/TD-Flexsol-903-DE.pdf
# documents/products/Flexsol-903/TD-Flexsol-903-FR.pdf
paths = files.map(&:path)

# TD-Flexsol-903-EN.pdf
# TD-Flexsol-903-DE.pdf
# TD-Flexsol-903-FR.pdf
file_names = paths.map { |p| p.split('/').last }

# EN.pdf
# DE.pdf
# FR.pdf
extensions = file_names.map { |p| p.split('-').last }

# EN.pdf,DE.pdf,FR.pdf
update!(variations: extensions.join(','))
end
end

0 comments on commit 2f57747

Please sign in to comment.