From f440afd0c69d8501ea53591742d89ccc25f63ee9 Mon Sep 17 00:00:00 2001 From: Alexander Meindl Date: Tue, 11 Dec 2018 15:14:56 +0100 Subject: [PATCH] Allow custom date with date macro --- CHANGELOG.rst | 6 +++++ docs/macros/date.rst | 7 ++++++ init.rb | 2 +- lib/additionals/wiki_macros/date_macro.rb | 9 +++---- test/functional/wiki_controller_test.rb | 29 ++++++++++++++++++++++- 5 files changed, 45 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 18559cd4..78b45f08 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Changelog ========= +2.0.17-dev +++++++++++ + +- add possibility to use custom date with date macro + + 2.0.16 ++++++ diff --git a/docs/macros/date.rst b/docs/macros/date.rst index a13523e6..e3dd6dc6 100644 --- a/docs/macros/date.rst +++ b/docs/macros/date.rst @@ -14,6 +14,7 @@ Date current_minute current minute current_weekday current weekday current_weeknumber current week number (1 - 52) The week starts with Monday + YYYY-MM-DD e.g. 2018-12-24, which will formated with Redmine date format Scope +++++ @@ -49,3 +50,9 @@ Show current week number .. code-block:: smarty {{date(current_weeknumber)}} + +Show date 2018-12-24 in right format, which is specified in Redmine Settings, which shows e.g. 24.12.2018 + +.. code-block:: smarty + + {{date(2018-12-24)}} diff --git a/init.rb b/init.rb index 2574267e..9e5f906c 100644 --- a/init.rb +++ b/init.rb @@ -4,7 +4,7 @@ Redmine::Plugin.register :additionals do name 'Additionals' author 'AlphaNodes GmbH' description 'Customizing Redmine, providing wiki macros and act as a library/function provider for other Redmine plugins' - version '2.0.16' + version '2.0.17-dev' author_url 'https://alphanodes.com/' url 'https://github.com/alphanodes/additionals' diff --git a/lib/additionals/wiki_macros/date_macro.rb b/lib/additionals/wiki_macros/date_macro.rb index 45b3a1fd..2ae18969 100644 --- a/lib/additionals/wiki_macros/date_macro.rb +++ b/lib/additionals/wiki_macros/date_macro.rb @@ -18,6 +18,7 @@ module Additionals - current_minute current minute - current_weekday current weekday - current_weeknumber current week number (1 - 52) The week starts with Monday + - YYYY-MM-DD e.g. 2018-12-24, which will formated with Redmine date format Examples: @@ -32,18 +33,12 @@ module Additionals DESCRIPTION macro :date do |_obj, args| - valid_types = %w[current_date current_date_with_time current_year - current_month current_day current_hour current_minute - current_weekday current_weeknumber] - type = if args.present? args[0] else 'current_date' end - return if valid_types.exclude?(type) - d = Additionals.now_with_user_time_zone date_result = case type when 'current_date' @@ -64,6 +59,8 @@ module Additionals day_name(d.wday) when 'current_weeknumber' User.current.today.cweek + else + format_date(type.to_date) end content_tag(:span, date_result, class: 'current-date') diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 2ba492c4..fafabeeb 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -324,6 +324,7 @@ class WikiControllerTest < Additionals::ControllerTest assert_select 'div.wiki', html: /{{date/, count: 0 assert_select 'div.wiki span.current-date', count: valid_types.count assert_select 'div.wiki span.current-date', User.current.today.cweek.to_s + assert_select 'div.flash.error', html: /Error executing/, count: 0 end def test_show_with_date_macro_and_invalid_type @@ -336,7 +337,33 @@ class WikiControllerTest < Additionals::ControllerTest params: { project_id: 1, id: @page_name } assert_response :success - assert_select 'div.wiki', html: /{{date/ + assert_select 'div.flash.error', html: /Error executing/ + end + + def test_show_with_date_macro_custom_date + @request.session[:user_id] = WIKI_MACRO_USER_ID + + @page.content.text = '{{date(2017-02-25)}}' + @page.content.save! + + get :show, + params: { project_id: 1, id: @page_name } + + assert_response :success + assert_select 'div.flash.error', html: /Error executing/, count: 0 + end + + def test_show_with_date_macro_invalid_custom_date + @request.session[:user_id] = WIKI_MACRO_USER_ID + + @page.content.text = '{{date(2017-02-30)}}' + @page.content.save! + + get :show, + params: { project_id: 1, id: @page_name } + + assert_response :success + assert_select 'div.flash.error', html: /Error executing/ end def test_show_issue