diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 88bde91e562fa923a570c0f1f5998a158d3714d2..f830fbf9bdbb5a084429e8da65660e12238d0e7e 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 622a3e9194aad5f2bf2c5b345bbcdd87b01c0901..8e7e3e4e57ea01cbc5ebea9dbed1e9981f1cf9db 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 159a9da4771e57c8403d769d68f23ceaac90e3e0..da5b55a5c76e2a897fcac9827c4fe644f0d860b7 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 0d1396dc3796b1f1463aed72db4af6e3bc0eeb70..3e340df5d84f7df337a2b846d21a90a25191514c 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 2e7379fd144c03f637b37fdaf60490692e7a3396..f62b43ce8818c9b1828d52b00f9e9a5e95bb402f 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 95c7531dc869cd34757e048a76ae7bd1996c0b3d..605b6ede0349f77a9b558440f6a94ec0d0551d30 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