-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sync_variations to Document model
- Loading branch information
1 parent
b30a555
commit 2f57747
Showing
2 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |