Last active
August 29, 2015 14:04
-
-
Save nfriend21/2574b1e6e330fe39a623 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Product < ActiveRecord::Base | |
| # model code here... | |
| # for Sunspot gem. Need to run rake sunspot:solr:start to get this running, and to avoid connection errors. | |
| # each time you change the searchable code on a model, you need to reindex the solr database locally. | |
| searchable do | |
| integer :id | |
| text :name | |
| text :artist | |
| integer :website_id | |
| integer :category_id, :multiple => true | |
| time :archived_at | |
| text :tag_list do | |
| tag_list.join(', ') | |
| end | |
| end | |
| def crop_product | |
| picture.recreate_versions! if self.crop_x.present? | |
| end | |
| def generate_slug | |
| self.slug ||= name.parameterize | |
| unless Product.exists?(slug: self.slug) && Product.find_by_slug(self.slug).id == self.id | |
| self.slug = self.slug << ('-' + SecureRandom.base64(8).parameterize) | |
| end | |
| end | |
| def to_param | |
| slug | |
| end | |
| def self.reset_to_param | |
| define_method :to_param do | |
| id | |
| end | |
| end | |
| def we_fulfill_manufacturer | |
| self.website.reseller.manufacturers.find_by_is_reseller(true) | |
| end | |
| def we_fulfill_media | |
| self.we_fulfill_manufacturer.media | |
| end | |
| def self.search_by_keywords(id, search_string, restrictive_category_ids = nil) | |
| website = Website.find(id) | |
| products = main_search(id, search_string).results | |
| run_filters(products, website, search_string, restrictive_category_ids) | |
| end | |
| def self.run_filters(products, website, search_string, restrictive_category_ids) | |
| add_child_products(products, website, search_string) | |
| if products.any? | |
| products = filter_unavailable(products) | |
| products = filter_for_product_gallery(products, website, restrictive_category_ids) | |
| end | |
| end | |
| def self.add_child_products(products, website, search_string) | |
| if website.load_products_from_children? | |
| products.concat(self.search_by_keywords_in_children(website, search_string)) | |
| end | |
| end | |
| def self.search_by_keywords_in_children(website, search_string, products = []) | |
| website.children.each do |child_site| | |
| id = child_site.id | |
| products.concat(main_search(id, search_string).results) | |
| end | |
| filter_unauthorized_from_children(website, products) | |
| end | |
| def self.main_search(id, search_string) | |
| begin | |
| Product.search do | |
| with(:website_id, id) | |
| with(:archived_at, nil) | |
| fulltext search_string do | |
| phrase_fields(:name => 1, :artist => 3, :tag_list => 2) | |
| minimum_match 0 #enables search to be performed by each word, not the whole phrase. | |
| end | |
| paginate :page => 1, :per_page => 300 | |
| end | |
| rescue EOFError, RSolr::Error::Http | |
| [] | |
| end | |
| end | |
| def self.filter_unavailable(products, collection = []) | |
| products.each do |product| | |
| # product is not in a category. filter out visitor uploads, and make sure store allows product gallery visibility. | |
| if product.store && product.store.page && product.store.visible_in_product_galleries && !product.is_visitor_upload? && product.category_id.nil? | |
| collection << product | |
| # product is in a category and allows product gallery visiblity | |
| elsif product.category && product.category.pswd.blank? && product.category.visible_in_product_galleries | |
| collection << product | |
| end | |
| end | |
| collection | |
| end | |
| def self.filter_for_product_gallery(products, website, restrictive_category_ids) | |
| if restrictive_category_ids.present? && website.subdomain != 'global' | |
| products = products.select { |product| restrictive_category_ids.include?(product.category_id.to_s) } | |
| end | |
| products | |
| end | |
| def self.filter_unauthorized_from_children(website, products) | |
| products.select { |product| website.global_product_ids.include?(product.id) } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment