cirandas.net

ref: master

app/models/scrap.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Scrap < ApplicationRecord

  include SanitizeHelper

  attr_accessible :content, :sender_id, :receiver_id, :scrap_id, :marked_people

  SEARCHABLE_FIELDS = {
    :content => {:label => _('Content'), :weight => 1},
  }
  validates_presence_of :content
  validates_presence_of :sender_id, :receiver_id

  belongs_to :receiver, :class_name => 'Profile', :foreign_key => 'receiver_id'
  belongs_to :sender, :class_name => 'Person', :foreign_key => 'sender_id'
  has_many :replies, :class_name => 'Scrap', :foreign_key => 'scrap_id', :dependent => :destroy
  belongs_to :root, :class_name => 'Scrap', :foreign_key => 'scrap_id'

  has_many :profile_activities, -> {
    where profile_activities: {activity_type: 'Scrap'}
  }, foreign_key: :activity_id, dependent: :destroy

  has_and_belongs_to_many :marked_people, :join_table => :private_scraps, :class_name => 'Person'

  after_create :create_activity
  after_update :update_activity

  scope :all_scraps, -> profile { limit(30).where("receiver_id = ? OR sender_id = ?", profile, profile) }

  scope :not_replies, -> { where scrap_id: nil }

  track_actions :leave_scrap, :after_create, :keep_params => ['sender.name', 'content', 'receiver.name', 'receiver.url'], :if => Proc.new{|s| s.sender != s.receiver && s.sender != s.top_root.receiver}, :custom_target => :action_tracker_target, :custom_user => :sender

  track_actions :leave_scrap_to_self, :after_create, :keep_params => ['sender.name', 'content'], :if => Proc.new{|s| s.sender == s.receiver}, :custom_user => :sender

  track_actions :reply_scrap_on_self, :after_create, :keep_params => ['sender.name', 'content', 'receiver.name', 'receiver.url'], :if => Proc.new{|s| s.sender != s.receiver && s.sender == s.top_root.receiver}, :custom_user => :sender

  after_create :send_notification

  before_validation :strip_all_html_tags

  alias :user :sender
  alias :target :receiver

  def top_root
    scrap = self
    scrap = Scrap.find(scrap.scrap_id) while scrap.scrap_id
    scrap
  end

  def strip_all_html_tags
    self.content = sanitize_html(self.content)
  end

  def action_tracker_target
    self.receiver.is_a?(Community) ? self.receiver : self
  end

  def is_root?
    !root.nil?
  end

  def scrap_wall_url
    is_root? ? root.receiver.wall_url : receiver.wall_url
  end

  def send_notification?
    sender != receiver && (is_root? ? root.receiver.receives_scrap_notification? : receiver.receives_scrap_notification?)
  end

  def display_to?(user = nil)
    marked_people.blank? || marked_people.include?(user)
  end

  protected

  def create_activity
    # do not scrap replies (when scrap_id is not nil)
    return if self.scrap_id.present?
    ProfileActivity.create! profile_id: self.receiver_id, activity: self
  end

  def update_activity
    ProfileActivity.update_activity self
  end

  def send_notification
    self.root.update_attribute('updated_at', DateTime.now) unless self.root.nil?
    ScrapNotifier.notification(self).deliver if self.send_notification?
  end

end