[Git][noosfero/noosfero][master] 2 commits: html_safe: fix action tracker description mess

Bráulio Bhavamitra gitlab at gitlab.com
Sun Aug 9 20:36:33 BRT 2015


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


Commits:
530e66dd by Braulio Bhavamitra at 2015-08-06T13:55:41Z
html_safe: fix action tracker description mess

- - - - -
31430d6c by Bráulio Bhavamitra at 2015-08-09T23:35:31Z
Merge branch 'fix-action-tracker-mess' into 'master'

html_safe: fix action tracker description mess

See merge request !647

- - - - -


7 changed files:

- app/controllers/public/profile_controller.rb
- + app/helpers/action_tracker_helper.rb
- app/models/person_notifier.rb
- app/views/person_notifier/mailer/_default_activity.html.erb
- config/initializers/action_tracker.rb
- test/unit/person_notifier_test.rb
- vendor/plugins/action_tracker/lib/action_tracker.rb


Changes:

=====================================
app/controllers/public/profile_controller.rb
=====================================
--- a/app/controllers/public/profile_controller.rb
+++ b/app/controllers/public/profile_controller.rb
@@ -6,6 +6,7 @@ class ProfileController < PublicController
   before_filter :login_required, :only => [:add, :join, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity, :send_mail]
 
   helper TagsHelper
+  helper ActionTrackerHelper
 
   protect 'send_mail_to_members', :profile, :only => [:send_mail]
 


=====================================
app/helpers/action_tracker_helper.rb
=====================================
--- /dev/null
+++ b/app/helpers/action_tracker_helper.rb
@@ -0,0 +1,70 @@
+module ActionTrackerHelper
+
+  def create_article_description ta
+    _('published an article: %{title}') % { title: link_to(truncate(ta.get_name), ta.get_url) }
+  end
+
+  def new_friendship_description ta
+    n_('has made 1 new friend:<br />%{name}', 'has made %{num} new friends:<br />%{name}', ta.get_friend_name.size) % {
+      num: ta.get_friend_name.size,
+      name: ta.collect_group_with_index(:friend_name) do |n,i|
+        link_to image_tag(ta.get_friend_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/person-icon.png")),
+                ta.get_friend_url[i], title: n
+      end.join
+    }
+  end
+
+  def join_community_description ta
+    n_('has joined 1 community:<br />%{name}', 'has joined %{num} communities:<br />%{name}', ta.get_resource_name.size) % {
+      num: ta.get_resource_name.size,
+      name: ta.collect_group_with_index(:resource_name) do |n,i|
+        link_to image_tag(ta.get_resource_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/community-icon.png")),
+                ta.get_resource_url[i], title: n
+      end.join
+    }
+  end
+
+  def add_member_in_community_description ta
+    _('has joined the community.')
+  end
+
+  def upload_image_description ta
+    total = ta.get_view_url.size
+    (n_('uploaded 1 image', 'uploaded %d images', total) % total) +
+      tag(:br) +
+      ta.collect_group_with_index(:thumbnail_path) do |t,i|
+        if total == 1
+          link_to image_tag(t), ta.get_view_url[i], class: 'upimg'
+        else
+          pos = total-i;
+          morethen2 = pos>2 ? 'morethen2' : ''
+          morethen5 = pos>5 ? 'morethen5' : ''
+          t = t.gsub(/(.*)(display)(.*)/, '\\1thumb\\3')
+
+          link_to ' '.html_safe, ta.get_view_url[i],
+            style: "background-image:url(#{t})",
+            class: "upimg pos#{pos} #{morethen2} #{morethen5}"
+        end
+      end.reverse.join +
+      if total <= 5 then ''.html_safe else content_tag :span, '…'.html_safe,
+        class: 'more', onclick: "this.parentNode.className+=' show-all'" end +
+      tag(:br, style: 'clear: both')
+  end
+
+  def reply_scrap_description ta
+    _('sent a message to %{receiver}: <br /> "%{message}"') % {
+      receiver: link_to(ta.get_receiver_name, ta.get_receiver_url),
+      message: auto_link_urls(ta.get_content)
+    }
+  end
+
+  alias :leave_scrap_description :reply_scrap_description
+  alias :reply_scrap_on_self_description :reply_scrap_description
+
+  def leave_scrap_to_self_description ta
+    _('wrote: <br /> "%{text}"') % {
+      text: auto_link_urls(ta.get_content)
+    }
+  end
+
+end


=====================================
app/models/person_notifier.rb
=====================================
--- a/app/models/person_notifier.rb
+++ b/app/models/person_notifier.rb
@@ -76,7 +76,8 @@ class PersonNotifier
 
   class Mailer < ActionMailer::Base
 
-    add_template_helper(ApplicationHelper)
+    helper ApplicationHelper
+    helper ActionTrackerHelper
 
     def session
       {:theme => nil}


=====================================
app/views/person_notifier/mailer/_default_activity.html.erb
=====================================
--- a/app/views/person_notifier/mailer/_default_activity.html.erb
+++ b/app/views/person_notifier/mailer/_default_activity.html.erb
@@ -4,7 +4,10 @@
   </td>
   <td>
     <p>
-      <span style="font-size: 14px;"><%= link_to activity.user.name, activity.user.url %> <%= describe activity %></span>
+      <span style="font-size: 14px;">
+        <%= link_to activity.user.name, activity.user.url %>
+        <%= describe activity %>
+      </span>
       <span style="font-size: 10px; color: #929292; float: right;"><%= time_ago_in_words(activity.created_at) %></span>
     </p>
   </td>


=====================================
config/initializers/action_tracker.rb
=====================================
--- a/config/initializers/action_tracker.rb
+++ b/config/initializers/action_tracker.rb
@@ -2,64 +2,33 @@ require 'noosfero/i18n'
 
 # ActionTracker plugin stuff
 
- at reply_scrap_description = proc { _('sent a message to %{receiver}: <br /> "%{message}"') % { :receiver => "{{link_to(ta.get_receiver_name, ta.get_receiver_url)}}", :message => "{{auto_link_urls(ta.get_content)}}" } }
-
 ActionTrackerConfig.verbs = {
 
-  :create_article => { 
-    :description => proc { _('published an article: %{title}') % { :title => '{{link_to(truncate(ta.get_name), ta.get_url)}}' } }
+  create_article: {
   },
 
-  :new_friendship => { 
-    :description => proc { n_('has made 1 new friend:<br />%{name}', 'has made %{num} new friends:<br />%{name}', get_friend_name.size) % { :num => get_friend_name.size, :name => '{{ta.collect_group_with_index(:friend_name){ |n,i| link_to(image_tag(ta.get_friend_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/person-icon.png")), ta.get_friend_url[i], :title => n)}.join}}' } },
-    :type => :groupable
+  new_friendship: {
+    type: :groupable
   },
 
-  :join_community => { 
-    :description => proc { n_('has joined 1 community:<br />%{name}', 'has joined %{num} communities:<br />%{name}', get_resource_name.size) % { :num => get_resource_name.size, :name => '{{ta.collect_group_with_index(:resource_name){ |n,i| link_to(image_tag(ta.get_resource_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/community-icon.png")), ta.get_resource_url[i], :title => n)}.join}}' } },
-    :type => :groupable
+  join_community: {
+    type: :groupable
   },
 
-  :add_member_in_community => { 
-    :description => proc { _('has joined the community.') },
+  add_member_in_community: {
   },
 
-  :upload_image => { 
-    :description => proc do
-      total = get_view_url.size
-      n_('uploaded 1 image', 'uploaded %d images', total) % total +
-      '<br />{{'+
-      'ta.collect_group_with_index(:thumbnail_path) { |t,i|' +
-      "  if ( #{total} == 1 );" +
-      '    link_to( image_tag(t), ta.get_view_url[i], :class => \'upimg\' );' +
-      '   else;' +
-      "    pos = #{total}-i;" +
-      '    morethen2 = pos>2 ? \'morethen2\' : \'\';' +
-      '    morethen5 = pos>5 ? \'morethen5\' : \'\';' +
-      '    t = t.gsub(/(.*)(display)(.*)/, \'\\1thumb\\3\');' +
-      '    link_to( \' \', ta.get_view_url[i],' +
-      '      :style => "background-image:url(#{t})",' +
-      '      :class => "upimg pos#{pos} #{morethen2} #{morethen5}" );' +
-      '  end' +
-      '}.reverse.join}}' +
-      ( total > 5 ?
-        '<span class="more" onclick="this.parentNode.className+=\' show-all\'">' +
-        '…</span>' : '' ) +
-      '<br style="clear: both;" />'
-    end,
-    :type => :groupable
+  upload_image: {
+    type: :groupable
   },
 
-  :leave_scrap => {
-    :description => @reply_scrap_description
+  leave_scrap: {
   },
 
-  :leave_scrap_to_self => {
-    :description => proc { _('wrote: <br /> "%{text}"') % { :text => "{{auto_link_urls(ta.get_content)}}" } }
+  leave_scrap_to_self: {
   },
 
-  :reply_scrap_on_self => {
-    :description => @reply_scrap_description
+  reply_scrap_on_self: {
   },
 }
 


=====================================
test/unit/person_notifier_test.rb
=====================================
--- a/test/unit/person_notifier_test.rb
+++ b/test/unit/person_notifier_test.rb
@@ -162,23 +162,31 @@ class PersonNotifierTest < ActiveSupport::TestCase
 
   ActionTrackerConfig.verb_names.each do |verb|
     should "render notification for verb #{verb}" do
-      action = mock()
-      action.stubs(:verb).returns(verb)
-      action.stubs(:user).returns(@member)
-      action.stubs(:created_at).returns(DateTime.now)
-      action.stubs(:target).returns(fast_create(Forum))
-      action.stubs(:comments_count).returns(0)
-      action.stubs(:comments).returns([])
-      action.stubs(:params).returns({'name' => 'home', 'url' => '/', 'lead' => ''})
-      action.stubs(:get_url).returns('')
-
-      notifications = []
-      notifications.stubs(:find).returns([action])
-      Person.any_instance.stubs(:tracked_notifications).returns(notifications)
-
-      notify
-      sent = ActionMailer::Base.deliveries.last
-      assert_no_match /cannot render notification for #{verb}/, sent.body.to_s
+      @member.tracked_notifications = []
+
+      a = @member.tracked_notifications.build
+      a.verb = verb
+      a.user = @member
+      a.created_at = @member.notifier.notify_from + 1.day
+      a.target = fast_create(Forum)
+      a.comments_count = 0
+      a.params = {
+        'name' => 'home', 'url' => '/', 'lead' => '',
+        'receiver_url' => '/', 'content' => 'nothing',
+        'friend_url' => '/', 'friend_profile_custom_icon' => [], 'friend_name' => ['joe'],
+        'resource_name' => ['resource'], 'resource_profile_custom_icon' => [], 'resource_url' => ['/'],
+        'view_url'=> ['/'], 'thumbnail_path' => ['1'],
+      }
+      a.get_url = ''
+      a.save!
+      n = @member.action_tracker_notifications.build
+      n.action_tracker = a
+      n.profile = @member
+      n.save!
+
+      assert_nothing_raised do
+        notify
+      end
     end
   end
 


=====================================
vendor/plugins/action_tracker/lib/action_tracker.rb
=====================================
--- a/vendor/plugins/action_tracker/lib/action_tracker.rb
+++ b/vendor/plugins/action_tracker/lib/action_tracker.rb
@@ -8,21 +8,21 @@ module ActionTracker
       base.send :user_stamp, ActionTracker::Record
       base.send :extend, ClassMethods
     end
-  
+
     module ClassMethods
 
       def track_actions_after(verb, options = {}, &block)
         track_actions_by_time(verb, :after, options, &block)
       end
-  
+
       def track_actions_before(verb, options = {}, &block)
         track_actions_by_time(verb, :before, options, &block)
       end
-  
+
       def track_actions(verb, options = {}, &block)
         track_actions_by_time(verb, ActionTrackerConfig.default_filter_time, options, &block)
       end
-  
+
       def track_actions_by_time(verb, time, options = {}, &block)
         keep_params = options.delete(:keep_params) || options.delete('keep_params') || :all
         send("#{time}_filter", options) do |x|
@@ -32,7 +32,7 @@ module ActionTracker
         send :include, InstanceMethods
       end
     end
-  
+
     module InstanceMethods
       def save_action_for_verb(verb, keep_params = :all)
         if keep_params.is_a? Array
@@ -62,7 +62,7 @@ module ActionTracker
     def self.included(base)
       base.send :extend, ClassMethods
     end
-  
+
     module ClassMethods
       def track_actions(verb, callback, options = {}, &block)
         keep_params = options.delete(:keep_params) || options.delete('keep_params') || :all
@@ -78,11 +78,11 @@ module ActionTracker
         send :include, InstanceMethods
       end
     end
-  
+
     module InstanceMethods
       def time_spent_doing(verb, conditions = {})
         time = 0
-        tracked_actions.all(:conditions => conditions.merge({ :verb => verb.to_s })).each do |t| 
+        tracked_actions.all(:conditions => conditions.merge({ :verb => verb.to_s })).each do |t|
           time += t.updated_at - t.created_at
         end
         time.to_f
@@ -124,13 +124,7 @@ module ActionTracker
 
   module ViewHelper
     def describe(ta)
-      "".tap do |result|
-        if ta.is_a?(ActionTracker::Record)
-          result << ta.description.gsub(/\{\{(.*?)\}\}/) { eval $1 }
-        else
-          result << ""
-        end
-      end
+      send "#{ta.verb}_description", ta if ta.is_a? ActionTracker::Record
     end
   end
 



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/2f07775bdd470f71031627414598fa28a6563b94...31430d6c303b3d371071089001aa1eb6c73957b2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150809/979cd1de/attachment-0001.html>


More information about the Noosfero-dev mailing list