From c651cbab78bd0a598e9ff73082601ca4acc82502 Mon Sep 17 00:00:00 2001 From: Braulio Martinez Date: Wed, 3 Apr 2013 21:58:52 -0300 Subject: [PATCH] Always redirect user to downcase version of tags on UTF-8 encoding --- .../javascripts/app/helpers/text_formatter.js | 3 ++- app/assets/javascripts/app/router.js | 2 +- app/controllers/tags_controller.rb | 11 +++++++++++ spec/controllers/tags_controller_spec.rb | 11 +++++++++++ .../app/helpers/text_formatter_spec.js | 6 ++++++ spec/javascripts/app/router_spec.js | 16 ++++++++++++++++ 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/app/helpers/text_formatter.js b/app/assets/javascripts/app/helpers/text_formatter.js index a4602b12f..57c7a5d57 100644 --- a/app/assets/javascripts/app/helpers/text_formatter.js +++ b/app/assets/javascripts/app/helpers/text_formatter.js @@ -104,7 +104,8 @@ $(function() { textFormatter.hashtagify = function hashtagify(text){ var utf8WordCharcters =/(\s|^|>)#([\u0080-\uFFFF|\w|-]+|<3)/g return text.replace(utf8WordCharcters, function(hashtag, preceeder, tagText) { - return preceeder + "#" + tagText + "" + return preceeder + "#" + tagText + "" }) }; diff --git a/app/assets/javascripts/app/router.js b/app/assets/javascripts/app/router.js index 42c8d1a4e..4f143aa9d 100644 --- a/app/assets/javascripts/app/router.js +++ b/app/assets/javascripts/app/router.js @@ -77,7 +77,7 @@ app.Router = Backbone.Router.extend({ if(name) { var followedTagsAction = new app.views.TagFollowingAction( - {tagText: decodeURIComponent(name)} + {tagText: decodeURIComponent(name).toLowerCase()} ); $("#author_info").prepend(followedTagsAction.render().el) } diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 9d86638fa..2729268ef 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -30,6 +30,8 @@ class TagsController < ApplicationController end def show + redirect_to(:action => :show, :name => downcased_tag_name) && return if tag_has_capitals? + if user_signed_in? gon.tagFollowings = tags end @@ -45,6 +47,15 @@ class TagsController < ApplicationController TagFollowing.user_is_following?(current_user, params[:name]) end + def tag_has_capitals? + mb_tag = params[:name].mb_chars + mb_tag.downcase != mb_tag + end + + def downcased_tag_name + params[:name].mb_chars.downcase.to_s + end + def prep_tags_for_javascript @tags.map! do |tag| { :name => ("#" + tag.name) } diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index a0f18c696..391cb3b8a 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -36,6 +36,17 @@ describe TagsController do end describe '#show' do + context 'tag with capital letters' do + before do + sign_in :user, alice + end + + it 'redirect to the downcase tag uri' do + get :show, :name => 'DiasporaRocks!' + response.should redirect_to(:action => :show, :name => 'diasporarocks!') + end + end + context 'signed in' do before do sign_in :user, alice diff --git a/spec/javascripts/app/helpers/text_formatter_spec.js b/spec/javascripts/app/helpers/text_formatter_spec.js index 51acb2816..1a95c0c3a 100644 --- a/spec/javascripts/app/helpers/text_formatter_spec.js +++ b/spec/javascripts/app/helpers/text_formatter_spec.js @@ -214,6 +214,12 @@ describe("app.helpers.textFormatter", function(){ expect(wrapper.find("h1").length).toBe(0) expect(wrapper.find("a[href='/tags/parties']").text()).toContain("#parties") }) + + it("and the resultant link has the tags name downcased", function(){ + var formattedText = this.formatter.hashtagify("#PARTIES, I love") + + expect(formattedText).toContain("/tags/parties") + }) }) }) diff --git a/spec/javascripts/app/router_spec.js b/spec/javascripts/app/router_spec.js index e116daaf4..d2a19ab81 100644 --- a/spec/javascripts/app/router_spec.js +++ b/spec/javascripts/app/router_spec.js @@ -15,5 +15,21 @@ describe('app.Router', function () { expect(followed_tags).toHaveBeenCalled(); expect(tag_following_action).toHaveBeenCalledWith({tagText: 'օբյեկտիվ'}); }); + + it('navigates to the downcase version of the corresponding tag', function () { + var followed_tags = spyOn(app.router, 'followed_tags').andCallThrough(); + var tag_following_action = spyOn(app.views, 'TagFollowingAction').andCallFake(function(data) { + return {render: function() { return {el: ""}}}; + }); + spyOn(window.history, 'pushState').andCallFake(function (data, title, url) { + var route = app.router._routeToRegExp("tags/:name"); + var args = app.router._extractParameters(route, url.replace(/^\//, "")); + app.router.followed_tags(args[0]); + }); + window.preloads = {tagFollowings: []}; + app.router.navigate('/tags/'+encodeURIComponent('SomethingWithCapitalLetters')); + expect(followed_tags).toHaveBeenCalled(); + expect(tag_following_action).toHaveBeenCalledWith({tagText: 'somethingwithcapitalletters'}); + }); }); });