From: Tom Hughes Date: Thu, 27 Jul 2017 18:27:17 +0000 (+0100) Subject: Convert @user to current_user in helpers X-Git-Tag: live~3857^2~2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/2c9c427587a7cb42136315d6c1d533d3eb058545?hp=555a821c3e1b79093b63b96e981c4766cf43baa9 Convert @user to current_user in helpers --- diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c5b08e515..d27b47f27 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -21,12 +21,12 @@ module ApplicationHelper css = "" css << ".hidden { display: none !important }" - css << ".hide_unless_logged_in { display: none !important }" unless @user - css << ".hide_if_logged_in { display: none !important }" if @user - css << ".hide_if_user_#{@user.id} { display: none !important }" if @user - css << ".show_if_user_#{@user.id} { display: inline !important }" if @user - css << ".hide_unless_administrator { display: none !important }" unless @user && @user.administrator? - css << ".hide_unless_moderator { display: none !important }" unless @user && @user.moderator? + css << ".hide_unless_logged_in { display: none !important }" unless current_user + css << ".hide_if_logged_in { display: none !important }" if current_user + css << ".hide_if_user_#{current_user.id} { display: none !important }" if current_user + css << ".show_if_user_#{current_user.id} { display: inline !important }" if current_user + css << ".hide_unless_administrator { display: none !important }" unless current_user && current_user.administrator? + css << ".hide_unless_moderator { display: none !important }" unless current_user && current_user.moderator? content_tag(:style, css, :type => "text/css") end @@ -107,11 +107,11 @@ module ApplicationHelper :preferred_editor => preferred_editor } - if @user - data[:user] = @user.id.to_json + if current_user + data[:user] = current_user.id.to_json - unless @user.home_lon.nil? || @user.home_lat.nil? - data[:user_home] = { :lat => @user.home_lat, :lon => @user.home_lon } + unless current_user.home_lon.nil? || current_user.home_lat.nil? + data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } end end diff --git a/app/helpers/user_roles_helper.rb b/app/helpers/user_roles_helper.rb index 7093a96b2..805abb58f 100644 --- a/app/helpers/user_roles_helper.rb +++ b/app/helpers/user_roles_helper.rb @@ -6,7 +6,7 @@ module UserRolesHelper end def role_icon(user, role) - if @user && @user.administrator? + if current_user && current_user.administrator? if user.has_role?(role) image = "roles/#{role}" alt = t("user.view.role.revoke.#{role}") diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index 720113e65..d1c41d62c 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -1,6 +1,8 @@ require "test_helper" class ApplicationHelperTest < ActionView::TestCase + attr_accessor :current_user + def setup I18n.locale = "en" end @@ -46,7 +48,7 @@ class ApplicationHelperTest < ActionView::TestCase end def test_style_rules - @user = nil + self.current_user = nil css = style_rules assert_match /\.hidden /, css @@ -57,36 +59,36 @@ class ApplicationHelperTest < ActionView::TestCase assert_match /\.hide_unless_administrator /, css assert_match /\.hide_unless_moderator /, css - @user = create(:user) + self.current_user = create(:user) css = style_rules assert_match /\.hidden /, css assert_no_match /\.hide_unless_logged_in /, css assert_match /\.hide_if_logged_in /, css - assert_match /\.hide_if_user_#{@user.id} /, css - assert_match /\.show_if_user_#{@user.id} /, css + assert_match /\.hide_if_user_#{current_user.id} /, css + assert_match /\.show_if_user_#{current_user.id} /, css assert_match /\.hide_unless_administrator /, css assert_match /\.hide_unless_moderator /, css - @user = create(:moderator_user) + self.current_user = create(:moderator_user) css = style_rules assert_match /\.hidden /, css assert_no_match /\.hide_unless_logged_in /, css assert_match /\.hide_if_logged_in /, css - assert_match /\.hide_if_user_#{@user.id} /, css - assert_match /\.show_if_user_#{@user.id} /, css + assert_match /\.hide_if_user_#{current_user.id} /, css + assert_match /\.show_if_user_#{current_user.id} /, css assert_match /\.hide_unless_administrator /, css assert_no_match /\.hide_unless_moderator /, css - @user = create(:administrator_user) + self.current_user = create(:administrator_user) css = style_rules assert_match /\.hidden /, css assert_no_match /\.hide_unless_logged_in /, css assert_match /\.hide_if_logged_in /, css - assert_match /\.hide_if_user_#{@user.id} /, css - assert_match /\.show_if_user_#{@user.id} /, css + assert_match /\.hide_if_user_#{current_user.id} /, css + assert_match /\.show_if_user_#{current_user.id} /, css assert_no_match /\.hide_unless_administrator /, css assert_match /\.hide_unless_moderator /, css end diff --git a/test/helpers/user_roles_helper_test.rb b/test/helpers/user_roles_helper_test.rb index 880c16e59..3ffc9fbff 100644 --- a/test/helpers/user_roles_helper_test.rb +++ b/test/helpers/user_roles_helper_test.rb @@ -1,11 +1,12 @@ require "test_helper" class UserRolesHelperTest < ActionView::TestCase + attr_accessor :current_user + def test_role_icon_normal - user = create(:user) - @user = user + self.current_user = create(:user) - icon = role_icon(user, "moderator") + icon = role_icon(current_user, "moderator") assert_dom_equal "", icon icon = role_icon(create(:moderator_user), "moderator") @@ -13,7 +14,7 @@ class UserRolesHelperTest < ActionView::TestCase end def test_role_icon_administrator - @user = create(:administrator_user) + self.current_user = create(:administrator_user) user = create(:user) icon = role_icon(user, "moderator") @@ -25,10 +26,9 @@ class UserRolesHelperTest < ActionView::TestCase end def test_role_icons_normal - user = create(:user) - @user = user + self.current_user = create(:user) - icons = role_icons(user) + icons = role_icons(current_user) assert_dom_equal " ", icons icons = role_icons(create(:moderator_user)) @@ -39,7 +39,7 @@ class UserRolesHelperTest < ActionView::TestCase end def test_role_icons_administrator - @user = create(:administrator_user) + self.current_user = create(:administrator_user) user = create(:user) icons = role_icons(user)