Skip to content

Commit c8bd3e3

Browse files
committed
Merge pull request #36 from jc00ke/limit-number-links-in-desceiption
Limit the # of links allowed in event description
2 parents c9f188e + 97c9210 commit c8bd3e3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

app/controllers/events_controller.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ def create
7474
flash[:failure] = "<h3>Evil Robot</h3> We didn't create this event because we think you're an evil robot. If you're really not an evil robot, look at the form instructions more carefully. If this doesn't work please file a bug report and let us know."
7575
end
7676

77+
if too_many_links = too_many_links?(@event.description)
78+
flash[:failure] = "We allow a maximum of 3 links in a description. You have too many links."
79+
end
80+
7781
respond_to do |format|
78-
if !evil_robot && params[:preview].nil? && @event.save
82+
if !evil_robot && !too_many_links && params[:preview].nil? && @event.save
7983
flash[:success] = 'Your event was successfully created. '
8084
format.html {
8185
if has_new_venue && !params[:venue_name].blank?
@@ -108,8 +112,12 @@ def update
108112
flash[:failure] = "<h3>Evil Robot</h3> We didn't update this event because we think you're an evil robot. If you're really not an evil robot, look at the form instructions more carefully. If this doesn't work please file a bug report and let us know."
109113
end
110114

115+
if too_many_links = too_many_links?(@event.description)
116+
flash[:failure] = "We allow a maximum of 3 links in a description. You have too many links."
117+
end
118+
111119
respond_to do |format|
112-
if !evil_robot && params[:preview].nil? && @event.update_attributes(params[:event])
120+
if !evil_robot && !too_many_links && params[:preview].nil? && @event.update_attributes(params[:event])
113121
flash[:success] = 'Event was successfully updated.'
114122
format.html {
115123
if has_new_venue && !params[:venue_name].blank?
@@ -219,6 +227,12 @@ def clone
219227

220228
protected
221229

230+
# Checks if the description has too many links
231+
# which is probably spam
232+
def too_many_links?(description)
233+
description.scan(/http/).size > 3
234+
end
235+
222236
# Export +events+ to an iCalendar file.
223237
def ical_export(events=nil)
224238
events = events || Event.future.non_duplicates

spec/controllers/events_controller_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,18 @@
419419
flash[:failure].should match /evil robot/i
420420
end
421421

422+
it "should not allow too many links in the description" do
423+
@params[:event][:description] = <<-DESC
424+
http://example.com
425+
https://example.com
426+
http://example.net
427+
https://example.net
428+
DESC
429+
post "create", @params
430+
response.should render_template :new
431+
flash[:failure].should match /too many links/i
432+
end
433+
422434
it "should allow the user to preview the event" do
423435
event = Event.new(:title => "Awesomeness")
424436
Event.should_receive(:new).and_return(event)
@@ -555,6 +567,18 @@
555567
flash[:failure].should match /evil robot/i
556568
end
557569

570+
it "should not allow too many links in the description" do
571+
@params[:event][:description] = <<-DESC
572+
http://example.com
573+
https://example.com
574+
http://example.net
575+
https://example.net
576+
DESC
577+
post "create", @params
578+
response.should render_template :new
579+
flash[:failure].should match /too many links/i
580+
end
581+
558582
it "should allow the user to preview the event" do
559583
tags = []
560584
tags.should_receive(:reload)

0 commit comments

Comments
 (0)