cirandas.net

ref: master

app/models/profile_activity.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class ProfileActivity < ApplicationRecord

  self.record_timestamps = false

  attr_accessible :profile_id,
    :profile, :activity

  belongs_to :profile
  belongs_to :activity, polymorphic: true

  # non polymorphic versions
  belongs_to :scrap, -> {
    where profile_activities: {activity_type: 'Scrap'}
  }, foreign_key: :activity_id, class_name: 'Scrap'
  belongs_to :action_tracker, -> {
    where profile_activities: {activity_type: 'ActionTracker::Record'}
  }, foreign_key: :activity_id, class_name: 'ActionTracker::Record'

  before_validation :copy_timestamps

  def self.update_activity activity
    profile_activity = ProfileActivity.where(activity_id: activity.id, activity_type: activity.class.base_class.name).first
    return unless profile_activity
    profile_activity.send :copy_timestamps
    profile_activity.save!
    profile_activity
  end

  protected

  def copy_timestamps
    self.created_at = self.activity.created_at if self.activity.created_at
    self.updated_at = self.activity.updated_at if self.activity.updated_at
  end

end