Commit ab8d14e6e0d82d9523a95fc6e654ebd2337b6477

Authored by Braulio Bhavamitra
2 parents 16f70434 2ce4aa07

Merge branch 'products-activities' into 'master'

Add products activities on wall

Depends !473

See merge request !474
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
@@ -17,13 +17,14 @@ class Product < ActiveRecord::Base @@ -17,13 +17,14 @@ class Product < ActiveRecord::Base
17 'full' 17 'full'
18 end 18 end
19 19
20 - belongs_to :enterprise, :foreign_key => :profile_id, :class_name => 'Profile'  
21 belongs_to :profile 20 belongs_to :profile
  21 + # backwards compatibility
  22 + belongs_to :enterprise, :foreign_key => :profile_id, :class_name => 'Profile'
22 alias_method :enterprise=, :profile= 23 alias_method :enterprise=, :profile=
23 alias_method :enterprise, :profile 24 alias_method :enterprise, :profile
24 25
25 - has_one :region, :through => :enterprise  
26 - validates_presence_of :enterprise 26 + has_one :region, :through => :profile
  27 + validates_presence_of :profile
27 28
28 belongs_to :product_category 29 belongs_to :product_category
29 30
@@ -37,6 +38,10 @@ class Product < ActiveRecord::Base @@ -37,6 +38,10 @@ class Product < ActiveRecord::Base
37 38
38 acts_as_having_settings :field => :data 39 acts_as_having_settings :field => :data
39 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 +
40 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?
41 46
42 validates_presence_of :product_category_id 47 validates_presence_of :product_category_id
@@ -54,10 +59,10 @@ class Product < ActiveRecord::Base @@ -54,10 +59,10 @@ class Product < ActiveRecord::Base
54 after_update :save_image 59 after_update :save_image
55 60
56 def lat 61 def lat
57 - self.enterprise.lat 62 + self.profile.lat
58 end 63 end
59 def lng 64 def lng
60 - self.enterprise.lng 65 + self.profile.lng
61 end 66 end
62 67
63 xss_terminate :only => [ :name ], :on => 'validation' 68 xss_terminate :only => [ :name ], :on => 'validation'
@@ -71,7 +76,7 @@ class Product < ActiveRecord::Base @@ -71,7 +76,7 @@ class Product < ActiveRecord::Base
71 filter_iframes :description 76 filter_iframes :description
72 77
73 def iframe_whitelist 78 def iframe_whitelist
74 - enterprise && enterprise.environment && enterprise.environment.trusted_sites_for_iframe 79 + self.profile && self.profile.environment && self.profile.environment.trusted_sites_for_iframe
75 end 80 end
76 81
77 def name 82 def name
@@ -109,16 +114,16 @@ class Product < ActiveRecord::Base @@ -109,16 +114,16 @@ class Product < ActiveRecord::Base
109 end 114 end
110 115
111 def url 116 def url
112 - enterprise.public_profile_url.merge(:controller => 'manage_products', :action => 'show', :id => id) 117 + self.profile.public_profile_url.merge(:controller => 'manage_products', :action => 'show', :id => id)
113 end 118 end
114 119
115 def public? 120 def public?
116 - enterprise.public? 121 + self.profile.public?
117 end 122 end
118 123
119 def formatted_value(method) 124 def formatted_value(method)
120 value = self[method] || self.send(method) 125 value = self[method] || self.send(method)
121 - ("%.2f" % value).to_s.gsub('.', enterprise.environment.currency_separator) if value 126 + ("%.2f" % value).to_s.gsub('.', self.profile.environment.currency_separator) if value
122 end 127 end
123 128
124 def price_with_discount 129 def price_with_discount
@@ -223,16 +228,16 @@ class Product < ActiveRecord::Base @@ -223,16 +228,16 @@ class Product < ActiveRecord::Base
223 end 228 end
224 229
225 def available_production_costs 230 def available_production_costs
226 - self.enterprise.environment.production_costs + self.enterprise.production_costs 231 + self.profile.environment.production_costs + self.profile.production_costs
227 end 232 end
228 233
229 include Rails.application.routes.url_helpers 234 include Rails.application.routes.url_helpers
230 def price_composition_bar_display_url 235 def price_composition_bar_display_url
231 - url_for({:host => enterprise.default_hostname, :controller => 'manage_products', :action => 'display_price_composition_bar', :profile => enterprise.identifier, :id => self.id }.merge(Noosfero.url_options)) 236 + url_for({:host => self.profile.default_hostname, :controller => 'manage_products', :action => 'display_price_composition_bar', :profile => self.profile.identifier, :id => self.id }.merge(Noosfero.url_options))
232 end 237 end
233 238
234 def inputs_cost_update_url 239 def inputs_cost_update_url
235 - url_for({:host => enterprise.default_hostname, :controller => 'manage_products', :action => 'display_inputs_cost', :profile => enterprise.identifier, :id => self.id }.merge(Noosfero.url_options)) 240 + url_for({:host => self.profile.default_hostname, :controller => 'manage_products', :action => 'display_inputs_cost', :profile => self.profile.identifier, :id => self.id }.merge(Noosfero.url_options))
236 end 241 end
237 242
238 def percentage_from_solidarity_economy 243 def percentage_from_solidarity_economy
@@ -249,7 +254,7 @@ class Product < ActiveRecord::Base @@ -249,7 +254,7 @@ class Product < ActiveRecord::Base
249 end 254 end
250 end 255 end
251 256
252 - delegate :enabled, :region, :region_id, :environment, :environment_id, :to => :enterprise 257 + delegate :enabled, :region, :region_id, :environment, :environment_id, :to => :profile, allow_nil: true
253 258
254 protected 259 protected
255 260
@@ -257,4 +262,13 @@ class Product < ActiveRecord::Base @@ -257,4 +262,13 @@ class Product < ActiveRecord::Base
257 true 262 true
258 end 263 end
259 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 +
260 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