From 18a1958ef89f1abe83ba1787cb3fc94a0fe00a78 Mon Sep 17 00:00:00 2001 From: Sayed Date: Sun, 15 Mar 2015 16:08:27 +0200 Subject: [PATCH] fix too long tag name #5737 --- app/controllers/status_messages_controller.rb | 3 ++- app/models/profile.rb | 1 + config/locales/diaspora/en.yml | 2 ++ lib/diaspora/taggable.rb | 13 ++++++++++++- spec/models/profile_spec.rb | 5 +++++ spec/models/status_message_spec.rb | 14 ++++++++++++++ 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 88bde91e5..f830fbf9b 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -93,7 +93,8 @@ class StatusMessagesController < ApplicationController respond_to do |format| format.html { redirect_to :back } format.mobile { redirect_to stream_path } - format.json { render :text => @status_message.errors.messages[:text].to_sentence, :status => 403 } + #there are some errors, so we report the first one to the user + format.json { render :text => @status_message.errors.messages.values.first.to_sentence, :status => 403 } end end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 622a3e919..8e7e3e4e5 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -42,6 +42,7 @@ class Profile < ActiveRecord::Base belongs_to :person before_validation do self.tag_string = self.tag_string.split[0..4].join(' ') + self.build_tags end before_save do diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 159a9da47..da5b55a5c 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -1253,6 +1253,8 @@ en: following: "Following #%{tag}" stop_following: "Stop following #%{tag}" none: "The empty tag does not exist!" + name_too_long: "Please make your tag name fewer than %{count} characters. Right now it is %{current_length} characters" + tag_followings: create: success: "Hooray! You’re now following #%{name}." diff --git a/lib/diaspora/taggable.rb b/lib/diaspora/taggable.rb index 0d1396dc3..3e340df5d 100644 --- a/lib/diaspora/taggable.rb +++ b/lib/diaspora/taggable.rb @@ -7,9 +7,20 @@ module Diaspora def self.included(model) model.class_eval do cattr_accessor :field_with_tags + + # validate tag's name maximum length [tag's name should be less than or equal to 255 chars] + validate :tag_name_max_length, on: :create + + # tag's name is limited to 255 charchters according to ActsAsTaggableOn gem, so we check the length of the name for each tag + def tag_name_max_length + self.tag_list.each do |tag| + errors[:tags] << I18n.t('tags.name_too_long', :count => 255, :current_length => tag.length) if tag.length > 255 + end + end + protected :tag_name_max_length end model.instance_eval do - before_create :build_tags + before_validation :build_tags # build tags before validation fixs the too long tag name issue #5737 def extract_tags_from sym self.field_with_tags = sym diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index 2e7379fd1..f62b43ce8 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -272,6 +272,11 @@ describe Profile, :type => :model do @object.save expect(@object.tags.count).to eq(5) end + it 'should require tag name not be more than 255 characters long' do + @object.tag_string = "##{'a' * (255+1)}" + @object.save + expect(@object).not_to be_valid + end it_should_behave_like 'it is taggable' end diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 95c7531dc..605b6ede0 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -70,6 +70,14 @@ describe StatusMessage, :type => :model do expect(guids).to eq([sm1.guid]) end end + + describe '.before_validation' do + it 'calls build_tags' do + status = FactoryGirl.build(:status_message) + expect(status).to receive(:build_tags) + status.save + end + end describe '.before_create' do it 'calls build_tags' do @@ -255,6 +263,12 @@ STR expect(msg_uc.tags).to match_array(tag_array) expect(msg_cp.tags).to match_array(tag_array) end + + it 'should require tag name not be more than 255 characters long' do + message = "##{'a' * (255+1)}" + status_message = FactoryGirl.build(:status_message, :text => message) + expect(status_message).not_to be_valid + end end describe "XML" do