From 2ce4aa070dcad44cc73c1c435ac966629ee880c0 Mon Sep 17 00:00:00 2001 From: Isaac Canan Date: Thu, 29 Sep 2011 11:05:53 -0300 Subject: [PATCH] products-activities: Changes to insert notifications to network of products actions (create, update and destroy) --- app/helpers/action_tracker_helper.rb | 18 ++++++++++++++++++ app/models/enterprise.rb | 10 +++++++++- app/models/organization.rb | 4 ++++ app/models/product.rb | 13 +++++++++++++ app/views/profile/_create_product.html.erb | 13 +++++++++++++ app/views/profile/_remove_product.html.erb | 12 ++++++++++++ app/views/profile/_update_product.html.erb | 13 +++++++++++++ config/initializers/action_tracker.rb | 10 ++++++++++ lib/notify_activity_to_profiles_job.rb | 6 ++++++ plugins/bsc/lib/bsc_plugin/ext/product.rb | 10 ++++++++++ test/unit/product_test.rb | 2 +- 11 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 app/views/profile/_create_product.html.erb create mode 100644 app/views/profile/_remove_product.html.erb create mode 100644 app/views/profile/_update_product.html.erb diff --git a/app/helpers/action_tracker_helper.rb b/app/helpers/action_tracker_helper.rb index b1b05af..196d9ad 100644 --- a/app/helpers/action_tracker_helper.rb +++ b/app/helpers/action_tracker_helper.rb @@ -67,4 +67,22 @@ module ActionTrackerHelper } end + def create_product_description + _('created the product %{title}') % { + title: link_to(truncate(ta.get_name), ta.get_url), + } + end + + def update_product_description + _('updated the product %{title}') % { + title: link_to(truncate(ta.get_name), ta.get_url), + } + end + + def remove_product_description + _('removed the product %{title}') % { + title: truncate(ta.get_name), + } + end + end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index e6caf22..5cc43f6 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -15,7 +15,10 @@ class Enterprise < Organization N_('Enterprise') - has_many :products, :foreign_key => :profile_id, :dependent => :destroy, :order => 'name ASC' + acts_as_trackable after_add: proc{ |p, t| notify_activity t } + + has_many :products, :foreign_key => :profile_id, :dependent => :destroy + has_many :product_categories, :through => :products has_many :inputs, :through => :products has_many :production_costs, :as => :owner @@ -202,4 +205,9 @@ class Enterprise < Organization '' end + def followed_by? person + super or self.fans.where(id: person.id).count > 0 + end + + end diff --git a/app/models/organization.rb b/app/models/organization.rb index 17fb7b8..a38285c 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -35,6 +35,10 @@ class Organization < Profile validate :presence_of_required_fieds, :unless => :is_template + def self.notify_activity tracked_action + Delayed::Job.enqueue NotifyActivityToProfilesJob.new(tracked_action.id) + end + def presence_of_required_fieds self.required_fields.each do |field| if self.send(field).blank? diff --git a/app/models/product.rb b/app/models/product.rb index 9401635..1e1f365 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -38,6 +38,10 @@ class Product < ActiveRecord::Base acts_as_having_settings :field => :data + track_actions :create_product, :after_create, :keep_params => [:name, :url ], :if => Proc.new { |a| a.is_trackable? }, :custom_user => :action_tracker_user + track_actions :update_product, :before_update, :keep_params => [:name, :url], :if => Proc.new { |a| a.is_trackable? }, :custom_user => :action_tracker_user + track_actions :remove_product, :before_destroy, :keep_params => [:name], :if => Proc.new { |a| a.is_trackable? }, :custom_user => :action_tracker_user + validates_uniqueness_of :name, :scope => :profile_id, :allow_nil => true, :if => :validate_uniqueness_of_column_name? validates_presence_of :product_category_id @@ -258,4 +262,13 @@ class Product < ActiveRecord::Base true end + def is_trackable? + # shopping_cart create products without profile + self.profile.present? + end + + def action_tracker_user + self.profile + end + end diff --git a/app/views/profile/_create_product.html.erb b/app/views/profile/_create_product.html.erb new file mode 100644 index 0000000..d7021d3 --- /dev/null +++ b/app/views/profile/_create_product.html.erb @@ -0,0 +1,13 @@ +
+ <%= link_to image_tag(activity.target.default_image :minor), activity.target.url, class: 'product-pic' if activity.target.present? %> +
+
+

<%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %>

+

<%= time_ago_as_sentence activity.created_at %>

+ +
+ <%= 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 %> +
+
+ +
diff --git a/app/views/profile/_remove_product.html.erb b/app/views/profile/_remove_product.html.erb new file mode 100644 index 0000000..0ddb381 --- /dev/null +++ b/app/views/profile/_remove_product.html.erb @@ -0,0 +1,12 @@ +
+
+
+

<%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %>

+

<%= time_ago_as_sentence activity.created_at %>

+ +
+ <%= 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 %> +
+
+ +
diff --git a/app/views/profile/_update_product.html.erb b/app/views/profile/_update_product.html.erb new file mode 100644 index 0000000..d7021d3 --- /dev/null +++ b/app/views/profile/_update_product.html.erb @@ -0,0 +1,13 @@ +
+ <%= link_to image_tag(activity.target.default_image :minor), activity.target.url, class: 'product-pic' if activity.target.present? %> +
+
+

<%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %>

+

<%= time_ago_as_sentence activity.created_at %>

+ +
+ <%= 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 %> +
+
+ +
diff --git a/config/initializers/action_tracker.rb b/config/initializers/action_tracker.rb index 6508e41..7d7ab94 100644 --- a/config/initializers/action_tracker.rb +++ b/config/initializers/action_tracker.rb @@ -30,6 +30,16 @@ ActionTrackerConfig.verbs = { reply_scrap_on_self: { }, + + create_product: { + }, + + update_product: { + }, + + remove_product: { + }, + } ActionTrackerConfig.current_user = proc do diff --git a/lib/notify_activity_to_profiles_job.rb b/lib/notify_activity_to_profiles_job.rb index cbc0753..c909f41 100644 --- a/lib/notify_activity_to_profiles_job.rb +++ b/lib/notify_activity_to_profiles_job.rb @@ -22,6 +22,12 @@ class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) # Notify all friends 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})") + if tracked_action.user.is_a? Organization + ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " + + "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " + + 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 + end + if target.is_a?(Community) ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) diff --git a/plugins/bsc/lib/bsc_plugin/ext/product.rb b/plugins/bsc/lib/bsc_plugin/ext/product.rb index d808f48..0572688 100644 --- a/plugins/bsc/lib/bsc_plugin/ext/product.rb +++ b/plugins/bsc/lib/bsc_plugin/ext/product.rb @@ -12,4 +12,14 @@ class Product def display_supplier_on_search? false end + + def action_tracker_user + return self.enterprise if self.enterprise.validated + + if self.enterprise.bsc + self.enterprise.bsc + else + self.enterprise + end + end end diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index 38f8c9f..363fbc6 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -117,7 +117,7 @@ class ProductTest < ActiveSupport::TestCase enterprise.expects(:public_profile_url).returns({}) product.expects(:id).returns(999) - product.expects(:enterprise).returns(enterprise) + product.expects(:profile).returns(enterprise) assert_equal({:controller => 'manage_products', :action => 'show', :id => 999}, product.url) end -- libgit2 0.21.2