Module: SubjModels::NomenclatureModule

Defined in:
lib/subj_models/nomenclature.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(including_class) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/subj_models/nomenclature.rb', line 8

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 :actions
    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(:actions).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
      order_ids = Order.joins(order_items: [nomenclature_variety: :nomenclature]).where("nomenclatures.external_id IN (?)", ids).uniq.pluck(:id)
      Nomenclature.joins(nomenclature_varieties: [order_items: :order]).where("orders.id" => order_ids).order(:id).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(:actions).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)
      order(name: ordering)
    end

    scope :order_by_popularity, -> (ordering) do
      ordering = ordering.second.try(:to_sym)
      order(popularity: ordering)
    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

.name_field_update(field) ⇒ Object



155
156
157
158
159
# File 'lib/subj_models/nomenclature.rb', line 155

def self.name_field_update(field)
  if field.to_s == 'name' #TODO refactor this
    field = 'name.raw'
  end
end

Instance Method Details

#is_bought(user_id) ⇒ Object



145
146
147
148
149
# File 'lib/subj_models/nomenclature.rb', line 145

def is_bought(user_id)
  Order.joins(order_items: :nomenclature)
  .where("nomenclatures.id IN (?)", self.id)
  .where(user_id: user_id).any?
end

#to_sObject



151
152
153
# File 'lib/subj_models/nomenclature.rb', line 151

def to_s
  name.to_s # TODO
end