Commit a04081a433e589b92d5aae80d715ab80e9c1ce9f

Authored by Braulio Bhavamitra
1 parent f2aaabd6

sniffer: drop sniffer_plugin_profiles table

Changed pulled from ESCAMBO
plugins/sniffer/controllers/sniffer_plugin_myprofile_controller.rb
1 1 class SnifferPluginMyprofileController < MyProfileController
2 2  
3   - before_filter :fetch_sniffer_profile, :only => [:edit, :search]
4   -
5 3 include SnifferPlugin::Helper
6 4 helper SnifferPlugin::Helper
7 5 helper CmsHelper
8 6  
9 7 def edit
10 8 if request.post?
11   - begin
12   - @sniffer_profile.update(params[:sniffer_plugin_profile])
13   - @sniffer_profile.enabled = true
14   - @sniffer_profile.save!
  9 + if @profile.update params[:profile_data]
15 10 session[:notice] = _('Consumer interests updated')
16   - rescue Exception => exception
17   - flash[:error] = _('Could not save consumer interests')
18 11 end
19 12 end
20 13 end
... ... @@ -22,22 +15,21 @@ class SnifferPluginMyprofileController &lt; MyProfileController
22 15 def product_category_search
23 16 query = params[:q] || params[:term]
24 17  
25   - scope = ProductCategory.by_environment(environment)
26   - @categories = find_by_contents(:product_categories, @profile, scope, query, {:per_page => 10, :page => 1})[:results]
  18 + @categories = find_by_contents(:categories, @profile, environment.product_categories, query, {per_page: 10, page: 1})[:results]
27 19  
28 20 autocomplete = params.has_key?(:term)
29   - render :json => @categories.map { |i| autocomplete ? {:value => i.id, :label => i.name} : {:id => i.id, :name => i.name} }
  21 + render json: @categories.map { |i| autocomplete ? {value: i.id, label: i.name} : {id: i.id, name: i.name} }
30 22 end
31 23  
32 24 def product_category_add
33 25 product_category = environment.categories.find params[:id]
34   - response = { :productCategory => {
  26 + response = { productCategory: {
35 27 :id => product_category.id
36 28 }
37 29 }
38 30 response[:enterprises] = product_category.sniffer_plugin_enterprises.enabled.visible.map do |enterprise|
39 31 profile_data = filter_visible_attr_profile(enterprise)
40   - profile_data[:balloonUrl] = url_for :controller => :sniffer_plugin_myprofile, :action => :map_balloon, :id => enterprise[:id], :escape => false
  32 + profile_data[:balloonUrl] = url_for controller: :sniffer_plugin_myprofile, action: :map_balloon, id: enterprise[:id], escape: false
41 33 profile_data[:sniffer_plugin_distance] = Noosfero::GeoRef.dist(@profile.lat, @profile.lng, enterprise.lat, enterprise.lng)
42 34 profile_data[:suppliersProducts] = filter_visible_attr_suppliers_products(
43 35 enterprise.products.sniffer_plugin_products_from_category(product_category)
... ... @@ -45,14 +37,14 @@ class SnifferPluginMyprofileController &lt; MyProfileController
45 37 profile_data[:consumersProducts] = []
46 38 profile_data
47 39 end
48   - render :text => response.to_json
  40 + render text: response.to_json
49 41 end
50 42  
51 43 def search
52 44 @no_design_blocks = true
53 45  
54   - suppliers_products = @sniffer_profile.suppliers_products
55   - consumers_products = @sniffer_profile.consumers_products
  46 + suppliers_products = @profile.sniffer_suppliers_products
  47 + consumers_products = @profile.sniffer_consumers_products
56 48  
57 49 profiles_of_interest = fetch_profiles(suppliers_products + consumers_products)
58 50  
... ... @@ -67,13 +59,13 @@ class SnifferPluginMyprofileController &lt; MyProfileController
67 59 @profiles_data = {}
68 60 suppliers.each do |id, products|
69 61 @profiles_data[id] = {
70   - :profile => profiles_of_interest[id],
71   - :suppliers_products => products,
72   - :consumers_products => []
  62 + profile: profiles_of_interest[id],
  63 + suppliers_products: products,
  64 + consumers_products: []
73 65 }
74 66 end
75 67 consumers.each do |id, products|
76   - @profiles_data[id] ||= { :profile => profiles_of_interest[id] }
  68 + @profiles_data[id] ||= { profile: profiles_of_interest[id] }
77 69 @profiles_data[id][:suppliers_products] ||= []
78 70 @profiles_data[id][:consumers_products] = products
79 71 end
... ... @@ -91,22 +83,18 @@ class SnifferPluginMyprofileController &lt; MyProfileController
91 83 @suppliers = build_products(suppliers_products).values.first
92 84 @consumers = build_products(consumers_products).values.first
93 85  
94   - render :layout => false
  86 + render layout: false
95 87 end
96 88  
97 89 def my_map_balloon
98 90 @categories = @profile.categories
99   - render :layout => false
  91 + render layout: false
100 92 end
101 93  
102 94 protected
103 95  
104   - def fetch_sniffer_profile
105   - @sniffer_profile = SnifferPlugin::Profile.find_or_create profile
106   - end
107   -
108 96 def fetch_profiles(products)
109   - profiles = Profile.all :conditions => {:id => products.map { |p| target_profile_id(p) }}
  97 + profiles = Profile.all conditions: {id: products.map { |p| target_profile_id(p) }}
110 98 profiles_by_id = {}
111 99 profiles.each do |p|
112 100 p.sniffer_plugin_distance = Noosfero::GeoRef.dist(@profile.lat, @profile.lng, p.lat, p.lng)
... ... @@ -125,9 +113,9 @@ class SnifferPluginMyprofileController &lt; MyProfileController
125 113  
126 114 id_profiles = fetch_profiles(data)
127 115  
128   - products = Product.all :conditions => {:id => grab_id.call('id')}, :include => [:enterprise, :product_category]
  116 + products = Product.all conditions: {id: grab_id.call('id')}, include: [:enterprise, :product_category]
129 117 products.each{ |p| id_products[p.id] ||= p }
130   - knowledges = Article.all :conditions => {:id => grab_id.call('knowledge_id')}
  118 + knowledges = Article.all conditions: {id: grab_id.call('knowledge_id')}
131 119 knowledges.each{ |k| id_knowledges[k.id] ||= k}
132 120  
133 121 data.each do |attributes|
... ... @@ -135,9 +123,9 @@ class SnifferPluginMyprofileController &lt; MyProfileController
135 123  
136 124 results[profile.id] ||= []
137 125 results[profile.id] << {
138   - :partial => attributes['view'],
139   - :product => id_products[attributes['id'].to_i],
140   - :knowledge => id_knowledges[attributes['knowledge_id'].to_i]
  126 + partial: attributes['view'],
  127 + product: id_products[attributes['id'].to_i],
  128 + knowledge: id_knowledges[attributes['knowledge_id'].to_i]
141 129 }
142 130 end
143 131 results
... ...
plugins/sniffer/db/migrate/20131212124106_drop_sniffer_profile_table.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +SnifferPlugin.send :remove_const, :Opportunity if defined? SnifferPlugin::Opportunity
  2 +
  3 +class SnifferPlugin::Profile < ActiveRecord::Base
  4 + belongs_to :profile
  5 +end
  6 +class SnifferPlugin::Opportunity < ActiveRecord::Base
  7 + belongs_to :sniffer_profile, class_name: 'SnifferPlugin::Profile', foreign_key: :profile_id
  8 +end
  9 +
  10 +class DropSnifferProfileTable < ActiveRecord::Migration
  11 + def self.up
  12 + SnifferPlugin::Opportunity.find_each do |opportunity|
  13 + sniffer_profile = opportunity.sniffer_profile
  14 + next unless sniffer_profile.profile
  15 +
  16 + opportunity.profile_id = sniffer_profile.profile.id
  17 + opportunity.save!
  18 + end
  19 +
  20 + drop_table :sniffer_plugin_profiles
  21 + end
  22 +
  23 + def self.down
  24 + end
  25 +end
... ...
plugins/sniffer/lib/ext/article.rb
... ... @@ -5,16 +5,16 @@ class Article
5 5 # search for interests of interested that matches the knowledges of wise
6 6 scope :sniffer_plugin_knowledges_interests, lambda { |wise, interested|
7 7 {
8   - :select => "op.opportunity_id AS interest_cat,
  8 + select: "op.opportunity_id AS interest_cat,
9 9 articles.name AS knowledge_name, articles.id AS id,
10 10 article_resources.resource_id AS knowledge_cat",
11   - :joins => "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
  11 + joins: "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
12 12 INNER JOIN sniffer_plugin_opportunities AS op ON (article_resources.resource_id = op.opportunity_id
13 13 AND article_resources.resource_type = 'ProductCategory' AND op.opportunity_type = 'ProductCategory')
14   - INNER JOIN sniffer_plugin_profiles sniffer ON (op.profile_id = sniffer.id AND sniffer.enabled = true)",
15   - :conditions => "articles.type = 'CmsLearningPlugin::Learning'
  14 + INNER JOIN profiles sniffer ON op.profile_id = sniffer.id",
  15 + conditions: "articles.type = 'CmsLearningPlugin::Learning'
16 16 AND articles.profile_id = #{wise.id}
17   - AND sniffer.profile_id = #{interested.id}"
  17 + AND sniffer.id = #{interested.id}"
18 18 }
19 19 }
20 20 end
... ...
plugins/sniffer/lib/ext/product.rb
... ... @@ -13,13 +13,13 @@ class Product
13 13 # -> Enterprise 1 as a parameter to this scope would return product B
14 14 scope :sniffer_plugin_suppliers_products, lambda { |enterprise|
15 15 {
16   - :select => "DISTINCT products_2.*,
  16 + select: "DISTINCT products_2.*,
17 17 'product' as view",
18   - :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
  18 + joins: "INNER JOIN inputs ON ( products.id = inputs.product_id )
19 19 INNER JOIN categories ON ( inputs.product_category_id = categories.id )
20 20 INNER JOIN products products_2 ON ( categories.id = products_2.product_category_id )
21 21 INNER JOIN profiles ON ( profiles.id = products_2.profile_id )",
22   - :conditions => "products.profile_id = #{enterprise.id}
  22 + conditions: "products.profile_id = #{enterprise.id}
23 23 AND profiles.public_profile = true AND profiles.visible = true
24 24 AND profiles.enabled = true
25 25 AND profiles.id <> #{enterprise.id}"
... ... @@ -36,14 +36,14 @@ class Product
36 36 # with an extra column `consumer_profile_id` equal to Enterprise 2 id
37 37 scope :sniffer_plugin_consumers_products, lambda { |enterprise|
38 38 {
39   - :select => "DISTINCT products_2.*,
  39 + select: "DISTINCT products_2.*,
40 40 profiles.id as consumer_profile_id,
41 41 'product' as view",
42   - :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
  42 + joins: "INNER JOIN inputs ON ( products.id = inputs.product_id )
43 43 INNER JOIN categories ON ( inputs.product_category_id = categories.id )
44 44 INNER JOIN products products_2 ON ( categories.id = products_2.product_category_id )
45 45 INNER JOIN profiles ON ( profiles.id = products.profile_id )",
46   - :conditions => "products_2.profile_id = #{enterprise.id}
  46 + conditions: "products_2.profile_id = #{enterprise.id}
47 47 AND profiles.public_profile = true AND profiles.visible = true
48 48 AND profiles.enabled = true
49 49 AND profiles.id <> #{enterprise.id}"
... ... @@ -59,14 +59,14 @@ class Product
59 59 # -> Enterprise 1 as a parameter to this scope would return product B
60 60 scope :sniffer_plugin_interests_suppliers_products, lambda { |profile|
61 61 {
62   - :from => "sniffer_plugin_profiles sniffer",
63   - :select => "DISTINCT products.*,
  62 + from: "profiles sniffer",
  63 + select: "DISTINCT products.*,
64 64 'product' as view",
65   - :joins => "INNER JOIN sniffer_plugin_opportunities AS op ON ( sniffer.id = op.profile_id AND op.opportunity_type = 'ProductCategory' )
  65 + joins: "INNER JOIN sniffer_plugin_opportunities AS op ON ( sniffer.id = op.profile_id AND op.opportunity_type = 'ProductCategory' )
66 66 INNER JOIN categories ON ( op.opportunity_id = categories.id )
67 67 INNER JOIN products ON ( products.product_category_id = categories.id )
68 68 INNER JOIN profiles ON ( products.profile_id = profiles.id )",
69   - :conditions => "sniffer.enabled = true AND sniffer.profile_id = #{profile.id} AND products.profile_id <> #{profile.id}
  69 + conditions: "sniffer.id = #{profile.id} AND products.profile_id <> #{profile.id}
70 70 AND profiles.public_profile = true AND profiles.visible = true
71 71 AND profiles.enabled = true
72 72 AND profiles.id <> #{profile.id}"
... ... @@ -83,14 +83,13 @@ class Product
83 83 # with an extra column `consumer_profile_id` equal to Enterprise 2 id
84 84 scope :sniffer_plugin_interests_consumers_products, lambda { |profile|
85 85 {
86   - :select => "DISTINCT products.*,
  86 + select: "DISTINCT products.*,
87 87 profiles.id as consumer_profile_id,
88 88 'product' as view",
89   - :joins => "INNER JOIN categories ON ( categories.id = products.product_category_id )
  89 + joins: "INNER JOIN categories ON ( categories.id = products.product_category_id )
90 90 INNER JOIN sniffer_plugin_opportunities as op ON ( categories.id = op.opportunity_id AND op.opportunity_type = 'ProductCategory' )
91   - INNER JOIN sniffer_plugin_profiles sniffer ON ( op.profile_id = sniffer.id AND sniffer.enabled = true )
92   - INNER JOIN profiles ON ( sniffer.profile_id = profiles.id )",
93   - :conditions => "products.profile_id = #{profile.id}
  91 + INNER JOIN profiles ON ( op.profile_id = profiles.id )",
  92 + conditions: "products.profile_id = #{profile.id}
94 93 AND profiles.public_profile = true AND profiles.visible = true
95 94 AND profiles.enabled = true
96 95 AND profiles.id <> #{profile.id}"
... ... @@ -100,14 +99,14 @@ class Product
100 99 # knowledge x inputs
101 100 scope :sniffer_plugin_knowledge_consumers_inputs, lambda { |profile|
102 101 {
103   - :select => "DISTINCT products.*,
  102 + select: "DISTINCT products.*,
104 103 articles.id AS knowledge_id,
105 104 'knowledge' as view",
106   - :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
  105 + joins: "INNER JOIN inputs ON ( products.id = inputs.product_id )
107 106 INNER JOIN article_resources ON (article_resources.resource_id = inputs.product_category_id AND article_resources.resource_type = 'ProductCategory')
108 107 INNER JOIN articles ON (article_resources.article_id = articles.id)
109 108 INNER JOIN profiles ON ( products.profile_id = profiles.id )",
110   - :conditions => "articles.type = 'CmsLearningPlugin::Learning'
  109 + conditions: "articles.type = 'CmsLearningPlugin::Learning'
111 110 AND articles.profile_id = #{profile.id}
112 111 AND products.profile_id <> #{profile.id}"
113 112 }
... ... @@ -116,14 +115,14 @@ class Product
116 115 # inputs x knowledge
117 116 scope :sniffer_plugin_knowledge_suppliers_inputs, lambda { |profile|
118 117 {
119   - :select => "DISTINCT products.*,
  118 + select: "DISTINCT products.*,
120 119 profiles.id as supplier_profile_id, articles.id AS knowledge_id,
121 120 'knowledge' as view",
122   - :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
  121 + joins: "INNER JOIN inputs ON ( products.id = inputs.product_id )
123 122 INNER JOIN article_resources ON (article_resources.resource_id = inputs.product_category_id AND article_resources.resource_type = 'ProductCategory')
124 123 INNER JOIN articles ON (article_resources.article_id = articles.id)
125 124 INNER JOIN profiles ON ( articles.profile_id = profiles.id )",
126   - :conditions => "articles.type = 'CmsLearningPlugin::Learning'
  125 + conditions: "articles.type = 'CmsLearningPlugin::Learning'
127 126 AND articles.profile_id <> #{profile.id}
128 127 AND products.profile_id = #{profile.id}"
129 128 }
... ... @@ -132,16 +131,15 @@ class Product
132 131 # knowledge x interests
133 132 scope :sniffer_plugin_knowledge_consumers_interests, lambda { |profile|
134 133 {
135   - :select => "DISTINCT articles.id AS knowledge_id,
  134 + select: "DISTINCT articles.id AS knowledge_id,
136 135 op.opportunity_id AS product_category_id,
137 136 profiles.id as profile_id,
138 137 'knowledge' as view",
139   - :from => "articles",
140   - :joins => "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
  138 + from: "articles",
  139 + joins: "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
141 140 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' )
142   - INNER JOIN sniffer_plugin_profiles sniffer ON ( op.profile_id = sniffer.id AND sniffer.enabled = true )
143   - INNER JOIN profiles ON ( sniffer.profile_id = profiles.id )",
144   - :conditions => "articles.profile_id = #{profile.id}
  141 + INNER JOIN profiles ON ( op.profile_id = profiles.id )",
  142 + conditions: "articles.profile_id = #{profile.id}
145 143 AND profiles.public_profile = true
146 144 AND profiles.visible = true
147 145 AND profiles.enabled = true
... ... @@ -152,28 +150,27 @@ class Product
152 150 # interests x knowledge
153 151 scope :sniffer_plugin_knowledge_suppliers_interests, lambda { |profile|
154 152 {
155   - :select => "DISTINCT articles.id AS knowledge_id,
  153 + select: "DISTINCT articles.id AS knowledge_id,
156 154 op.opportunity_id AS product_category_id,
157 155 profiles.id as profile_id,
158 156 'knowledge' as view",
159   - :from => "articles",
160   - :joins => "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
  157 + from: "articles",
  158 + joins: "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
161 159 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' )
162   - INNER JOIN sniffer_plugin_profiles sniffer ON ( op.profile_id = sniffer.id AND sniffer.enabled = true )
163 160 INNER JOIN profiles ON ( articles.profile_id = profiles.id )",
164   - :conditions => "articles.profile_id <> #{profile.id}
  161 + conditions: "articles.profile_id <> #{profile.id}
165 162 AND profiles.public_profile = true
166 163 AND profiles.visible = true
167 164 AND profiles.enabled = true
168   - AND sniffer.profile_id = #{profile.id}"
  165 + AND profiles.id = #{profile.id}"
169 166 }
170 167 }
171 168  
172 169 # searches for products as supplies for a given product category
173 170 scope :sniffer_plugin_products_from_category, lambda { |product_category|
174 171 {
175   - :conditions => { :product_category_id => product_category.id },
176   - :select => "*, 'product' as view"
  172 + conditions: { product_category_id: product_category.id },
  173 + select: "*, 'product' as view"
177 174 }
178 175 }
179 176  
... ...
plugins/sniffer/lib/ext/profile.rb
1 1 require_dependency 'profile'
2   -# WORKAROUND: plugin class don't scope subclasses causing core classes conflict
3   -require_dependency File.expand_path "#{File.dirname __FILE__}/../../lib/sniffer_plugin/profile"
4 2  
5 3 class Profile
6 4  
7   - has_one :sniffer_plugin_profile, :class_name => 'SnifferPlugin::Profile'
8   - has_many :sniffer_plugin_interests, :source => :product_categories, :through => :sniffer_plugin_profile
9   - has_many :sniffer_plugin_opportunities, :source => :opportunities, :through => :sniffer_plugin_profile
10   -
11 5 attr_accessor :sniffer_plugin_distance
12 6  
  7 + has_many :sniffer_opportunities, :class_name => 'SnifferPlugin::Opportunity', :dependent => :destroy
  8 + has_many :sniffer_interested_product_categories, :through => :sniffer_opportunities, :source => :product_category, :class_name => 'ProductCategory',
  9 + :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
  10 +
  11 + attr_accessor :sniffer_interested_product_category_string_ids
  12 + descendants.each do |k|
  13 + k.attr_accessible :sniffer_interested_product_category_string_ids
  14 + end
  15 +
  16 + def sniffer_interested_product_category_string_ids
  17 + ''
  18 + end
  19 + def sniffer_interested_product_category_string_ids=(ids)
  20 + ids = ids.split(',')
  21 + self.sniffer_interested_product_categories = []
  22 + r = environment.product_categories.find ids
  23 + self.sniffer_interested_product_categories = ids.collect{ |id| r.detect {|x| x.id == id.to_i} }
  24 + self.sniffer_opportunities.where(:opportunity_id => ids).each{|o| o.opportunity_type = 'ProductCategory'; o.save! }
  25 + end
  26 +
  27 + def sniffer_categories
  28 + (self.product_categories + self.input_categories + self.sniffer_interested_product_categories).uniq
  29 + end
  30 +
  31 + def sniffer_suppliers_products
  32 + products = []
  33 +
  34 + products += Product.sniffer_plugin_suppliers_products self if self.enterprise?
  35 + products += Product.sniffer_plugin_interests_suppliers_products self
  36 + if defined?(CmsLearningPlugin)
  37 + products += Product.sniffer_plugin_knowledge_suppliers_inputs self
  38 + products += Product.sniffer_plugin_knowledge_suppliers_interests self
  39 + end
  40 +
  41 + products
  42 + end
  43 +
  44 + def sniffer_consumers_products
  45 + products = []
  46 +
  47 + products += Product.sniffer_plugin_consumers_products self if self.enterprise?
  48 + products += Product.sniffer_plugin_interests_consumers_products self
  49 + if defined?(CmsLearningPlugin)
  50 + products += Product.sniffer_plugin_knowledge_consumers_inputs self
  51 + products += Product.sniffer_plugin_knowledge_consumers_interests self
  52 + end
  53 +
  54 + products
  55 + end
  56 +
13 57 end
... ...
plugins/sniffer/lib/sniffer_plugin/interests_block.rb
... ... @@ -18,11 +18,11 @@ class SnifferPlugin::InterestsBlock &lt; Block
18 18  
19 19 def content(args = {})
20 20 block = self
  21 + profile = block.owner
21 22 proc do
22 23 if block.owner.is_a?(Profile)
23   - sniffer = SnifferPlugin::Profile.find_or_create(block.owner)
24   - interests = sniffer.opportunities
25   - interests |= sniffer.profile.inputs if sniffer.profile.enterprise?
  24 + interests = profile.snnifer_opportunities
  25 + interests |= profile.inputs if sniffer.profile.enterprise?
26 26 else # Environment
27 27 interests = SnifferPlugin::Opportunity.product_categories :limit => 5, :order => 'created_at DESC'
28 28 interests += Input.all :limit => 5, :order => 'created_at DESC'
... ...
plugins/sniffer/lib/sniffer_plugin/opportunity.rb
... ... @@ -1,34 +0,0 @@
1   -class SnifferPlugin::Opportunity < ActiveRecord::Base
2   -
3   - self.table_name = :sniffer_plugin_opportunities
4   -
5   - belongs_to :sniffer_profile, :class_name => 'SnifferPlugin::Profile', :foreign_key => :profile_id
6   - has_one :profile, :through => :sniffer_profile
7   -
8   - belongs_to :opportunity, :polymorphic => true
9   -
10   - # for has_many :through
11   - belongs_to :product_category, :class_name => 'ProductCategory', :foreign_key => :opportunity_id,
12   - :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
13   - # getter
14   - def product_category
15   - opportunity_type == 'ProductCategory' ? opportunity : nil
16   - end
17   -
18   - scope :product_categories, {
19   - :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
20   - }
21   -
22   - if defined? SolrPlugin
23   - acts_as_searchable :fields => [
24   - # searched fields
25   - # filtered fields
26   - # ordered/query-boosted fields
27   - ], :include => [
28   - {:product_category => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
29   - ]
30   -
31   - handle_asynchronously :solr_save
32   - end
33   -
34   -end
plugins/sniffer/lib/sniffer_plugin/profile.rb
... ... @@ -1,79 +0,0 @@
1   -class SnifferPlugin::Profile < ActiveRecord::Base
2   -
3   - self.table_name = :sniffer_plugin_profiles
4   -
5   - belongs_to :profile, :class_name => '::Profile'
6   -
7   - has_many :opportunities, :class_name => 'SnifferPlugin::Opportunity', :foreign_key => :profile_id, :dependent => :destroy
8   - has_many :product_categories, :through => :opportunities, :source => :product_category, :foreign_key => :profile_id, :class_name => 'ProductCategory',
9   - :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
10   -
11   - validates_presence_of :profile
12   -
13   - attr_accessible :product_category_string_ids, :enabled
14   -
15   - def self.find_or_create profile
16   - sniffer = SnifferPlugin::Profile.find_by_profile_id profile.id
17   - if sniffer.nil?
18   - sniffer = SnifferPlugin::Profile.new
19   - sniffer.profile = profile
20   - sniffer.enabled = true
21   - sniffer.save!
22   - end
23   - sniffer
24   - end
25   -
26   - def product_category_string_ids
27   - ''
28   - end
29   -
30   - def product_category_string_ids=(ids)
31   - ids = ids.split(',')
32   - self.product_categories = []
33   - self.product_categories = ProductCategory.find(ids)
34   - self.opportunities.
35   - find(:all, :conditions => {:opportunity_id => ids}).each do |o|
36   - o.opportunity_type = 'ProductCategory'
37   - o.save!
38   - end
39   - end
40   -
41   - def profile_input_categories
42   - profile.input_categories
43   - end
44   -
45   - def profile_product_categories
46   - profile.product_categories
47   - end
48   -
49   - def all_categories
50   - (profile_product_categories + profile_input_categories + product_categories).uniq
51   - end
52   -
53   - def suppliers_products
54   - products = []
55   -
56   - products += Product.sniffer_plugin_suppliers_products profile if profile.enterprise?
57   - products += Product.sniffer_plugin_interests_suppliers_products profile
58   - if defined?(CmsLearningPlugin)
59   - products += Product.sniffer_plugin_knowledge_suppliers_inputs profile
60   - products += Product.sniffer_plugin_knowledge_suppliers_interests profile
61   - end
62   -
63   - products
64   - end
65   -
66   - def consumers_products
67   - products = []
68   -
69   - products += Product.sniffer_plugin_consumers_products profile if profile.enterprise?
70   - products += Product.sniffer_plugin_interests_consumers_products profile
71   - if defined?(CmsLearningPlugin)
72   - products += Product.sniffer_plugin_knowledge_consumers_inputs profile
73   - products += Product.sniffer_plugin_knowledge_consumers_interests profile
74   - end
75   -
76   - products
77   - end
78   -
79   -end
plugins/sniffer/models/sniffer_plugin/opportunity.rb 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +class SnifferPlugin::Opportunity < ActiveRecord::Base
  2 +
  3 + self.table_name = :sniffer_plugin_opportunities
  4 +
  5 + belongs_to :profile
  6 +
  7 + belongs_to :opportunity, polymorphic: true
  8 +
  9 + # for has_many :through
  10 + belongs_to :product_category, class_name: 'ProductCategory', foreign_key: :opportunity_id,
  11 + conditions: ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
  12 + # getter
  13 + def product_category
  14 + opportunity_type == 'ProductCategory' ? opportunity : nil
  15 + end
  16 +
  17 + scope :product_categories, {
  18 + conditions: ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
  19 + }
  20 +
  21 + if defined? SolrPlugin
  22 + acts_as_searchable fields: [
  23 + # searched fields
  24 + # filtered fields
  25 + # ordered/query-boosted fields
  26 + ], include: [
  27 + {product_category: {fields: [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
  28 + ]
  29 +
  30 + handle_asynchronously :solr_save
  31 + end
  32 +
  33 + delegate :lat, :lng, :to => :product_category, :allow_nil => true
  34 +
  35 + # delegate missing methods to opportunity
  36 + def method_missing method, *args, &block
  37 + if self.opportunity.respond_to? method
  38 + self.opportunity.send method, *args, &block
  39 + else
  40 + super method, *args, &block
  41 + end
  42 + end
  43 + def respond_to_with_opportunity? method, p2 = true
  44 + respond_to_without_opportunity? method, p2 or (self.opportunity and self.opportunity.respond_to? method)
  45 + end
  46 + alias_method_chain :respond_to?, :opportunity
  47 +
  48 +end
... ...
plugins/sniffer/test/integration/sniffer_map_test.rb
... ... @@ -165,10 +165,9 @@ class SnifferMapTest &lt; ActionDispatch::IntegrationTest
165 165 :identifier => 'acme', :name => 'ACME S.A.', :lat => 0, :lng => 0
166 166 )
167 167 # get the extended sniffer profile for the enterprise:
168   - sniffer_acme = SnifferPlugin::Profile.find_or_create acme
169   - sniffer_acme.product_category_string_ids = "#{@c[1].id},#{@c[4].id}"
170   - sniffer_acme.enabled = true
171   - sniffer_acme.save!
  168 + acme.sniffer_interested_product_category_string_ids = "#{@c[1].id},#{@c[4].id}"
  169 + acme.enabled = true
  170 + acme.save!
172 171  
173 172 # visit the map page:
174 173 get url_plugin_myprofile(acme, :search)
... ...
plugins/sniffer/test/unit/ext/profile_test.rb 0 → 100644
... ... @@ -0,0 +1,96 @@
  1 +require 'test_helper'
  2 +
  3 +class ProfileTest < ActiveSupport::TestCase
  4 +
  5 + should 'register interest on a product category for a profile' do
  6 + # crate an entreprise
  7 + coop = create(Enterprise,
  8 + :identifier => 'coop', :name => 'A Cooperative', :lat => 0, :lng => 0
  9 + )
  10 + # create categories
  11 + c1 = create(ProductCategory, :name => 'Category 1')
  12 + c2 = create(ProductCategory, :name => 'Category 2')
  13 + # get the extended sniffer profile for the enterprise:
  14 + coop.sniffer_interested_product_category_string_ids = "#{c1.id},#{c2.id}"
  15 + coop.enabled = true
  16 + coop.save!
  17 +
  18 + categories = coop.sniffer_interested_product_categories
  19 + assert_equal 2, categories.length
  20 + assert_equal 'Category 1', categories[0].name
  21 + assert_equal 'Category 2', categories[1].name
  22 + end
  23 +
  24 + should 'find suppliers and consumers products' do
  25 + # Enterprises:
  26 + e1 = fast_create(Enterprise, :identifier => 'ent1' )
  27 + e2 = fast_create(Enterprise, :identifier => 'ent2' )
  28 + e3 = fast_create(Enterprise, :identifier => 'ent3' )
  29 + # Categories:
  30 + c1 = fast_create(ProductCategory, :name => 'Category 1')
  31 + c2 = fast_create(ProductCategory, :name => 'Category 2')
  32 + c3 = fast_create(ProductCategory, :name => 'Category 3')
  33 + c4 = fast_create(ProductCategory, :name => 'Category 4') # not used by products
  34 + # Products (for enterprise 1):
  35 + p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
  36 + p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e1.id )
  37 + # Products (for enterprise 2):
  38 + p3 = fast_create(Product, :product_category_id => c3.id, :profile_id => e2.id )
  39 + p3.inputs.build.product_category = c1 # p3 uses p1 as input on its production
  40 + p3.save!
  41 + # Products (for enterprise 3):
  42 + p4 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
  43 + p5 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
  44 + p4.inputs.build.product_category = c1 # p4 uses p1 as input on its production
  45 + p5.inputs.build.product_category = c1 # as does p5
  46 + p4.save!
  47 + p5.save!
  48 +
  49 + # register e2 interest for 'Category 2' use by p2
  50 + e2.sniffer_interested_product_category_string_ids = "#{c2.id},#{c4.id}"
  51 + e2.enabled = true
  52 + e2.save!
  53 +
  54 + assert_equal [p1.id, p1.id, p2.id],
  55 + e1.sniffer_consumers_products.sort_by(&:id).map(&:id)
  56 +
  57 + # since they have interest in the same product, e2 and e3 position
  58 + # may vary here, but the last enterprise should be e2
  59 + assert_equivalent [e2.id, e3.id],
  60 + e1.sniffer_consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.first(2)
  61 + assert_equal e2.id,
  62 + e1.sniffer_consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.last
  63 +
  64 + assert_equal [p1.id, p2.id],
  65 + e2.sniffer_suppliers_products.sort_by(&:id).map(&:id)
  66 + assert_equal [], e2.sniffer_consumers_products
  67 + end
  68 +
  69 + should 'not search for suppliers and consumers on disabled enterprises' do
  70 + # Enterprises:
  71 + e1 = fast_create(Enterprise, :identifier => 'ent1' )
  72 + e2 = fast_create(Enterprise, :identifier => 'ent2' )
  73 + # Categories:
  74 + c1 = fast_create(ProductCategory, :name => 'Category 1')
  75 + c2 = fast_create(ProductCategory, :name => 'Category 2')
  76 + # Products (for enterprise 1):
  77 + p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
  78 +
  79 + # Products (for enterprise 2):
  80 + p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e2.id )
  81 + p2.inputs.build.product_category = c1
  82 + p2.save!
  83 +
  84 + # register e2 interest for 'Category 1' used by p1
  85 + e2.sniffer_interested_product_category_string_ids = "#{c1.id}"
  86 + e2.enabled = true
  87 + e2.save!
  88 +
  89 + # should not find anything for disabled enterprise
  90 + e1.enabled = false
  91 + e1.save!
  92 + assert_equal [], e2.sniffer_consumers_products
  93 + assert_equal [], e2.sniffer_suppliers_products
  94 + end
  95 +
  96 +end
... ...
plugins/sniffer/test/unit/sniffer_plugin_profile_test.rb
... ... @@ -1,106 +0,0 @@
1   -require 'test_helper'
2   -
3   -class SnifferPluginProfileTest < ActiveSupport::TestCase
4   -
5   - should 'register interest on a product category for a profile' do
6   - # crate an entreprise
7   - coop = fast_create(Enterprise,
8   - :identifier => 'coop', :name => 'A Cooperative', :lat => 0, :lng => 0
9   - )
10   - # create categories
11   - c1 = fast_create(ProductCategory, :name => 'Category 1')
12   - c2 = fast_create(ProductCategory, :name => 'Category 2')
13   - # get the extended sniffer profile for the enterprise:
14   - sniffer_coop = SnifferPlugin::Profile.find_or_create coop
15   - sniffer_coop.product_category_string_ids = "#{c1.id},#{c2.id}"
16   - sniffer_coop.enabled = true
17   - sniffer_coop.save!
18   -
19   - # search for and instance again the profile sniffer for coop
20   - same_sniffer = SnifferPlugin::Profile.find_or_create coop
21   -
22   - categories = same_sniffer.product_categories
23   - assert_equal 2, categories.length
24   - assert_equal 'Category 1', categories[0].name
25   - assert_equal 'Category 2', categories[1].name
26   - end
27   -
28   - should 'find suppliers and consumers products' do
29   - # Enterprises:
30   - e1 = fast_create(Enterprise, :identifier => 'ent1' )
31   - e2 = fast_create(Enterprise, :identifier => 'ent2' )
32   - e3 = fast_create(Enterprise, :identifier => 'ent3' )
33   - # Categories:
34   - c1 = fast_create(ProductCategory, :name => 'Category 1')
35   - c2 = fast_create(ProductCategory, :name => 'Category 2')
36   - c3 = fast_create(ProductCategory, :name => 'Category 3')
37   - c4 = fast_create(ProductCategory, :name => 'Category 4') # not used by products
38   - # Products (for enterprise 1):
39   - p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
40   - p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e1.id )
41   - # Products (for enterprise 2):
42   - p3 = fast_create(Product, :product_category_id => c3.id, :profile_id => e2.id )
43   - p3.inputs.build.product_category = c1 # p3 uses p1 as input on its production
44   - p3.save!
45   - # Products (for enterprise 3):
46   - p4 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
47   - p5 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
48   - p4.inputs.build.product_category = c1 # p4 uses p1 as input on its production
49   - p5.inputs.build.product_category = c1 # as does p5
50   - p4.save!
51   - p5.save!
52   -
53   - # get the extended sniffer profile for the enterprise:
54   - e1_sniffer = SnifferPlugin::Profile.find_or_create e1
55   - e2_sniffer = SnifferPlugin::Profile.find_or_create e2
56   - # register e2 interest for 'Category 2' use by p2
57   - e2_sniffer.product_category_string_ids = "#{c2.id},#{c4.id}"
58   - e2_sniffer.enabled = true
59   - e2_sniffer.save!
60   -
61   - assert_equal [p1.id, p1.id, p2.id],
62   - e1_sniffer.consumers_products.sort_by(&:id).map(&:id)
63   -
64   - # since they have interest in the same product, e2 and e3 position
65   - # may vary here, but the last enterprise should be e2
66   - assert_equivalent [e2.id, e3.id],
67   - e1_sniffer.consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.first(2)
68   - assert_equal e2.id,
69   - e1_sniffer.consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.last
70   -
71   - assert_equal [p1.id, p2.id],
72   - e2_sniffer.suppliers_products.sort_by(&:id).map(&:id)
73   - assert_equal [], e2_sniffer.consumers_products
74   - end
75   -
76   - should 'not search for suppliers and consumers on disabled enterprises' do
77   - # Enterprises:
78   - e1 = fast_create(Enterprise, :identifier => 'ent1' )
79   - e2 = fast_create(Enterprise, :identifier => 'ent2' )
80   - # Categories:
81   - c1 = fast_create(ProductCategory, :name => 'Category 1')
82   - c2 = fast_create(ProductCategory, :name => 'Category 2')
83   - # Products (for enterprise 1):
84   - p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
85   -
86   - # Products (for enterprise 2):
87   - p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e2.id )
88   - p2.inputs.build.product_category = c1
89   - p2.save!
90   -
91   - # get the extended sniffer profile for the enterprise:
92   - e1_sniffer = SnifferPlugin::Profile.find_or_create e1
93   - e2_sniffer = SnifferPlugin::Profile.find_or_create e2
94   - # register e2 interest for 'Category 1' used by p1
95   - e2_sniffer.product_category_string_ids = "#{c1.id}"
96   - e2_sniffer.enabled = true
97   - e2_sniffer.save!
98   -
99   - # should not find anything for disabled enterprise
100   - e1.enabled = false
101   - e1.save!
102   - assert_equal [], e2_sniffer.consumers_products
103   - assert_equal [], e2_sniffer.suppliers_products
104   - end
105   -
106   -end
plugins/sniffer/views/sniffer_plugin_myprofile/edit.html.erb
... ... @@ -4,18 +4,21 @@
4 4 <%= _('Select here products and services categories that you have an interest on buying. Then you can go to the Opportunity Sniffer and check out enterprises near you that offer such products. Type in some characters and choose your interests from our list.') %>
5 5 </p>
6 6  
7   -<%= form_for(@sniffer_profile, :url => {:action => 'edit'}, :method => 'post') do |f| %>
  7 +<%= form_for(@profile, as: :profile_data, url: {action: 'edit'}, method: 'post') do |f| %>
8 8  
9 9 <div id='sniffer-plugin-product-select'>
10   - <% current_categories = @sniffer_profile.product_categories.collect{ |i| {:id => i.id, :name => i.name} } %>
  10 + <% current_categories = @profile.sniffer_interested_product_categories.map{ |i| {id: i.id, name: i.name} } %>
11 11  
12   - <%= token_input_field_tag('sniffer_plugin_profile[product_category_string_ids]', 'sniffer_product_category_string_ids', { :action => 'product_category_search' }, {search_delay: 150, pre_populate: current_categories, prevent_duplicates: true, hint_text: _('Type in a keyword') }) %>
  12 + <%= token_input_field_tag('profile_data[sniffer_interested_product_category_string_ids]',
  13 + 'sniffer_interested_product_category_string_ids',
  14 + { action: 'product_category_search' },
  15 + {search_delay: 150, pre_populate: current_categories, prevent_duplicates: true, hint_text: _('Type in a keyword') }) %>
13 16  
14 17 </div>
15 18  
16 19 <% button_bar do %>
17 20 <%= submit_button(:save, _('Save')) %>
18   - <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %>
  21 + <%= button :back, _('Back to control panel'), controller: 'profile_editor' %>
19 22 <% end %>
20 23 <% end %>
21 24  
... ...