require 'json' require 'subj_models/concerns/comprising_external_id' module SubjModels module NomenclatureModule def self.included(including_class) including_class.class_eval do include SubjModels::ComprisingExternalId belongs_to :brand_line belongs_to :brand belongs_to :user_specialization belongs_to :category has_many :nomenclature_varieties, dependent: :destroy has_many :nomenclature_photos, dependent: :destroy has_many :nomenclature_reviews, dependent: :destroy has_many :nomenclature_files, dependent: :destroy has_many :attribute_values, dependent: :destroy has_many :nomenclature_prices, dependent: :destroy belongs_to :analog_related_nomenclature, class_name: "Nomenclature" has_many :analogs, foreign_key: "analog_related_nomenclature_id", class_name: "Nomenclature" belongs_to :bought_together_related_nomenclature, class_name: "Nomenclature" has_many :bought_together, foreign_key: "bought_together_related_nomenclature_id", class_name: "Nomenclature" has_and_belongs_to_many :action_banners has_and_belongs_to_many :access_groups, through: :nomenclature_access_groups has_and_belongs_to_many :events validates :name, presence: true scope :with_category, -> { where.not(category_id: nil) } scope :brand_line_ids, -> (brand_line_id) { where(brand_line_id: brand_line_id) } scope :with_action, -> { joins(:action_banners).distinct } scope :is_recommended, -> condition { where(is_recommended: condition) } scope :in_index_list, -> condition { where(show_on_index: condition) } scope :is_stock, -> (condition) do nomenclature_prices_join.joins("LEFT OUTER JOIN qualities ON qualities.id = nomenclature_prices.quality_id").where("qualities.is_stock" => condition).uniq end scope :bought_together_external_id, -> (ids) do joins(:bought_together_related_nomenclature).where(bought_together_related_nomenclature: {external_id: ids }).uniq end scope :is_available, -> (params) do is_available = params.first return all unless is_available == 'true' user = User.find_by(id: params.second.to_i) not_professional_ids = Nomenclature.is_professional(false).pluck(:id) ids = user ? user.available_nomenclature_ids.concat(not_professional_ids) : [] where(id: ids) end scope :only_promotional, -> do joins(:action_banners).uniq end scope :is_professional, -> (condition) {where(is_professional: condition) } scope :price, -> (prices) do prices = prices.map { |p| p.split(',') } nomenclature_prices_join. where( prices.map do |range| if range[0] && range[1] '(nomenclature_prices.current_price >= ? AND nomenclature_prices.current_price < ?)' elsif range[0] '(nomenclature_prices.current_price >= ?)' elsif range[1] '(nomenclature_prices.current_price < ?)' end end.join(' OR '), *prices.map(&:compact).flatten ) end scope :order_by_name, -> (ordering) do ordering = ordering.second.try(:to_sym) if [:asc, :desc].include? ordering order(name: ordering) end end scope :order_by_popularity, -> (ordering) do ordering = ordering.second.try(:to_sym) if [:asc, :desc].include? ordering order(popularity: ordering) end end scope :attribute_value_ids, -> (attribute_value_name_strings) do query_result = nil nomenclature_ids = ids attribute_value_name_strings.each do |str| attr_names = str.split('/') query_result = Nomenclature.joins(:attribute_values).where("attribute_values.nomenclature_value" => attr_names).where(id: nomenclature_ids).uniq nomenclature_ids = query_result.pluck(:id) end query_result end scope :nomenclature_prices_join, -> do joins(:nomenclature_prices) #joins('LEFT OUTER JOIN nomenclature_varieties ON nomenclature_varieties.id = (SELECT innerNomVar.id FROM nomenclature_varieties as innerNomVar WHERE innerNomVar.nomenclature_id = nomenclatures.id LIMIT 1)') #.joins('LEFT OUTER JOIN nomenclature_prices on nomenclature_prices.id = (SELECT innerNomPrice.id FROM nomenclature_prices AS innerNomPrice WHERE innerNomPrice.nomenclature_variety_id = nomenclature_varieties.id LIMIT 1)') end scope :order_by_price, -> (ordering) do ordering = ordering.second.try(:to_sym) if [:asc, :desc].include? ordering nomenclature_prices_join. order("nomenclature_prices.current_price #{ordering}") end end scope :category_id, -> (category_id) do return all unless category_id.present? where(category_id: category_id) end scope :nomenclature_ids, -> (ids) { where(id: ids) } scope :brand_ids, -> (brand) do return all unless brand.present? where(brand: brand) end scope :with_brand, -> (brand) do return none unless brand.present? where(brand: brand) end scope :brand_line_ids, -> (ids) do return all unless ids.present? where(brand_line_id: ids) end end end def is_bought(user_id) Order.joins(order_items: :nomenclature) .where("nomenclatures.id IN (?)", self.id) .where(user_id: user_id).any? end def to_s name.to_s # TODO end def self.name_field_update(field) if field.to_s == 'name' #TODO refactor this field = 'name.raw' end end end end