Class: Activity
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Activity
- Includes:
- Adva::BelongsToAuthor
- Defined in:
- app/models/activity.rb
Instance Attribute Summary collapse
-
#siblings ⇒ Object
Returns the value of attribute siblings.
Class Method Summary collapse
- .find_coinciding(options = {}) ⇒ Object
- .find_coinciding_grouped_by_dates(*dates) ⇒ Object
- .group_coinciding(activities, delta = nil) ⇒ Object
Instance Method Summary collapse
-
#all_actions ⇒ Object
FIXME should be translated!.
- #coincides_with?(other, delta = nil) ⇒ Boolean
- #from ⇒ Object
- #to ⇒ Object
Methods included from Adva::BelongsToAuthor
Instance Attribute Details
#siblings ⇒ Object
Returns the value of attribute siblings.
24 25 26 |
# File 'app/models/activity.rb', line 24 def siblings @siblings end |
Class Method Details
.find_coinciding(options = {}) ⇒ Object
44 45 46 47 48 49 |
# File 'app/models/activity.rb', line 44 def find_coinciding( = {}) activities = order(created_at: :desc).limit(50) activities = activities.group_by{|r| "#{r.object_type}#{r.object_id}"}.values activities = group_coinciding(activities) activities.sort{|a, b| b.created_at <=> a.created_at } end |
.find_coinciding_grouped_by_dates(*dates) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/activity.rb', line 27 def find_coinciding_grouped_by_dates(*dates) groups = (1..dates.size).collect{[]} activities = find_coinciding #, :include => :user # collect activities for the given dates activities.each do |activity| activity_date = activity.created_at.to_date dates.each_with_index {|date, i| groups[i] << activity and break if activity_date == date } end # remove all found activities from the original resultset groups.each{|group| group.each{ |activity| activities.delete(activity) }} # push remaining resultset as a group itself (i.e. 'the rest of them') groups << activities end |
.group_coinciding(activities, delta = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/activity.rb', line 51 def group_coinciding(activities, delta = nil) activities.inject [] do |chunks, group| chunks << group.shift group.each do |activity| last = chunks.last.siblings.last || chunks.last if last.coincides_with?(activity, delta) chunks.last.siblings << activity else chunks << activity end end chunks end end |
Instance Method Details
#all_actions ⇒ Object
FIXME should be translated!
77 78 79 80 81 82 |
# File 'app/models/activity.rb', line 77 def all_actions actions = Array(siblings.reverse.map(&:actions).compact.flatten) + self.actions previous = nil actions.reject! { |action| (action == previous).tap { previous = action } } actions end |
#coincides_with?(other, delta = nil) ⇒ Boolean
71 72 73 74 |
# File 'app/models/activity.rb', line 71 def coincides_with?(other, delta = nil) delta ||= 1.hour created_at - other.created_at <= delta.to_i end |
#from ⇒ Object
84 85 86 |
# File 'app/models/activity.rb', line 84 def from siblings.last.created_at if siblings.present? end |
#to ⇒ Object
88 89 90 |
# File 'app/models/activity.rb', line 88 def to created_at end |