Created
January 31, 2014 10:04
-
-
Save kreshikhin/8729395 to your computer and use it in GitHub Desktop.
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
require 'zbase32' | |
require 'translit' | |
class Product | |
include MongoMapper::Document | |
@@semaphore = Mutex.new | |
key :url, String # ??? | |
key :origin_url, String | |
key :keywords, String | |
key :translit, String, :default => '' | |
key :title, String | |
key :brand, String | |
key :description, String | |
key :pricing, String | |
key :article, String | |
key :visible, Boolean, :default => true | |
key :price, Integer | |
key :nearby, String | |
many :sizes | |
key :features, Array | |
belongs_to :category | |
def category | |
return Category.default if self[:category_id].nil? | |
return Category.find(self[:category_id]) | |
end | |
belongs_to :photo | |
key :photo_ids, Array | |
many :photos, :in => :photo_ids | |
key :texture_ids, Array | |
many :textures, :in => :texture_ids | |
def photo | |
photo = Photo.find(self[:photo_id]) | |
return photo if not photo.nil? | |
return Photo.default | |
end | |
def self.create_by_arrival(arrival) | |
product_category_id = nil | |
@@semaphore.synchronize { | |
path = arrival.categories | |
if path.nil? or path.empty? | |
category = Category.default | |
else | |
category = Category.where(:parent_id=>nil).detect{ | |
|c| c.title == path[0]} | |
if category.nil? | |
category = Category.create(title: path[0]) | |
category.test_translit | |
end | |
path[1..-1].each do |title| | |
subcategory = category.children.detect{|c| c.title == title} | |
if subcategory.nil? | |
subcategory = Category.create(title: title) | |
subcategory.test_translit | |
subcategory.parent = category | |
category.children << subcategory | |
category.save | |
subcategory.save | |
end | |
category = subcategory | |
end | |
end | |
product_category_id = category.id | |
} | |
product = Product.find_or_create_by_origin_url(arrival.origin_url) | |
product.article = arrival.article | |
product.category_id = product_category_id | |
product.title = arrival.title | |
product.brand = arrival.brand | |
product.origin_url = arrival.origin_url | |
product.description = arrival.description | |
product.article = arrival.article | |
product.price = arrival.price | |
product.features = arrival.features | |
product.photo = arrival.photo | |
product.sizes = arrival.sizes | |
product.photos = arrival.photos | |
product.textures = arrival.textures | |
product.nearby = arrival.nearby | |
product.update_keywords | |
return product.save | |
end | |
def href | |
test_translit | |
return (self.category.translit || '') + '/' + self.translit | |
end | |
def test_translit | |
correct_translit = Translit.make_link(self.title || 'q') | |
product = Product.find_by_translit(correct_translit) | |
while product and product != self | |
correct_translit = Translit.make_link( | |
((self.title || '') + '-' + (rand(999) + 1).to_s)) | |
product = Product.find_by_translit(correct_translit) | |
end | |
return if self.translit == correct_translit | |
self.translit = correct_translit | |
self.save | |
end | |
def dump | |
result = JSON.parse(self.to_json) | |
result["category"] = self.category.dump | |
result["href"] = href | |
return result | |
end | |
def update_keywords | |
self.keywords = [id, title, price, article, brand, description].join(" ") | |
self.keywords += features.join(" ") | |
self.keywords += (self.textures || []).map{|t| t.title}.join(" ") | |
end | |
def get_numeric_sizes | |
self.sizes.each do |size| | |
tag = size.title | |
sizes = size.values | |
if (not sizes.empty?) && (sizes[0].to_i != 0) | |
result = [] | |
sizes.each{|s| ns = s.to_i; next if result.include?(ns); result << ns} | |
return result | |
end | |
end | |
return [] | |
end | |
def get_letter_sizes | |
self.sizes.each do |size| | |
tag = size.title | |
sizes = size.values | |
if (not sizes.empty?) && (sizes[0].to_i == 0) | |
return sizes.map{|s| s.gsub(/[^A^B^C^D^X^L^M^S]/, '')} | |
end | |
end | |
return [] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment