Class: SimpleForum::UserActivity

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

Defined Under Namespace

Classes: Checker

Class Method Summary collapse

Class Method Details

.bang(object, user) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/models/simple_forum/user_activity.rb', line 63

def self.bang(object, user)
  if am = load(object, user)
    update_all({:read_at => Time.now}, {:id => am.id})
  else
    am = create_for(object, user)
  end
  am
end

.create_for(object, user, time = Time.now) ⇒ Object



78
79
80
81
82
83
84
# File 'app/models/simple_forum/user_activity.rb', line 78

def self.create_for(object, user, time=Time.now)
  SimpleForum::UserActivity.create do |m|
    m.memoryable = object
    m.user = user
    m.read_at = time
  end
end

.load(object, user, conditions = nil) ⇒ Object



72
73
74
75
76
# File 'app/models/simple_forum/user_activity.rb', line 72

def self.load(object, user, conditions=nil)
  scope = self.where({:memoryable_type => object.class.name, :memoryable_id => object.id, :user_id => user.id})
  scope = scope.where(conditions) if conditions
  scope.first
end

.recent_activity_for_user(user) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'app/models/simple_forum/user_activity.rb', line 42

def self.recent_activity_for_user(user)
  if user && user.persisted?
    u = user
    hash = self.where(:user_id => user.try(:id)).to_hash
  else
    u = nil
    hash = {}
  end
  Checker.new(u, hash)
end

.to_hash(collection = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/models/simple_forum/user_activity.rb', line 53

def self.to_hash(collection=nil)
  collection ||= only_read.all
  {}.tap do |hash|
    collection.each do |a|
      hash[a.memoryable_type.to_sym] ||= {}
      hash[a.memoryable_type.to_sym][a.memoryable_id] = a.read_at
    end
  end
end