lib/tekeya/feed/activity.rb in tekeya-0.0.7 vs lib/tekeya/feed/activity.rb in tekeya-0.0.9
- old
+ new
@@ -3,21 +3,27 @@
module Activity
extend ActiveSupport::Concern
included do
belongs_to :entity, polymorphic: true, autosave: true
+ belongs_to :author, polymorphic: true, autosave: true
has_many :attachments, as: :attache, class_name: 'Tekeya::Attachment'
+ before_create do |act|
+ act.author ||= act.entity
+ end
before_create :group_activities
+
after_create :write_activity_in_redis
after_destroy :delete_activity_from_redis
accepts_nested_attributes_for :attachments
- validates_presence_of :attachments
+ validates_presence_of :attachments, :activity_type
attr_writer :group_with_recent
+ attr_accessible :attachments, :activity_type, :author, :entity, :group_with_recent
end
# Check if this activity is cached in redis
#
# @return [Boolean] true if an aggregate of the activity exists in redis, false otherwise
@@ -38,19 +44,35 @@
return (stamp.to_f / 15.minutes).floor * 15.minutes
else
return current_time_from_proper_timezone.to_i
end
else
- return Time.now.to_i
+ if from_time.present?
+ stamp = from_time.to_i
+
+ # floors the timestamp to the nearest 15 minute
+ return (stamp.to_f / 15.minutes).floor * 15.minutes
+ else
+ return Time.now.to_i
+ end
end
end
# Returns an activity key for usage in caching
#
# @return [String] the activity key
def activity_key
- "activity:#{self.id}:#{self.entity_type}:#{self.entity.send(self.entity.entity_primary_key)}:#{self.activity_type}:#{score}"
+ k = []
+ k[0] = 'activity'
+ k[1] = self.id
+ k[2] = self.entity_type
+ k[3] = self.entity.send(self.entity.entity_primary_key)
+ k[4] = self.author_type
+ k[5] = self.author.send(self.author.entity_primary_key)
+ k[6] = self.activity_type
+ k[7] = score(self.created_at)
+ k.join(':')
end
# @private
#
# returns if the activity should be grouped with similar recent activities
@@ -80,11 +102,13 @@
# @private
# Checks if the activity should be grouped and aborts the creation of a new record
def group_activities
if self.group_with_recent
self.created_at = current_time_from_proper_timezone
- rel = self.class.where(created_at: self.created_at, activity_type: self.activity_type, entity_id: self.entity_id, entity_type: entity_type)
+ rel = self.class.where( created_at: self.created_at, activity_type: self.activity_type,
+ entity_id: self.entity_id, entity_type: entity_type,
+ author_id: self.author_id, author_type: self.author_type)
if rel.count > 0
activity = rel.first
activity.attachments << self.attachments
self.id = activity.id
self.reload
@@ -120,6 +144,6 @@
::Tekeya.redis.sadd(activity_key, attachment)
end
end
end
end
-end
\ No newline at end of file
+end