lib/tekeya/entity.rb in tekeya-0.0.9 vs lib/tekeya/entity.rb in tekeya-0.0.10
- old
+ new
@@ -14,15 +14,18 @@
# define the relation with the activity
has_many :activities, as: :entity, class_name: "::Tekeya::Activity", dependent: :destroy do
# Returns activities dating up to 10 days in the past
def recent
- unless ::Tekeya::Configuration.instance.feed_storage_orm.to_sym == :mongoid
+ c = unless ::Tekeya::Configuration.instance.feed_storage_orm.to_sym == :mongoid
where("created_at > ?", 10.days.ago).order('created_at DESC')
else
criteria.where(:created_at.gte => 10.days.ago).desc('created_at')
end
+
+ c = yield c if block_given?
+ c
end
# Any method missing invoked on activities is considered a new activity
def method_missing(meth, *args, &block)
options = args.extract_options!
@@ -299,17 +302,17 @@
end
# Returns the entity's recent activities
#
# @return [Array] the list of recent activities by this entity
- def profile_feed
+ def profile_feed(&blck)
acts = []
pkey = self.profile_feed_key
recent_activities_count = ::Tekeya.redis.zcard(pkey)
# Check if the cache is not empty
- if recent_activities_count > 0
+ if recent_activities_count > 0 && !block_given?
# Retrieve the aggregate keys from redis
acts_keys = ::Tekeya.redis.zrevrange(pkey, 0, -1)
# Retrieve the aggregates
acts_keys.each do |act_key|
# Make `from_redis` only hit the db if author != entity
@@ -320,11 +323,11 @@
acts << ::Tekeya::Feed::Activity::Item.from_redis(act_key, actor)
end
else
# Retrieve the activities from the DB
- db_recent_activities = self.activities.recent
+ db_recent_activities = self.activities.recent(&blck)
db_recent_activities.each do |activity|
acts << ::Tekeya::Feed::Activity::Item.from_db(activity, activity.author)
end
end
@@ -332,27 +335,33 @@
end
# Returns the entity's feed
#
# @return [Array] the list of activities for the entities tracked by this entity
- def feed
+ def feed(&blck)
acts = []
fkey = self.feed_key
recent_activities_count = ::Tekeya.redis.zcard(fkey)
# Check if the cache is not empty
- if recent_activities_count > 0
+ if recent_activities_count > 0 && !block_given?
# Retrieve the aggregate keys from redis
acts_keys = ::Tekeya.redis.zrevrange(fkey, 0, -1)
# Retrieve the aggregates
acts_keys.each do |act_key|
- acts << ::Tekeya::Feed::Activity::Item.from_redis(act_key, self)
+ # Make `from_redis` only hit the db if author != entity
+ key_components = act_key.split(':')
+ actor = if key_components[4] == self.class.to_s && key_components[5] == self.entity_primary_key
+ self
+ end
+
+ acts << ::Tekeya::Feed::Activity::Item.from_redis(act_key, actor)
end
else
# Retrieve the activities from the DB
(self.tracking + [self]).each do |tracker|
- db_recent_activities = tracker.activities.recent
+ db_recent_activities = tracker.activities.recent(&blck)
db_recent_activities.each do |activity|
acts << ::Tekeya::Feed::Activity::Item.from_db(activity, tracker)
end
end
end
@@ -414,20 +423,22 @@
# @private
# Retrieves rebat relations
def tekeya_relations_of(from, relation_type, entity_type, reverse = false)
result_entity_class = entity_type.safe_constantize if entity_type
- unless reverse
+ relations = unless reverse
::Tekeya.relations.where(from.send(from.class.entity_primary_key), from.class.name, nil, entity_type, relation_type).entries.map do |entry|
- result_entity_class ||= entry.toEntityType.safe_constantize
- result_entity_class.where(:"#{result_entity_class.entity_primary_key}" => entry.toEntityId).first
+ entity_class = result_entity_class || entry.toEntityType.safe_constantize
+ entity_class.where(:"#{entity_class.entity_primary_key}" => entry.toEntityId).first
end
else
::Tekeya.relations.where(nil, entity_type, from.send(from.class.entity_primary_key), from.class.name, relation_type).entries.map do |entry|
- result_entity_class ||= entry.fromEntityType.safe_constantize
- result_entity_class.where(:"#{result_entity_class.entity_primary_key}" => entry.fromEntityId).first
+ entity_class = result_entity_class || entry.fromEntityType.safe_constantize
+ entity_class.where(:"#{entity_class.entity_primary_key}" => entry.fromEntityId).first
end
end
+
+ relations.compact
end
# @private
# Checks if a rebat relation exists
def tekeya_relation_exists?(from, to, type)