-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathlink.rb
More file actions
131 lines (105 loc) · 2.87 KB
/
link.rb
File metadata and controls
131 lines (105 loc) · 2.87 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# encoding: UTF-8
# == Schema Information
#
# Table name: links
#
# id :integer not null, primary key
# news_id :integer not null
# title :string(100) not null
# url :string(255) not null
# lang :string(2) not null
# created_at :datetime
# updated_at :datetime
#
#
# The news can have some important links.
# We follow the number of clicks on each of these links.
#
class Link < ActiveRecord::Base
PROTOCOLS = HTML::Pipeline::SanitizationFilter::WHITELIST[:protocols]['a']['href'] - [:relative]
belongs_to :news
attr_accessor :user
Accessible = [:id, :user, :title, :url, :lang]
validates :title, presence: { message: "Un lien doit obligatoirement avoir un titre" },
length: { maximum: 100, message: "Le titre est trop long" }
validates :url, uri: { protocols: PROTOCOLS, message: "L'adresse n'est pas valide" },
presence: { message: "Un lien doit obligatoirement avoir une adresse" },
length: { maximum: 255, message: "L’adresse est trop longue" }
before_validation do |link|
link.url = UriValidator.before_validation(link.url);
end
after_validation do |link|
link.url = UriValidator.after_validation(link.url);
end
### Behaviour ###
def self.hit(id)
url = $redis.get("links/#{id}/url")
return nil if url.blank?
$redis.incr("links/#{id}/hits")
url
end
def nb_clicks
$redis.get("links/#{self.id}/hits").to_i
end
def update_by(user)
self.user = user
if url.blank?
destroy
else
save
end
$redis.del lock_key
end
after_commit :create_new_version
def create_new_version
news.editor ||= user.try(:account)
news.put_paragraphs_together
news.create_new_version
end
after_save :save_url_in_redis
def save_url_in_redis
$redis.set("links/#{self.id}/url", url)
end
### Presentation ###
def to_s
"[#{lang}] #{title} : #{url}"
end
### Push ###
after_create :announce_create
def announce_create
Push.create(news, as_json.merge(kind: :add_link, nb_clicks: 0))
end
after_update :announce_update
def announce_update
Push.create(news, as_json.merge(kind: :update_link, nb_clicks: nb_clicks))
end
before_destroy :announce_destroy
def announce_destroy
Push.create(news, id: self.id, kind: :remove_link)
end
### Lock ###
def lock_key
"locks/#{news.id}/l/#{self.id}"
end
def lock_by(user)
return false if news.locked_for_reorg?
locker_id = $redis.get(lock_key)
return locker_id == user.id if locker_id
$redis.set lock_key, user.id
$redis.expire lock_key, 300
true
end
def unlock
$redis.del lock_key
end
def locked?
!!$redis.get(lock_key)
end
def locked_by?(user_id)
$redis.get(lock_key) == user_id
end
### Presentation ###
def locker
User.find($redis.get lock_key).name
end
end