From a04081a433e589b92d5aae80d715ab80e9c1ce9f Mon Sep 17 00:00:00 2001
From: Braulio Bhavamitra
Date: Sat, 12 Mar 2016 09:35:44 -0300
Subject: [PATCH] sniffer: drop sniffer_plugin_profiles table
---
plugins/sniffer/controllers/sniffer_plugin_myprofile_controller.rb | 52 ++++++++++++++++++++--------------------------------
plugins/sniffer/db/migrate/20131212124106_drop_sniffer_profile_table.rb | 25 +++++++++++++++++++++++++
plugins/sniffer/lib/ext/article.rb | 10 +++++-----
plugins/sniffer/lib/ext/product.rb | 67 ++++++++++++++++++++++++++++++++-----------------------------------
plugins/sniffer/lib/ext/profile.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------
plugins/sniffer/lib/sniffer_plugin/interests_block.rb | 6 +++---
plugins/sniffer/lib/sniffer_plugin/opportunity.rb | 34 ----------------------------------
plugins/sniffer/lib/sniffer_plugin/profile.rb | 79 -------------------------------------------------------------------------------
plugins/sniffer/models/sniffer_plugin/opportunity.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
plugins/sniffer/test/integration/sniffer_map_test.rb | 7 +++----
plugins/sniffer/test/unit/ext/profile_test.rb | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
plugins/sniffer/test/unit/sniffer_plugin_profile_test.rb | 106 ----------------------------------------------------------------------------------------------------------
plugins/sniffer/views/sniffer_plugin_myprofile/edit.html.erb | 11 +++++++----
13 files changed, 289 insertions(+), 308 deletions(-)
create mode 100644 plugins/sniffer/db/migrate/20131212124106_drop_sniffer_profile_table.rb
delete mode 100644 plugins/sniffer/lib/sniffer_plugin/opportunity.rb
delete mode 100644 plugins/sniffer/lib/sniffer_plugin/profile.rb
create mode 100644 plugins/sniffer/models/sniffer_plugin/opportunity.rb
create mode 100644 plugins/sniffer/test/unit/ext/profile_test.rb
delete mode 100644 plugins/sniffer/test/unit/sniffer_plugin_profile_test.rb
diff --git a/plugins/sniffer/controllers/sniffer_plugin_myprofile_controller.rb b/plugins/sniffer/controllers/sniffer_plugin_myprofile_controller.rb
index fbc20ea..262b949 100644
--- a/plugins/sniffer/controllers/sniffer_plugin_myprofile_controller.rb
+++ b/plugins/sniffer/controllers/sniffer_plugin_myprofile_controller.rb
@@ -1,20 +1,13 @@
class SnifferPluginMyprofileController < MyProfileController
- before_filter :fetch_sniffer_profile, :only => [:edit, :search]
-
include SnifferPlugin::Helper
helper SnifferPlugin::Helper
helper CmsHelper
def edit
if request.post?
- begin
- @sniffer_profile.update(params[:sniffer_plugin_profile])
- @sniffer_profile.enabled = true
- @sniffer_profile.save!
+ if @profile.update params[:profile_data]
session[:notice] = _('Consumer interests updated')
- rescue Exception => exception
- flash[:error] = _('Could not save consumer interests')
end
end
end
@@ -22,22 +15,21 @@ class SnifferPluginMyprofileController < MyProfileController
def product_category_search
query = params[:q] || params[:term]
- scope = ProductCategory.by_environment(environment)
- @categories = find_by_contents(:product_categories, @profile, scope, query, {:per_page => 10, :page => 1})[:results]
+ @categories = find_by_contents(:categories, @profile, environment.product_categories, query, {per_page: 10, page: 1})[:results]
autocomplete = params.has_key?(:term)
- render :json => @categories.map { |i| autocomplete ? {:value => i.id, :label => i.name} : {:id => i.id, :name => i.name} }
+ render json: @categories.map { |i| autocomplete ? {value: i.id, label: i.name} : {id: i.id, name: i.name} }
end
def product_category_add
product_category = environment.categories.find params[:id]
- response = { :productCategory => {
+ response = { productCategory: {
:id => product_category.id
}
}
response[:enterprises] = product_category.sniffer_plugin_enterprises.enabled.visible.map do |enterprise|
profile_data = filter_visible_attr_profile(enterprise)
- profile_data[:balloonUrl] = url_for :controller => :sniffer_plugin_myprofile, :action => :map_balloon, :id => enterprise[:id], :escape => false
+ profile_data[:balloonUrl] = url_for controller: :sniffer_plugin_myprofile, action: :map_balloon, id: enterprise[:id], escape: false
profile_data[:sniffer_plugin_distance] = Noosfero::GeoRef.dist(@profile.lat, @profile.lng, enterprise.lat, enterprise.lng)
profile_data[:suppliersProducts] = filter_visible_attr_suppliers_products(
enterprise.products.sniffer_plugin_products_from_category(product_category)
@@ -45,14 +37,14 @@ class SnifferPluginMyprofileController < MyProfileController
profile_data[:consumersProducts] = []
profile_data
end
- render :text => response.to_json
+ render text: response.to_json
end
def search
@no_design_blocks = true
- suppliers_products = @sniffer_profile.suppliers_products
- consumers_products = @sniffer_profile.consumers_products
+ suppliers_products = @profile.sniffer_suppliers_products
+ consumers_products = @profile.sniffer_consumers_products
profiles_of_interest = fetch_profiles(suppliers_products + consumers_products)
@@ -67,13 +59,13 @@ class SnifferPluginMyprofileController < MyProfileController
@profiles_data = {}
suppliers.each do |id, products|
@profiles_data[id] = {
- :profile => profiles_of_interest[id],
- :suppliers_products => products,
- :consumers_products => []
+ profile: profiles_of_interest[id],
+ suppliers_products: products,
+ consumers_products: []
}
end
consumers.each do |id, products|
- @profiles_data[id] ||= { :profile => profiles_of_interest[id] }
+ @profiles_data[id] ||= { profile: profiles_of_interest[id] }
@profiles_data[id][:suppliers_products] ||= []
@profiles_data[id][:consumers_products] = products
end
@@ -91,22 +83,18 @@ class SnifferPluginMyprofileController < MyProfileController
@suppliers = build_products(suppliers_products).values.first
@consumers = build_products(consumers_products).values.first
- render :layout => false
+ render layout: false
end
def my_map_balloon
@categories = @profile.categories
- render :layout => false
+ render layout: false
end
protected
- def fetch_sniffer_profile
- @sniffer_profile = SnifferPlugin::Profile.find_or_create profile
- end
-
def fetch_profiles(products)
- profiles = Profile.all :conditions => {:id => products.map { |p| target_profile_id(p) }}
+ profiles = Profile.all conditions: {id: products.map { |p| target_profile_id(p) }}
profiles_by_id = {}
profiles.each do |p|
p.sniffer_plugin_distance = Noosfero::GeoRef.dist(@profile.lat, @profile.lng, p.lat, p.lng)
@@ -125,9 +113,9 @@ class SnifferPluginMyprofileController < MyProfileController
id_profiles = fetch_profiles(data)
- products = Product.all :conditions => {:id => grab_id.call('id')}, :include => [:enterprise, :product_category]
+ products = Product.all conditions: {id: grab_id.call('id')}, include: [:enterprise, :product_category]
products.each{ |p| id_products[p.id] ||= p }
- knowledges = Article.all :conditions => {:id => grab_id.call('knowledge_id')}
+ knowledges = Article.all conditions: {id: grab_id.call('knowledge_id')}
knowledges.each{ |k| id_knowledges[k.id] ||= k}
data.each do |attributes|
@@ -135,9 +123,9 @@ class SnifferPluginMyprofileController < MyProfileController
results[profile.id] ||= []
results[profile.id] << {
- :partial => attributes['view'],
- :product => id_products[attributes['id'].to_i],
- :knowledge => id_knowledges[attributes['knowledge_id'].to_i]
+ partial: attributes['view'],
+ product: id_products[attributes['id'].to_i],
+ knowledge: id_knowledges[attributes['knowledge_id'].to_i]
}
end
results
diff --git a/plugins/sniffer/db/migrate/20131212124106_drop_sniffer_profile_table.rb b/plugins/sniffer/db/migrate/20131212124106_drop_sniffer_profile_table.rb
new file mode 100644
index 0000000..3fb6d3f
--- /dev/null
+++ b/plugins/sniffer/db/migrate/20131212124106_drop_sniffer_profile_table.rb
@@ -0,0 +1,25 @@
+SnifferPlugin.send :remove_const, :Opportunity if defined? SnifferPlugin::Opportunity
+
+class SnifferPlugin::Profile < ActiveRecord::Base
+ belongs_to :profile
+end
+class SnifferPlugin::Opportunity < ActiveRecord::Base
+ belongs_to :sniffer_profile, class_name: 'SnifferPlugin::Profile', foreign_key: :profile_id
+end
+
+class DropSnifferProfileTable < ActiveRecord::Migration
+ def self.up
+ SnifferPlugin::Opportunity.find_each do |opportunity|
+ sniffer_profile = opportunity.sniffer_profile
+ next unless sniffer_profile.profile
+
+ opportunity.profile_id = sniffer_profile.profile.id
+ opportunity.save!
+ end
+
+ drop_table :sniffer_plugin_profiles
+ end
+
+ def self.down
+ end
+end
diff --git a/plugins/sniffer/lib/ext/article.rb b/plugins/sniffer/lib/ext/article.rb
index 6dfe738..4888a2d 100644
--- a/plugins/sniffer/lib/ext/article.rb
+++ b/plugins/sniffer/lib/ext/article.rb
@@ -5,16 +5,16 @@ class Article
# search for interests of interested that matches the knowledges of wise
scope :sniffer_plugin_knowledges_interests, lambda { |wise, interested|
{
- :select => "op.opportunity_id AS interest_cat,
+ select: "op.opportunity_id AS interest_cat,
articles.name AS knowledge_name, articles.id AS id,
article_resources.resource_id AS knowledge_cat",
- :joins => "INNER JOIN article_resources ON (articles.id = article_resources.article_id)
+ 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 article_resources.resource_type = 'ProductCategory' AND op.opportunity_type = 'ProductCategory')
- INNER JOIN sniffer_plugin_profiles sniffer ON (op.profile_id = sniffer.id AND sniffer.enabled = true)",
- :conditions => "articles.type = 'CmsLearningPlugin::Learning'
+ INNER JOIN profiles sniffer ON op.profile_id = sniffer.id",
+ conditions: "articles.type = 'CmsLearningPlugin::Learning'
AND articles.profile_id = #{wise.id}
- AND sniffer.profile_id = #{interested.id}"
+ AND sniffer.id = #{interested.id}"
}
}
end
diff --git a/plugins/sniffer/lib/ext/product.rb b/plugins/sniffer/lib/ext/product.rb
index 0fa38c1..85321f6 100644
--- a/plugins/sniffer/lib/ext/product.rb
+++ b/plugins/sniffer/lib/ext/product.rb
@@ -13,13 +13,13 @@ class Product
# -> Enterprise 1 as a parameter to this scope would return product B
scope :sniffer_plugin_suppliers_products, lambda { |enterprise|
{
- :select => "DISTINCT products_2.*,
+ select: "DISTINCT products_2.*,
'product' as view",
- :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
+ 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}
+ conditions: "products.profile_id = #{enterprise.id}
AND profiles.public_profile = true AND profiles.visible = true
AND profiles.enabled = true
AND profiles.id <> #{enterprise.id}"
@@ -36,14 +36,14 @@ class Product
# with an extra column `consumer_profile_id` equal to Enterprise 2 id
scope :sniffer_plugin_consumers_products, lambda { |enterprise|
{
- :select => "DISTINCT products_2.*,
+ select: "DISTINCT products_2.*,
profiles.id as consumer_profile_id,
'product' as view",
- :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
+ 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}
+ 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}"
@@ -59,14 +59,14 @@ class Product
# -> 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.*,
+ from: "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' )
+ 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}
+ conditions: "sniffer.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}"
@@ -83,14 +83,13 @@ class Product
# with an extra column `consumer_profile_id` equal to Enterprise 2 id
scope :sniffer_plugin_interests_consumers_products, lambda { |profile|
{
- :select => "DISTINCT products.*,
+ select: "DISTINCT products.*,
profiles.id as consumer_profile_id,
'product' as view",
- :joins => "INNER JOIN categories ON ( categories.id = products.product_category_id )
+ 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}
+ INNER JOIN profiles ON ( op.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}"
@@ -100,14 +99,14 @@ class Product
# knowledge x inputs
scope :sniffer_plugin_knowledge_consumers_inputs, lambda { |profile|
{
- :select => "DISTINCT products.*,
+ select: "DISTINCT products.*,
articles.id AS knowledge_id,
'knowledge' as view",
- :joins => "INNER JOIN inputs ON ( products.id = inputs.product_id )
+ 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'
+ conditions: "articles.type = 'CmsLearningPlugin::Learning'
AND articles.profile_id = #{profile.id}
AND products.profile_id <> #{profile.id}"
}
@@ -116,14 +115,14 @@ class Product
# inputs x knowledge
scope :sniffer_plugin_knowledge_suppliers_inputs, lambda { |profile|
{
- :select => "DISTINCT products.*,
+ 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 )
+ 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'
+ conditions: "articles.type = 'CmsLearningPlugin::Learning'
AND articles.profile_id <> #{profile.id}
AND products.profile_id = #{profile.id}"
}
@@ -132,16 +131,15 @@ class Product
# knowledge x interests
scope :sniffer_plugin_knowledge_consumers_interests, lambda { |profile|
{
- :select => "DISTINCT articles.id AS knowledge_id,
+ 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)
+ 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}
+ INNER JOIN profiles ON ( op.profile_id = profiles.id )",
+ conditions: "articles.profile_id = #{profile.id}
AND profiles.public_profile = true
AND profiles.visible = true
AND profiles.enabled = true
@@ -152,28 +150,27 @@ class Product
# interests x knowledge
scope :sniffer_plugin_knowledge_suppliers_interests, lambda { |profile|
{
- :select => "DISTINCT articles.id AS knowledge_id,
+ 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)
+ 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}
+ 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}"
+ AND profiles.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"
+ conditions: { product_category_id: product_category.id },
+ select: "*, 'product' as view"
}
}
diff --git a/plugins/sniffer/lib/ext/profile.rb b/plugins/sniffer/lib/ext/profile.rb
index 4810d8b..0b287b0 100644
--- a/plugins/sniffer/lib/ext/profile.rb
+++ b/plugins/sniffer/lib/ext/profile.rb
@@ -1,13 +1,57 @@
require_dependency 'profile'
-# WORKAROUND: plugin class don't scope subclasses causing core classes conflict
-require_dependency File.expand_path "#{File.dirname __FILE__}/../../lib/sniffer_plugin/profile"
class Profile
- has_one :sniffer_plugin_profile, :class_name => 'SnifferPlugin::Profile'
- has_many :sniffer_plugin_interests, :source => :product_categories, :through => :sniffer_plugin_profile
- has_many :sniffer_plugin_opportunities, :source => :opportunities, :through => :sniffer_plugin_profile
-
attr_accessor :sniffer_plugin_distance
+ has_many :sniffer_opportunities, :class_name => 'SnifferPlugin::Opportunity', :dependent => :destroy
+ has_many :sniffer_interested_product_categories, :through => :sniffer_opportunities, :source => :product_category, :class_name => 'ProductCategory',
+ :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
+
+ attr_accessor :sniffer_interested_product_category_string_ids
+ descendants.each do |k|
+ k.attr_accessible :sniffer_interested_product_category_string_ids
+ end
+
+ def sniffer_interested_product_category_string_ids
+ ''
+ end
+ def sniffer_interested_product_category_string_ids=(ids)
+ ids = ids.split(',')
+ self.sniffer_interested_product_categories = []
+ r = environment.product_categories.find ids
+ self.sniffer_interested_product_categories = ids.collect{ |id| r.detect {|x| x.id == id.to_i} }
+ self.sniffer_opportunities.where(:opportunity_id => ids).each{|o| o.opportunity_type = 'ProductCategory'; o.save! }
+ end
+
+ def sniffer_categories
+ (self.product_categories + self.input_categories + self.sniffer_interested_product_categories).uniq
+ end
+
+ def sniffer_suppliers_products
+ products = []
+
+ products += Product.sniffer_plugin_suppliers_products self if self.enterprise?
+ products += Product.sniffer_plugin_interests_suppliers_products self
+ if defined?(CmsLearningPlugin)
+ products += Product.sniffer_plugin_knowledge_suppliers_inputs self
+ products += Product.sniffer_plugin_knowledge_suppliers_interests self
+ end
+
+ products
+ end
+
+ def sniffer_consumers_products
+ products = []
+
+ products += Product.sniffer_plugin_consumers_products self if self.enterprise?
+ products += Product.sniffer_plugin_interests_consumers_products self
+ if defined?(CmsLearningPlugin)
+ products += Product.sniffer_plugin_knowledge_consumers_inputs self
+ products += Product.sniffer_plugin_knowledge_consumers_interests self
+ end
+
+ products
+ end
+
end
diff --git a/plugins/sniffer/lib/sniffer_plugin/interests_block.rb b/plugins/sniffer/lib/sniffer_plugin/interests_block.rb
index 1a9b84c..17dda5c 100644
--- a/plugins/sniffer/lib/sniffer_plugin/interests_block.rb
+++ b/plugins/sniffer/lib/sniffer_plugin/interests_block.rb
@@ -18,11 +18,11 @@ class SnifferPlugin::InterestsBlock < Block
def content(args = {})
block = self
+ profile = block.owner
proc do
if block.owner.is_a?(Profile)
- sniffer = SnifferPlugin::Profile.find_or_create(block.owner)
- interests = sniffer.opportunities
- interests |= sniffer.profile.inputs if sniffer.profile.enterprise?
+ interests = profile.snnifer_opportunities
+ interests |= profile.inputs if sniffer.profile.enterprise?
else # Environment
interests = SnifferPlugin::Opportunity.product_categories :limit => 5, :order => 'created_at DESC'
interests += Input.all :limit => 5, :order => 'created_at DESC'
diff --git a/plugins/sniffer/lib/sniffer_plugin/opportunity.rb b/plugins/sniffer/lib/sniffer_plugin/opportunity.rb
deleted file mode 100644
index 02044e7..0000000
--- a/plugins/sniffer/lib/sniffer_plugin/opportunity.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-class SnifferPlugin::Opportunity < ActiveRecord::Base
-
- self.table_name = :sniffer_plugin_opportunities
-
- belongs_to :sniffer_profile, :class_name => 'SnifferPlugin::Profile', :foreign_key => :profile_id
- has_one :profile, :through => :sniffer_profile
-
- belongs_to :opportunity, :polymorphic => true
-
- # for has_many :through
- belongs_to :product_category, :class_name => 'ProductCategory', :foreign_key => :opportunity_id,
- :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
- # getter
- def product_category
- opportunity_type == 'ProductCategory' ? opportunity : nil
- end
-
- scope :product_categories, {
- :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
- }
-
- if defined? SolrPlugin
- acts_as_searchable :fields => [
- # searched fields
- # filtered fields
- # ordered/query-boosted fields
- ], :include => [
- {:product_category => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
- ]
-
- handle_asynchronously :solr_save
- end
-
-end
diff --git a/plugins/sniffer/lib/sniffer_plugin/profile.rb b/plugins/sniffer/lib/sniffer_plugin/profile.rb
deleted file mode 100644
index 741cf1d..0000000
--- a/plugins/sniffer/lib/sniffer_plugin/profile.rb
+++ /dev/null
@@ -1,79 +0,0 @@
-class SnifferPlugin::Profile < ActiveRecord::Base
-
- self.table_name = :sniffer_plugin_profiles
-
- belongs_to :profile, :class_name => '::Profile'
-
- has_many :opportunities, :class_name => 'SnifferPlugin::Opportunity', :foreign_key => :profile_id, :dependent => :destroy
- has_many :product_categories, :through => :opportunities, :source => :product_category, :foreign_key => :profile_id, :class_name => 'ProductCategory',
- :conditions => ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
-
- validates_presence_of :profile
-
- attr_accessible :product_category_string_ids, :enabled
-
- def self.find_or_create profile
- sniffer = SnifferPlugin::Profile.find_by_profile_id profile.id
- if sniffer.nil?
- sniffer = SnifferPlugin::Profile.new
- sniffer.profile = profile
- sniffer.enabled = true
- sniffer.save!
- end
- sniffer
- end
-
- def product_category_string_ids
- ''
- end
-
- def product_category_string_ids=(ids)
- ids = ids.split(',')
- self.product_categories = []
- self.product_categories = ProductCategory.find(ids)
- self.opportunities.
- find(:all, :conditions => {:opportunity_id => ids}).each do |o|
- o.opportunity_type = 'ProductCategory'
- o.save!
- end
- end
-
- def profile_input_categories
- profile.input_categories
- end
-
- def profile_product_categories
- profile.product_categories
- end
-
- def all_categories
- (profile_product_categories + profile_input_categories + product_categories).uniq
- end
-
- def suppliers_products
- products = []
-
- products += Product.sniffer_plugin_suppliers_products profile if profile.enterprise?
- products += Product.sniffer_plugin_interests_suppliers_products profile
- if defined?(CmsLearningPlugin)
- products += Product.sniffer_plugin_knowledge_suppliers_inputs profile
- products += Product.sniffer_plugin_knowledge_suppliers_interests profile
- end
-
- products
- end
-
- def consumers_products
- products = []
-
- products += Product.sniffer_plugin_consumers_products profile if profile.enterprise?
- products += Product.sniffer_plugin_interests_consumers_products profile
- if defined?(CmsLearningPlugin)
- products += Product.sniffer_plugin_knowledge_consumers_inputs profile
- products += Product.sniffer_plugin_knowledge_consumers_interests profile
- end
-
- products
- end
-
-end
diff --git a/plugins/sniffer/models/sniffer_plugin/opportunity.rb b/plugins/sniffer/models/sniffer_plugin/opportunity.rb
new file mode 100644
index 0000000..26b44e8
--- /dev/null
+++ b/plugins/sniffer/models/sniffer_plugin/opportunity.rb
@@ -0,0 +1,48 @@
+class SnifferPlugin::Opportunity < ActiveRecord::Base
+
+ self.table_name = :sniffer_plugin_opportunities
+
+ belongs_to :profile
+
+ belongs_to :opportunity, polymorphic: true
+
+ # for has_many :through
+ belongs_to :product_category, class_name: 'ProductCategory', foreign_key: :opportunity_id,
+ conditions: ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
+ # getter
+ def product_category
+ opportunity_type == 'ProductCategory' ? opportunity : nil
+ end
+
+ scope :product_categories, {
+ conditions: ['sniffer_plugin_opportunities.opportunity_type = ?', 'ProductCategory']
+ }
+
+ if defined? SolrPlugin
+ acts_as_searchable fields: [
+ # searched fields
+ # filtered fields
+ # ordered/query-boosted fields
+ ], include: [
+ {product_category: {fields: [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
+ ]
+
+ handle_asynchronously :solr_save
+ end
+
+ delegate :lat, :lng, :to => :product_category, :allow_nil => true
+
+ # delegate missing methods to opportunity
+ def method_missing method, *args, &block
+ if self.opportunity.respond_to? method
+ self.opportunity.send method, *args, &block
+ else
+ super method, *args, &block
+ end
+ end
+ def respond_to_with_opportunity? method, p2 = true
+ respond_to_without_opportunity? method, p2 or (self.opportunity and self.opportunity.respond_to? method)
+ end
+ alias_method_chain :respond_to?, :opportunity
+
+end
diff --git a/plugins/sniffer/test/integration/sniffer_map_test.rb b/plugins/sniffer/test/integration/sniffer_map_test.rb
index 0aaf3c8..4585cfc 100644
--- a/plugins/sniffer/test/integration/sniffer_map_test.rb
+++ b/plugins/sniffer/test/integration/sniffer_map_test.rb
@@ -165,10 +165,9 @@ class SnifferMapTest < ActionDispatch::IntegrationTest
:identifier => 'acme', :name => 'ACME S.A.', :lat => 0, :lng => 0
)
# get the extended sniffer profile for the enterprise:
- sniffer_acme = SnifferPlugin::Profile.find_or_create acme
- sniffer_acme.product_category_string_ids = "#{@c[1].id},#{@c[4].id}"
- sniffer_acme.enabled = true
- sniffer_acme.save!
+ acme.sniffer_interested_product_category_string_ids = "#{@c[1].id},#{@c[4].id}"
+ acme.enabled = true
+ acme.save!
# visit the map page:
get url_plugin_myprofile(acme, :search)
diff --git a/plugins/sniffer/test/unit/ext/profile_test.rb b/plugins/sniffer/test/unit/ext/profile_test.rb
new file mode 100644
index 0000000..36889f9
--- /dev/null
+++ b/plugins/sniffer/test/unit/ext/profile_test.rb
@@ -0,0 +1,96 @@
+require 'test_helper'
+
+class ProfileTest < ActiveSupport::TestCase
+
+ should 'register interest on a product category for a profile' do
+ # crate an entreprise
+ coop = create(Enterprise,
+ :identifier => 'coop', :name => 'A Cooperative', :lat => 0, :lng => 0
+ )
+ # create categories
+ c1 = create(ProductCategory, :name => 'Category 1')
+ c2 = create(ProductCategory, :name => 'Category 2')
+ # get the extended sniffer profile for the enterprise:
+ coop.sniffer_interested_product_category_string_ids = "#{c1.id},#{c2.id}"
+ coop.enabled = true
+ coop.save!
+
+ categories = coop.sniffer_interested_product_categories
+ assert_equal 2, categories.length
+ assert_equal 'Category 1', categories[0].name
+ assert_equal 'Category 2', categories[1].name
+ end
+
+ should 'find suppliers and consumers products' do
+ # Enterprises:
+ e1 = fast_create(Enterprise, :identifier => 'ent1' )
+ e2 = fast_create(Enterprise, :identifier => 'ent2' )
+ e3 = fast_create(Enterprise, :identifier => 'ent3' )
+ # Categories:
+ c1 = fast_create(ProductCategory, :name => 'Category 1')
+ c2 = fast_create(ProductCategory, :name => 'Category 2')
+ c3 = fast_create(ProductCategory, :name => 'Category 3')
+ c4 = fast_create(ProductCategory, :name => 'Category 4') # not used by products
+ # Products (for enterprise 1):
+ p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
+ p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e1.id )
+ # Products (for enterprise 2):
+ p3 = fast_create(Product, :product_category_id => c3.id, :profile_id => e2.id )
+ p3.inputs.build.product_category = c1 # p3 uses p1 as input on its production
+ p3.save!
+ # Products (for enterprise 3):
+ p4 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
+ p5 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
+ p4.inputs.build.product_category = c1 # p4 uses p1 as input on its production
+ p5.inputs.build.product_category = c1 # as does p5
+ p4.save!
+ p5.save!
+
+ # register e2 interest for 'Category 2' use by p2
+ e2.sniffer_interested_product_category_string_ids = "#{c2.id},#{c4.id}"
+ e2.enabled = true
+ e2.save!
+
+ assert_equal [p1.id, p1.id, p2.id],
+ e1.sniffer_consumers_products.sort_by(&:id).map(&:id)
+
+ # since they have interest in the same product, e2 and e3 position
+ # may vary here, but the last enterprise should be e2
+ assert_equivalent [e2.id, e3.id],
+ e1.sniffer_consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.first(2)
+ assert_equal e2.id,
+ e1.sniffer_consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.last
+
+ assert_equal [p1.id, p2.id],
+ e2.sniffer_suppliers_products.sort_by(&:id).map(&:id)
+ assert_equal [], e2.sniffer_consumers_products
+ end
+
+ should 'not search for suppliers and consumers on disabled enterprises' do
+ # Enterprises:
+ e1 = fast_create(Enterprise, :identifier => 'ent1' )
+ e2 = fast_create(Enterprise, :identifier => 'ent2' )
+ # Categories:
+ c1 = fast_create(ProductCategory, :name => 'Category 1')
+ c2 = fast_create(ProductCategory, :name => 'Category 2')
+ # Products (for enterprise 1):
+ p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
+
+ # Products (for enterprise 2):
+ p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e2.id )
+ p2.inputs.build.product_category = c1
+ p2.save!
+
+ # register e2 interest for 'Category 1' used by p1
+ e2.sniffer_interested_product_category_string_ids = "#{c1.id}"
+ e2.enabled = true
+ e2.save!
+
+ # should not find anything for disabled enterprise
+ e1.enabled = false
+ e1.save!
+ assert_equal [], e2.sniffer_consumers_products
+ assert_equal [], e2.sniffer_suppliers_products
+ end
+
+end
diff --git a/plugins/sniffer/test/unit/sniffer_plugin_profile_test.rb b/plugins/sniffer/test/unit/sniffer_plugin_profile_test.rb
deleted file mode 100644
index bb9711e..0000000
--- a/plugins/sniffer/test/unit/sniffer_plugin_profile_test.rb
+++ /dev/null
@@ -1,106 +0,0 @@
-require 'test_helper'
-
-class SnifferPluginProfileTest < ActiveSupport::TestCase
-
- should 'register interest on a product category for a profile' do
- # crate an entreprise
- coop = fast_create(Enterprise,
- :identifier => 'coop', :name => 'A Cooperative', :lat => 0, :lng => 0
- )
- # create categories
- c1 = fast_create(ProductCategory, :name => 'Category 1')
- c2 = fast_create(ProductCategory, :name => 'Category 2')
- # get the extended sniffer profile for the enterprise:
- sniffer_coop = SnifferPlugin::Profile.find_or_create coop
- sniffer_coop.product_category_string_ids = "#{c1.id},#{c2.id}"
- sniffer_coop.enabled = true
- sniffer_coop.save!
-
- # search for and instance again the profile sniffer for coop
- same_sniffer = SnifferPlugin::Profile.find_or_create coop
-
- categories = same_sniffer.product_categories
- assert_equal 2, categories.length
- assert_equal 'Category 1', categories[0].name
- assert_equal 'Category 2', categories[1].name
- end
-
- should 'find suppliers and consumers products' do
- # Enterprises:
- e1 = fast_create(Enterprise, :identifier => 'ent1' )
- e2 = fast_create(Enterprise, :identifier => 'ent2' )
- e3 = fast_create(Enterprise, :identifier => 'ent3' )
- # Categories:
- c1 = fast_create(ProductCategory, :name => 'Category 1')
- c2 = fast_create(ProductCategory, :name => 'Category 2')
- c3 = fast_create(ProductCategory, :name => 'Category 3')
- c4 = fast_create(ProductCategory, :name => 'Category 4') # not used by products
- # Products (for enterprise 1):
- p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
- p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e1.id )
- # Products (for enterprise 2):
- p3 = fast_create(Product, :product_category_id => c3.id, :profile_id => e2.id )
- p3.inputs.build.product_category = c1 # p3 uses p1 as input on its production
- p3.save!
- # Products (for enterprise 3):
- p4 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
- p5 = fast_create(Product, :product_category_id => c3.id, :profile_id => e3.id )
- p4.inputs.build.product_category = c1 # p4 uses p1 as input on its production
- p5.inputs.build.product_category = c1 # as does p5
- p4.save!
- p5.save!
-
- # get the extended sniffer profile for the enterprise:
- e1_sniffer = SnifferPlugin::Profile.find_or_create e1
- e2_sniffer = SnifferPlugin::Profile.find_or_create e2
- # register e2 interest for 'Category 2' use by p2
- e2_sniffer.product_category_string_ids = "#{c2.id},#{c4.id}"
- e2_sniffer.enabled = true
- e2_sniffer.save!
-
- assert_equal [p1.id, p1.id, p2.id],
- e1_sniffer.consumers_products.sort_by(&:id).map(&:id)
-
- # since they have interest in the same product, e2 and e3 position
- # may vary here, but the last enterprise should be e2
- assert_equivalent [e2.id, e3.id],
- e1_sniffer.consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.first(2)
- assert_equal e2.id,
- e1_sniffer.consumers_products.sort_by(&:id).map{|p| p[:consumer_profile_id].to_i}.last
-
- assert_equal [p1.id, p2.id],
- e2_sniffer.suppliers_products.sort_by(&:id).map(&:id)
- assert_equal [], e2_sniffer.consumers_products
- end
-
- should 'not search for suppliers and consumers on disabled enterprises' do
- # Enterprises:
- e1 = fast_create(Enterprise, :identifier => 'ent1' )
- e2 = fast_create(Enterprise, :identifier => 'ent2' )
- # Categories:
- c1 = fast_create(ProductCategory, :name => 'Category 1')
- c2 = fast_create(ProductCategory, :name => 'Category 2')
- # Products (for enterprise 1):
- p1 = fast_create(Product, :product_category_id => c1.id, :profile_id => e1.id )
-
- # Products (for enterprise 2):
- p2 = fast_create(Product, :product_category_id => c2.id, :profile_id => e2.id )
- p2.inputs.build.product_category = c1
- p2.save!
-
- # get the extended sniffer profile for the enterprise:
- e1_sniffer = SnifferPlugin::Profile.find_or_create e1
- e2_sniffer = SnifferPlugin::Profile.find_or_create e2
- # register e2 interest for 'Category 1' used by p1
- e2_sniffer.product_category_string_ids = "#{c1.id}"
- e2_sniffer.enabled = true
- e2_sniffer.save!
-
- # should not find anything for disabled enterprise
- e1.enabled = false
- e1.save!
- assert_equal [], e2_sniffer.consumers_products
- assert_equal [], e2_sniffer.suppliers_products
- end
-
-end
diff --git a/plugins/sniffer/views/sniffer_plugin_myprofile/edit.html.erb b/plugins/sniffer/views/sniffer_plugin_myprofile/edit.html.erb
index cbad7f4..b3c68bb 100644
--- a/plugins/sniffer/views/sniffer_plugin_myprofile/edit.html.erb
+++ b/plugins/sniffer/views/sniffer_plugin_myprofile/edit.html.erb
@@ -4,18 +4,21 @@
<%= _('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.') %>
-<%= form_for(@sniffer_profile, :url => {:action => 'edit'}, :method => 'post') do |f| %>
+<%= form_for(@profile, as: :profile_data, url: {action: 'edit'}, method: 'post') do |f| %>
- <% current_categories = @sniffer_profile.product_categories.collect{ |i| {:id => i.id, :name => i.name} } %>
+ <% current_categories = @profile.sniffer_interested_product_categories.map{ |i| {id: i.id, name: i.name} } %>
- <%= 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') }) %>
+ <%= token_input_field_tag('profile_data[sniffer_interested_product_category_string_ids]',
+ 'sniffer_interested_product_category_string_ids',
+ { action: 'product_category_search' },
+ {search_delay: 150, pre_populate: current_categories, prevent_duplicates: true, hint_text: _('Type in a keyword') }) %>
<% button_bar do %>
<%= submit_button(:save, _('Save')) %>
- <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %>
+ <%= button :back, _('Back to control panel'), controller: 'profile_editor' %>
<% end %>
<% end %>
--
libgit2 0.21.2