Commit 2ce4aa070dcad44cc73c1c435ac966629ee880c0

Authored by Isaac Canan
Committed by Braulio Bhavamitra
1 parent 01e4652f

products-activities: Changes to insert notifications to network of products acti…

…ons (create, update and destroy)
app/helpers/action_tracker_helper.rb
@@ -67,4 +67,22 @@ module ActionTrackerHelper @@ -67,4 +67,22 @@ module ActionTrackerHelper
67 } 67 }
68 end 68 end
69 69
  70 + def create_product_description
  71 + _('created the product %{title}') % {
  72 + title: link_to(truncate(ta.get_name), ta.get_url),
  73 + }
  74 + end
  75 +
  76 + def update_product_description
  77 + _('updated the product %{title}') % {
  78 + title: link_to(truncate(ta.get_name), ta.get_url),
  79 + }
  80 + end
  81 +
  82 + def remove_product_description
  83 + _('removed the product %{title}') % {
  84 + title: truncate(ta.get_name),
  85 + }
  86 + end
  87 +
70 end 88 end
app/models/enterprise.rb
@@ -15,7 +15,10 @@ class Enterprise < Organization @@ -15,7 +15,10 @@ class Enterprise < Organization
15 15
16 N_('Enterprise') 16 N_('Enterprise')
17 17
18 - has_many :products, :foreign_key => :profile_id, :dependent => :destroy, :order => 'name ASC' 18 + acts_as_trackable after_add: proc{ |p, t| notify_activity t }
  19 +
  20 + has_many :products, :foreign_key => :profile_id, :dependent => :destroy
  21 + has_many :product_categories, :through => :products
19 has_many :inputs, :through => :products 22 has_many :inputs, :through => :products
20 has_many :production_costs, :as => :owner 23 has_many :production_costs, :as => :owner
21 24
@@ -202,4 +205,9 @@ class Enterprise < Organization @@ -202,4 +205,9 @@ class Enterprise < Organization
202 '' 205 ''
203 end 206 end
204 207
  208 + def followed_by? person
  209 + super or self.fans.where(id: person.id).count > 0
  210 + end
  211 +
  212 +
205 end 213 end
app/models/organization.rb
@@ -35,6 +35,10 @@ class Organization < Profile @@ -35,6 +35,10 @@ class Organization < Profile
35 35
36 validate :presence_of_required_fieds, :unless => :is_template 36 validate :presence_of_required_fieds, :unless => :is_template
37 37
  38 + def self.notify_activity tracked_action
  39 + Delayed::Job.enqueue NotifyActivityToProfilesJob.new(tracked_action.id)
  40 + end
  41 +
38 def presence_of_required_fieds 42 def presence_of_required_fieds
39 self.required_fields.each do |field| 43 self.required_fields.each do |field|
40 if self.send(field).blank? 44 if self.send(field).blank?
app/models/product.rb
@@ -38,6 +38,10 @@ class Product < ActiveRecord::Base @@ -38,6 +38,10 @@ class Product < ActiveRecord::Base
38 38
39 acts_as_having_settings :field => :data 39 acts_as_having_settings :field => :data
40 40
  41 + track_actions :create_product, :after_create, :keep_params => [:name, :url ], :if => Proc.new { |a| a.is_trackable? }, :custom_user => :action_tracker_user
  42 + track_actions :update_product, :before_update, :keep_params => [:name, :url], :if => Proc.new { |a| a.is_trackable? }, :custom_user => :action_tracker_user
  43 + track_actions :remove_product, :before_destroy, :keep_params => [:name], :if => Proc.new { |a| a.is_trackable? }, :custom_user => :action_tracker_user
  44 +
41 validates_uniqueness_of :name, :scope => :profile_id, :allow_nil => true, :if => :validate_uniqueness_of_column_name? 45 validates_uniqueness_of :name, :scope => :profile_id, :allow_nil => true, :if => :validate_uniqueness_of_column_name?
42 46
43 validates_presence_of :product_category_id 47 validates_presence_of :product_category_id
@@ -258,4 +262,13 @@ class Product < ActiveRecord::Base @@ -258,4 +262,13 @@ class Product < ActiveRecord::Base
258 true 262 true
259 end 263 end
260 264
  265 + def is_trackable?
  266 + # shopping_cart create products without profile
  267 + self.profile.present?
  268 + end
  269 +
  270 + def action_tracker_user
  271 + self.profile
  272 + end
  273 +
261 end 274 end
app/views/profile/_create_product.html.erb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +<div class='profile-activity-image'>
  2 + <%= link_to image_tag(activity.target.default_image :minor), activity.target.url, class: 'product-pic' if activity.target.present? %>
  3 +</div>
  4 +<div class='profile-activity-description'>
  5 + <p class='profile-activity-text'><%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %></p>
  6 + <p class='profile-activity-time'><%= time_ago_as_sentence activity.created_at %></p>
  7 +
  8 + <div class='profile-wall-actions'>
  9 + <%= link_to_function(_('Remove'), 'remove_item_wall(this, \'%s\', \'%s\', \'%s\'); return false ;' % [".profile-activity-item", url_for(:profile => params[:profile], :action => :remove_activity, :activity_id => activity.id, :view => params[:view]), _('Are you sure you want to remove this activity and all its replies?')]) if logged_in? && current_person == @profile %>
  10 + </div>
  11 +</div>
  12 +
  13 +<div style="clear: both"></div>
app/views/profile/_remove_product.html.erb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +<div class='profile-activity-image'>
  2 +</div>
  3 +<div class='profile-activity-description'>
  4 + <p class='profile-activity-text'><%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %></p>
  5 + <p class='profile-activity-time'><%= time_ago_as_sentence activity.created_at %></p>
  6 +
  7 + <div class='profile-wall-actions'>
  8 + <%= link_to_function(_('Remove'), 'remove_item_wall(this, \'%s\', \'%s\', \'%s\'); return false ;' % [".profile-activity-item", url_for(:profile => params[:profile], :action => :remove_activity, :activity_id => activity.id, :view => params[:view]), _('Are you sure you want to remove this activity and all its replies?')]) if logged_in? && current_person == @profile %>
  9 + </div>
  10 +</div>
  11 +
  12 +<div style="clear: both"></div>
app/views/profile/_update_product.html.erb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +<div class='profile-activity-image'>
  2 + <%= link_to image_tag(activity.target.default_image :minor), activity.target.url, class: 'product-pic' if activity.target.present? %>
  3 +</div>
  4 +<div class='profile-activity-description'>
  5 + <p class='profile-activity-text'><%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %></p>
  6 + <p class='profile-activity-time'><%= time_ago_as_sentence activity.created_at %></p>
  7 +
  8 + <div class='profile-wall-actions'>
  9 + <%= link_to_function(_('Remove'), 'remove_item_wall(this, \'%s\', \'%s\', \'%s\'); return false ;' % [".profile-activity-item", url_for(:profile => params[:profile], :action => :remove_activity, :activity_id => activity.id, :view => params[:view]), _('Are you sure you want to remove this activity and all its replies?')]) if logged_in? && current_person == @profile %>
  10 + </div>
  11 +</div>
  12 +
  13 +<div style="clear: both"></div>
config/initializers/action_tracker.rb
@@ -30,6 +30,16 @@ ActionTrackerConfig.verbs = { @@ -30,6 +30,16 @@ ActionTrackerConfig.verbs = {
30 30
31 reply_scrap_on_self: { 31 reply_scrap_on_self: {
32 }, 32 },
  33 +
  34 + create_product: {
  35 + },
  36 +
  37 + update_product: {
  38 + },
  39 +
  40 + remove_product: {
  41 + },
  42 +
33 } 43 }
34 44
35 ActionTrackerConfig.current_user = proc do 45 ActionTrackerConfig.current_user = proc do
lib/notify_activity_to_profiles_job.rb
@@ -22,6 +22,12 @@ class NotifyActivityToProfilesJob &lt; Struct.new(:tracked_action_id) @@ -22,6 +22,12 @@ class NotifyActivityToProfilesJob &lt; Struct.new(:tracked_action_id)
22 # Notify all friends 22 # Notify all friends
23 ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})") 23 ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})")
24 24
  25 + if tracked_action.user.is_a? Organization
  26 + ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " +
  27 + "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " +
  28 + if tracked_action.user.is_a? Enterprise then "union select distinct person_id, #{tracked_action.id} from favorite_enteprises_people where enterprise_id = #{tracked_action.user.id}" else "" end
  29 + end
  30 +
25 if target.is_a?(Community) 31 if target.is_a?(Community)
26 ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) 32 ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
27 33
plugins/bsc/lib/bsc_plugin/ext/product.rb
@@ -12,4 +12,14 @@ class Product @@ -12,4 +12,14 @@ class Product
12 def display_supplier_on_search? 12 def display_supplier_on_search?
13 false 13 false
14 end 14 end
  15 +
  16 + def action_tracker_user
  17 + return self.enterprise if self.enterprise.validated
  18 +
  19 + if self.enterprise.bsc
  20 + self.enterprise.bsc
  21 + else
  22 + self.enterprise
  23 + end
  24 + end
15 end 25 end
test/unit/product_test.rb
@@ -117,7 +117,7 @@ class ProductTest &lt; ActiveSupport::TestCase @@ -117,7 +117,7 @@ class ProductTest &lt; ActiveSupport::TestCase
117 enterprise.expects(:public_profile_url).returns({}) 117 enterprise.expects(:public_profile_url).returns({})
118 118
119 product.expects(:id).returns(999) 119 product.expects(:id).returns(999)
120 - product.expects(:enterprise).returns(enterprise) 120 + product.expects(:profile).returns(enterprise)
121 assert_equal({:controller => 'manage_products', :action => 'show', :id => 999}, product.url) 121 assert_equal({:controller => 'manage_products', :action => 'show', :id => 999}, product.url)
122 end 122 end
123 123