Skip to content

Commit

Permalink
Improve compatibility with Redmine 5
Browse files Browse the repository at this point in the history
  • Loading branch information
nanego committed Jul 6, 2023
1 parent 596cd62 commit 43d4198
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 143 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/4_1_7.yml → .github/workflows/5_0_5.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Tests 4.1.7
name: Tests 5.0.5

env:
PLUGIN_NAME: redmine_datetime_custom_field
REDMINE_VERSION: 4.1.7
REDMINE_VERSION: 5.0.5
RAILS_ENV: test

on:
Expand All @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
ruby: ['2.6']
ruby: ['2.7']
db: ['postgres']
fail-fast: false

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ Note that this plugin has a dependency. You have to install this other plugin:

## Test status

|Plugin branch| Redmine Version | Test Status |
|-------------|-------------------|------------------|
|master | 4.2.10 | [![4.2.10][1]][5]|
|master | 4.1.7 | [![4.1.7][2]][5] |
|master | master | [![master][4]][5]|
|Plugin branch| Redmine Version | Test Status |
|-------------|-----------------|-------------------|
|master | 4.2.10 | [![4.2.10][1]][5] |
|master | 5.0.5 | [![5.0.5][2]][5] |
|master | master | [![master][4]][5] |

[1]: https://github.com/nanego/redmine_datetime_custom_field/actions/workflows/4_2_10.yml/badge.svg
[2]: https://github.com/nanego/redmine_datetime_custom_field/actions/workflows/4_1_7.yml/badge.svg
[2]: https://github.com/nanego/redmine_datetime_custom_field/actions/workflows/5_0_5.yml/badge.svg
[4]: https://github.com/nanego/redmine_datetime_custom_field/actions/workflows/master.yml/badge.svg
[5]: https://github.com/nanego/redmine_datetime_custom_field/actions
9 changes: 1 addition & 8 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,4 @@
end

# Custom patches
require_dependency 'redmine_datetime_custom_field/hooks'
Rails.application.config.to_prepare do
require_dependency 'redmine_datetime_custom_field/application_helper_patch'
require_dependency 'redmine_datetime_custom_field/field_format_patch'
require_dependency 'redmine_datetime_custom_field/custom_fields_helper_patch'
require_dependency 'redmine_datetime_custom_field/query_patch'
require_dependency 'redmine_datetime_custom_field/custom_field_patch'
end
require_relative 'lib/redmine_datetime_custom_field/hooks'
6 changes: 3 additions & 3 deletions lib/redmine_datetime_custom_field/application_helper_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Date::DATE_FORMATS[:default] = '%d/%m/%Y'
end

module PluginDatetimeCustomField
module ApplicationHelper
module RedmineDatetimeCustomField
module ApplicationHelperPatch
def format_object(object, html=true, &block)
if (object.class.name=='CustomValue' || object.class.name== 'CustomFieldValue') && object.custom_field
return "" unless object.customized&.visible?
Expand All @@ -25,7 +25,7 @@ def format_object(object, html=true, &block)
end
end
end
ApplicationHelper.prepend PluginDatetimeCustomField::ApplicationHelper
ApplicationHelper.prepend RedmineDatetimeCustomField::ApplicationHelperPatch
ActionView::Base.prepend ApplicationHelper

module ApplicationHelper
Expand Down
19 changes: 17 additions & 2 deletions lib/redmine_datetime_custom_field/custom_field_patch.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
require_dependency 'custom_field'

class CustomField < ActiveRecord::Base
safe_attributes 'show_hours', 'show_shortcut'
module RedmineDatetimeCustomField
module CustomFieldPatch
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
safe_attributes 'show_hours', 'show_shortcut'
end
end

module InstanceMethods
end
end
end

unless CustomField.included_modules.include?(RedmineDatetimeCustomField::CustomFieldPatch)
CustomField.send(:include, RedmineDatetimeCustomField::CustomFieldPatch)
end
25 changes: 15 additions & 10 deletions lib/redmine_datetime_custom_field/custom_fields_helper_patch.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
require_dependency 'custom_fields_helper'

module CustomFieldsHelper

# Return a string used to display a custom value
def format_value(value, custom_field)
formatted_value = custom_field.format.formatted_value(self, custom_field, value, false)
if formatted_value.class.name == 'Time'
format_time_without_zone(formatted_value, true)
else
format_object(formatted_value, false)
module RedmineDatetimeCustomField
module CustomFieldsHelperPatch

# Return a string used to display a custom value
def format_value(value, custom_field)
formatted_value = custom_field.format.formatted_value(self, custom_field, value, false)
if formatted_value.class.name == 'Time'
format_time_without_zone(formatted_value, true)
else
format_object(formatted_value, false)
end
end
end

end
end

ApplicationHelper.prepend RedmineDatetimeCustomField::CustomFieldsHelperPatch
ActionView::Base.prepend ApplicationHelper
75 changes: 41 additions & 34 deletions lib/redmine_datetime_custom_field/field_format_patch.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
require 'redmine/field_format'

module Redmine
module FieldFormat
class DateFormat < Unbounded
module RedmineDatetimeCustomField
module FieldFormatPatch

field_attributes :show_hours, :show_shortcut
def cast_single_value(custom_field, value, customized = nil)
(custom_field.show_hours == '1' ? value.to_time : value.to_date) rescue nil
end

def cast_single_value(custom_field, value, customized = nil)
(custom_field.show_hours == '1' ? value.to_time : value.to_date) rescue nil
def validate_single_value(custom_field, value, customized = nil)
if (((value =~ /^\d{4}-\d{2}-\d{2}$/ || value =~ /^\d{2}\/\d{2}\/\d{4}$/) && custom_field.show_hours != '1') || ((value =~ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/ || value =~ /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}$/) && custom_field.show_hours == '1')) && (value.to_date rescue false)
[]
else
[::I18n.t('activerecord.errors.messages.not_a_date')]
end
end

def validate_single_value(custom_field, value, customized = nil)
if (((value =~ /^\d{4}-\d{2}-\d{2}$/ || value =~ /^\d{2}\/\d{2}\/\d{4}$/) && custom_field.show_hours != '1') || ((value =~ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/ || value =~ /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}$/) && custom_field.show_hours == '1')) && (value.to_date rescue false)
[]
def edit_tag(view, tag_id, tag_name, custom_value, options = {})
edit_tag = view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 15)) +
view.calendar_for(tag_id, custom_value.custom_field.show_hours == '1')
edit_tag += view.link_to(::I18n.t(:now_label), '#',
onclick: "nowShortcut(this.dataset.cfId, " + custom_value.custom_field.show_hours + "); return false",
data: { cf_id: custom_value.custom_field.id },
class: "now_shortcut") if custom_value.custom_field.show_shortcut == '1'
edit_tag
end

def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options = {})
view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 15)) +
view.calendar_for(tag_id, custom_field.show_hours == '1') +
bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
end

if !Rails.env.test?
def order_statement(custom_field)
if custom_field.class.connection.adapter_name.downcase.to_sym == :mysql2
Arel.sql "STR_TO_DATE(#{join_alias custom_field}.value, '%d/%m/%Y %H:%i')"
else
[::I18n.t('activerecord.errors.messages.not_a_date')]
Arel.sql "to_timestamp(#{join_alias custom_field}.value, 'DD/MM/YYYY HH24:MI')"
end
end
end

def edit_tag(view, tag_id, tag_name, custom_value, options = {})
edit_tag = view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 15)) +
view.calendar_for(tag_id, custom_value.custom_field.show_hours == '1')
edit_tag += view.link_to(::I18n.t(:now_label), '#',
onclick: "nowShortcut(this.dataset.cfId, " + custom_value.custom_field.show_hours + "); return false",
data: { cf_id: custom_value.custom_field.id },
class: "now_shortcut") if custom_value.custom_field.show_shortcut == '1'
edit_tag
end

def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options = {})
view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 15)) +
view.calendar_for(tag_id, custom_field.show_hours == '1') +
bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
end
end
end

if !Rails.env.test?
def order_statement(custom_field)
if custom_field.class.connection.adapter_name.downcase.to_sym == :mysql2
Arel.sql "STR_TO_DATE(#{join_alias custom_field}.value, '%d/%m/%Y %H:%i')"
else
Arel.sql "to_timestamp(#{join_alias custom_field}.value, 'DD/MM/YYYY HH24:MI')"
end
end
end
module Redmine
module FieldFormat
class DateFormat < Unbounded
prepend RedmineDatetimeCustomField::FieldFormatPatch

field_attributes :show_hours, :show_shortcut
end
end
end
Expand All @@ -59,3 +65,4 @@ def validate_each(record, attribute, value)
end
end
end

10 changes: 10 additions & 0 deletions lib/redmine_datetime_custom_field/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,15 @@ def view_layouts_base_html_head(context)
javascript_include_tag("datetime_custom_field", :plugin => "redmine_datetime_custom_field")
end

class ModelHook < Redmine::Hook::Listener
def after_plugins_loaded(_context = {})
require_relative 'application_helper_patch'
require_relative 'field_format_patch'
require_relative 'custom_fields_helper_patch'
require_relative 'query_patch'
require_relative 'custom_field_patch'
end
end

end
end
Loading

0 comments on commit 43d4198

Please sign in to comment.