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
144
145
146
147
148
149
150
151
|
# 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 :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)
end
scope :nomenclature_without_prices, -> do
where(nomenclature_prices: { nomenclature_id: nil })
end
scope :order_by_price, -> (ordering) do
ordering = ordering.second.try(:to_sym)
if [:asc, :desc].include? ordering
most_recent_prices_ids = NomenclaturePrice.all.group(:nomenclature_id).maximum(:id).values
nomenclature_prices_join.where('nomenclature_prices.id in (?)', most_recent_prices_ids).
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
|