product.rb
8.4 KB
1
2
3
4
5
6
7
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require_dependency 'product'
class Product
include Noosfero::GeoRef
# products x inputs
# Fetches products the enterprise can be interested on buying based on the
# inputs of said enterprise's products
# Ex:
# - Enterprise 1 has Product A that uses input X
# - Enterprise 2 has Product B that belongs to category X
# -> Enterprise 1 as a parameter to this scope would return product B
scope :sniffer_plugin_suppliers_products, lambda { |enterprise|
{
:select => "DISTINCT products_2.*,
'product' as view",
:joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
INNER JOIN categories ON ( inputs.product_category_id = categories.id )
INNER JOIN products products_2 ON ( categories.id = products_2.product_category_id )
INNER JOIN profiles ON ( profiles.id = products_2.profile_id )",
:conditions => "products.profile_id = #{enterprise.id}
AND profiles.public_profile = true AND profiles.visible = true
AND profiles.enabled = true
AND profiles.id <> #{enterprise.id}"
}
}
# inputs x products
# Fetches the enterprise's products that can be of interest to other
# enterprises based on their products' inputs
# Ex:
# - Enterprise 1 has Product A that belongs to category X
# - Enterprise 2 has Product B that uses input X
# -> Enterprise 1 as a parameter to this scope would return product A
# with an extra column `consumer_profile_id` equal to Enterprise 2 id
scope :sniffer_plugin_consumers_products, lambda { |enterprise|
{
:select => "DISTINCT products_2.*,
profiles.id as consumer_profile_id,
'product' as view",
:joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
INNER JOIN categories ON ( inputs.product_category_id = categories.id )
INNER JOIN products products_2 ON ( categories.id = products_2.product_category_id )
INNER JOIN profiles ON ( profiles.id = products.profile_id )",
:conditions => "products_2.profile_id = #{enterprise.id}
AND profiles.public_profile = true AND profiles.visible = true
AND profiles.enabled = true
AND profiles.id <> #{enterprise.id}"
}
}
# interest x products
# Fetches products the enterprise can be interested on buying based on the
# buyer interests definded by this enterprise's admin
# Ex:
# - Enterprise 1 has category X as a buyer interest
# - Enterprise 2 has Product B that belongs to category X
# -> Enterprise 1 as a parameter to this scope would return product B
scope :sniffer_plugin_interests_suppliers_products, lambda { |profile|
{
:from => "sniffer_plugin_profiles sniffer",
:select => "DISTINCT products.*,
'product' as view",
:joins => "INNER JOIN sniffer_plugin_opportunities AS op ON ( sniffer.id = op.profile_id AND op.opportunity_type = 'ProductCategory' )
INNER JOIN categories ON ( op.opportunity_id = categories.id )
INNER JOIN products ON ( products.product_category_id = categories.id )
INNER JOIN profiles ON ( products.profile_id = profiles.id )",
:conditions => "sniffer.enabled = true AND sniffer.profile_id = #{profile.id} AND products.profile_id <> #{profile.id}
AND profiles.public_profile = true AND profiles.visible = true
AND profiles.enabled = true
AND profiles.id <> #{profile.id}"
}
}
# products x interests
# Fetches products the enterprise can sell to others based on the buyer
# interests definded by other enterprises' admins
# Ex:
# - Enterprise 1 has Product A that belongs to category X
# - Enterprise 2 has category X as a buyer interest
# -> Enterprise 1 as a parameter to this scope would return product A
# with an extra column `consumer_profile_id` equal to Enterprise 2 id
scope :sniffer_plugin_interests_consumers_products, lambda { |profile|
{
:select => "DISTINCT products.*,
profiles.id as consumer_profile_id,
'product' as view",
:joins => "INNER JOIN categories ON ( categories.id = products.product_category_id )
INNER JOIN sniffer_plugin_opportunities as op ON ( categories.id = op.opportunity_id AND op.opportunity_type = 'ProductCategory' )
INNER JOIN sniffer_plugin_profiles sniffer ON ( op.profile_id = sniffer.id AND sniffer.enabled = true )
INNER JOIN profiles ON ( sniffer.profile_id = profiles.id )",
:conditions => "products.profile_id = #{profile.id}
AND profiles.public_profile = true AND profiles.visible = true
AND profiles.enabled = true
AND profiles.id <> #{profile.id}"
}
}
# knowledge x inputs
scope :sniffer_plugin_knowledge_consumers_inputs, lambda { |profile|
{
:select => "DISTINCT products.*,
articles.id AS knowledge_id,
'knowledge' as view",
:joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
INNER JOIN article_resources ON (article_resources.resource_id = inputs.product_category_id AND article_resources.resource_type = 'ProductCategory')
INNER JOIN articles ON (article_resources.article_id = articles.id)
INNER JOIN profiles ON ( products.profile_id = profiles.id )",
:conditions => "articles.type = 'CmsLearningPlugin::Learning'
AND articles.profile_id = #{profile.id}
AND products.profile_id <> #{profile.id}"
}
}
# inputs x knowledge
scope :sniffer_plugin_knowledge_suppliers_inputs, lambda { |profile|
{
:select => "DISTINCT products.*,
profiles.id as supplier_profile_id, articles.id AS knowledge_id,
'knowledge' as view",
:joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
INNER JOIN article_resources ON (article_resources.resource_id = inputs.product_category_id AND article_resources.resource_type = 'ProductCategory')
INNER JOIN articles ON (article_resources.article_id = articles.id)
INNER JOIN profiles ON ( articles.profile_id = profiles.id )",
:conditions => "articles.type = 'CmsLearningPlugin::Learning'
AND articles.profile_id <> #{profile.id}
AND products.profile_id = #{profile.id}"
}
}
# knowledge x interests
scope :sniffer_plugin_knowledge_consumers_interests, lambda { |profile|
{
:select => "DISTINCT articles.id AS knowledge_id,
op.opportunity_id AS product_category_id,
profiles.id as profile_id,
'knowledge' as view",
:from => "articles",
:joins => "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
INNER JOIN sniffer_plugin_opportunities as op ON ( article_resources.resource_id = op.opportunity_id AND op.opportunity_type = 'ProductCategory' AND article_resources.resource_type = 'ProductCategory' )
INNER JOIN sniffer_plugin_profiles sniffer ON ( op.profile_id = sniffer.id AND sniffer.enabled = true )
INNER JOIN profiles ON ( sniffer.profile_id = profiles.id )",
:conditions => "articles.profile_id = #{profile.id}
AND profiles.public_profile = true
AND profiles.visible = true
AND profiles.enabled = true
AND profiles.id <> #{profile.id}"
}
}
# interests x knowledge
scope :sniffer_plugin_knowledge_suppliers_interests, lambda { |profile|
{
:select => "DISTINCT articles.id AS knowledge_id,
op.opportunity_id AS product_category_id,
profiles.id as profile_id,
'knowledge' as view",
:from => "articles",
:joins => "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
INNER JOIN sniffer_plugin_opportunities as op ON ( article_resources.resource_id = op.opportunity_id AND op.opportunity_type = 'ProductCategory' AND article_resources.resource_type = 'ProductCategory' )
INNER JOIN sniffer_plugin_profiles sniffer ON ( op.profile_id = sniffer.id AND sniffer.enabled = true )
INNER JOIN profiles ON ( articles.profile_id = profiles.id )",
:conditions => "articles.profile_id <> #{profile.id}
AND profiles.public_profile = true
AND profiles.visible = true
AND profiles.enabled = true
AND sniffer.profile_id = #{profile.id}"
}
}
# searches for products as supplies for a given product category
scope :sniffer_plugin_products_from_category, lambda { |product_category|
{
:conditions => { :product_category_id => product_category.id },
:select => "*, 'product' as view"
}
}
end