[Git][noosfero/noosfero][master] 3 commits: products: Replace enterprise with profile

Bráulio Bhavamitra gitlab at gitlab.com
Tue Aug 11 22:08:43 BRT 2015


Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero


Commits:
01e4652f by Braulio Bhavamitra at 2015-08-11T21:33:12Z
products: Replace enterprise with profile

- - - - -
2ce4aa07 by Isaac Canan at 2015-08-11T21:54:44Z
products-activities: Changes to insert notifications to network of products actions (create, update and destroy)

- - - - -
ab8d14e6 by Bráulio Bhavamitra at 2015-08-12T01:08:16Z
Merge branch 'products-activities' into 'master'

Add products activities on wall

Depends !473

See merge request !474

- - - - -


11 changed files:

- app/helpers/action_tracker_helper.rb
- app/models/enterprise.rb
- app/models/organization.rb
- app/models/product.rb
- + app/views/profile/_create_product.html.erb
- + app/views/profile/_remove_product.html.erb
- + app/views/profile/_update_product.html.erb
- config/initializers/action_tracker.rb
- lib/notify_activity_to_profiles_job.rb
- plugins/bsc/lib/bsc_plugin/ext/product.rb
- test/unit/product_test.rb


Changes:

=====================================
app/helpers/action_tracker_helper.rb
=====================================
--- 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


=====================================
app/models/enterprise.rb
=====================================
--- 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


=====================================
app/models/organization.rb
=====================================
--- 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?


=====================================
app/models/product.rb
=====================================
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -17,13 +17,14 @@ class Product < ActiveRecord::Base
     'full'
   end
 
-  belongs_to :enterprise, :foreign_key => :profile_id, :class_name => 'Profile'
   belongs_to :profile
+  # backwards compatibility
+  belongs_to :enterprise, :foreign_key => :profile_id, :class_name => 'Profile'
   alias_method :enterprise=, :profile=
   alias_method :enterprise, :profile
 
-  has_one :region, :through => :enterprise
-  validates_presence_of :enterprise
+  has_one :region, :through => :profile
+  validates_presence_of :profile
 
   belongs_to :product_category
 
@@ -37,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
@@ -54,10 +59,10 @@ class Product < ActiveRecord::Base
   after_update :save_image
 
   def lat
-    self.enterprise.lat
+    self.profile.lat
   end
   def lng
-    self.enterprise.lng
+    self.profile.lng
   end
 
   xss_terminate :only => [ :name ], :on => 'validation'
@@ -71,7 +76,7 @@ class Product < ActiveRecord::Base
   filter_iframes :description
 
   def iframe_whitelist
-    enterprise && enterprise.environment && enterprise.environment.trusted_sites_for_iframe
+    self.profile && self.profile.environment && self.profile.environment.trusted_sites_for_iframe
   end
 
   def name
@@ -109,16 +114,16 @@ class Product < ActiveRecord::Base
   end
 
   def url
-    enterprise.public_profile_url.merge(:controller => 'manage_products', :action => 'show', :id => id)
+    self.profile.public_profile_url.merge(:controller => 'manage_products', :action => 'show', :id => id)
   end
 
   def public?
-    enterprise.public?
+    self.profile.public?
   end
 
   def formatted_value(method)
     value = self[method] || self.send(method)
-    ("%.2f" % value).to_s.gsub('.', enterprise.environment.currency_separator) if value
+    ("%.2f" % value).to_s.gsub('.', self.profile.environment.currency_separator) if value
   end
 
   def price_with_discount
@@ -223,16 +228,16 @@ class Product < ActiveRecord::Base
   end
 
   def available_production_costs
-    self.enterprise.environment.production_costs + self.enterprise.production_costs
+    self.profile.environment.production_costs + self.profile.production_costs
   end
 
   include Rails.application.routes.url_helpers
   def price_composition_bar_display_url
-    url_for({:host => enterprise.default_hostname, :controller => 'manage_products', :action => 'display_price_composition_bar', :profile => enterprise.identifier, :id => self.id }.merge(Noosfero.url_options))
+    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))
   end
 
   def inputs_cost_update_url
-    url_for({:host => enterprise.default_hostname, :controller => 'manage_products', :action => 'display_inputs_cost', :profile => enterprise.identifier, :id => self.id }.merge(Noosfero.url_options))
+    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))
   end
 
   def percentage_from_solidarity_economy
@@ -249,7 +254,7 @@ class Product < ActiveRecord::Base
         end
   end
 
-  delegate :enabled, :region, :region_id, :environment, :environment_id, :to => :enterprise
+  delegate :enabled, :region, :region_id, :environment, :environment_id, :to => :profile, allow_nil: true
 
   protected
 
@@ -257,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


=====================================
app/views/profile/_create_product.html.erb
=====================================
--- /dev/null
+++ b/app/views/profile/_create_product.html.erb
@@ -0,0 +1,13 @@
+<div class='profile-activity-image'>
+  <%= link_to image_tag(activity.target.default_image :minor), activity.target.url, class: 'product-pic' if activity.target.present? %>
+</div>
+<div class='profile-activity-description'>
+  <p class='profile-activity-text'><%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %></p>
+  <p class='profile-activity-time'><%= time_ago_as_sentence activity.created_at %></p>
+
+  <div class='profile-wall-actions'>
+    <%= 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 %>
+  </div>
+</div>
+
+<div style="clear: both"></div>


=====================================
app/views/profile/_remove_product.html.erb
=====================================
--- /dev/null
+++ b/app/views/profile/_remove_product.html.erb
@@ -0,0 +1,12 @@
+<div class='profile-activity-image'>
+</div>
+<div class='profile-activity-description'>
+  <p class='profile-activity-text'><%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %></p>
+  <p class='profile-activity-time'><%= time_ago_as_sentence activity.created_at %></p>
+
+  <div class='profile-wall-actions'>
+    <%= 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 %>
+  </div>
+</div>
+
+<div style="clear: both"></div>


=====================================
app/views/profile/_update_product.html.erb
=====================================
--- /dev/null
+++ b/app/views/profile/_update_product.html.erb
@@ -0,0 +1,13 @@
+<div class='profile-activity-image'>
+  <%= link_to image_tag(activity.target.default_image :minor), activity.target.url, class: 'product-pic' if activity.target.present? %>
+</div>
+<div class='profile-activity-description'>
+  <p class='profile-activity-text'><%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %></p>
+  <p class='profile-activity-time'><%= time_ago_as_sentence activity.created_at %></p>
+
+  <div class='profile-wall-actions'>
+    <%= 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 %>
+  </div>
+</div>
+
+<div style="clear: both"></div>


=====================================
config/initializers/action_tracker.rb
=====================================
--- 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


=====================================
lib/notify_activity_to_profiles_job.rb
=====================================
--- 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)
 


=====================================
plugins/bsc/lib/bsc_plugin/ext/product.rb
=====================================
--- 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


=====================================
test/unit/product_test.rb
=====================================
--- 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
 



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/16f70434a650efc21b1603f004670bf5faeded4d...ab8d14e6e0d82d9523a95fc6e654ebd2337b6477
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150812/24abec66/attachment-0001.html>


More information about the Noosfero-dev mailing list