Class: Activity

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/activity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#siblingsObject

Returns the value of attribute siblings.



20
21
22
# File 'app/models/activity.rb', line 20

def siblings
  @siblings
end

Class Method Details

.find_coinciding(options = {}) ⇒ Object



40
41
42
43
44
45
# File 'app/models/activity.rb', line 40

def find_coinciding(options = {})
  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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/activity.rb', line 23

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/activity.rb', line 47

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_actionsObject

FIXME should be translated!



73
74
75
76
77
78
# File 'app/models/activity.rb', line 73

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

Returns:

  • (Boolean)


67
68
69
70
# File 'app/models/activity.rb', line 67

def coincides_with?(other, delta = nil)
  delta ||= 1.hour
  created_at - other.created_at <= delta.to_i
end

#fromObject



80
81
82
# File 'app/models/activity.rb', line 80

def from
  siblings.last.created_at if siblings.present?
end

#method_missing_with_object_attributes(name, *args) ⇒ Object



6
7
8
9
10
# File 'app/models/activity.rb', line 6

def method_missing_with_object_attributes(name, *args)
  attrs = self[:object_attributes]
  return attrs[name.to_s] if attrs && attrs.has_key?(name.to_s)
  method_missing_without_object_attributes name, *args
end

#toObject



84
85
86
# File 'app/models/activity.rb', line 84

def to
  created_at
end