-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve compatibility with Redmine 5
- Loading branch information
Showing
8 changed files
with
166 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
lib/redmine_datetime_custom_field/custom_fields_helper_patch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,103 @@ | ||
require_dependency 'query' | ||
require_dependency 'issue_query' | ||
|
||
class Query | ||
|
||
def validate_query_filters | ||
filters.each_key do |field| | ||
if values_for(field) | ||
case type_for(field) | ||
when :integer | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !/\A[+-]?\d+(,[+-]?\d+)*\z/.match?(v) } | ||
when :float | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !/\A[+-]?\d+(\.\d*)?\z/.match?(v) } | ||
when :date, :date_past | ||
case operator_for(field) | ||
when "=", ">=", "<=", "><" | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| | ||
### CUSTOM BEGIN | ||
# add new valid date format | ||
v.present? && ((!/\A\d{4}-\d{2}-\d{2}(T\d{2}((:)?\d{2}){0,2}(Z|\d{2}:?\d{2})?)?\z/.match?(v) && !v.match(/\A\d{2}\/\d{2}\/\d{4}(T\d{2}((:)?\d{2}){0,2}(Z|\d{2}:?\d{2})?)?\z/)) || parse_date(v).nil?) | ||
### CUSTOM END | ||
} | ||
when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-" | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !/^\d+$/.match?(v) } | ||
module RedmineDatetimeCustomField | ||
module QueryPatch | ||
def validate_query_filters | ||
filters.each_key do |field| | ||
if values_for(field) | ||
case type_for(field) | ||
when :integer | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !/\A[+-]?\d+(,[+-]?\d+)*\z/.match?(v) } | ||
when :float | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !/\A[+-]?\d+(\.\d*)?\z/.match?(v) } | ||
when :date, :date_past | ||
case operator_for(field) | ||
when "=", ">=", "<=", "><" | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| | ||
### CUSTOM BEGIN | ||
# add new valid date format | ||
v.present? && ((!/\A\d{4}-\d{2}-\d{2}(T\d{2}((:)?\d{2}){0,2}(Z|\d{2}:?\d{2})?)?\z/.match?(v) && !v.match(/\A\d{2}\/\d{2}\/\d{4}(T\d{2}((:)?\d{2}){0,2}(Z|\d{2}:?\d{2})?)?\z/)) || parse_date(v).nil?) | ||
### CUSTOM END | ||
} | ||
when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-" | ||
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !/^\d+$/.match?(v) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
add_filter_error(field, :blank) unless | ||
add_filter_error(field, :blank) unless | ||
# filter requires one or more values | ||
(values_for(field) and !values_for(field).first.blank?) or | ||
# filter doesn't require any value | ||
["o", "c", "!*", "*", "nd", "t", "ld", "nw", "w", "lw", "l2w", "nm", "m", "lm", "y", "*o", "!o"].include? operator_for(field) | ||
end if filters | ||
end | ||
# filter doesn't require any value | ||
["o", "c", "!*", "*", "nd", "t", "ld", "nw", "w", "lw", "l2w", "nm", "m", "lm", "y", "*o", "!o"].include? operator_for(field) | ||
end if filters | ||
end | ||
|
||
def quoted_time(time, is_custom_filter) | ||
if is_custom_filter | ||
# Custom field values are stored as strings in the DB | ||
# using this format that does not depend on DB date representation | ||
if Rails.env.test? | ||
time.strftime("%Y-%m-%d %H:%M:%S") | ||
def quoted_time(time, is_custom_filter) | ||
if is_custom_filter | ||
# Custom field values are stored as strings in the DB | ||
# using this format that does not depend on DB date representation | ||
if Rails.env.test? | ||
time.strftime("%Y-%m-%d %H:%M:%S") | ||
else | ||
time.strftime("%d/%m/%Y %H:%M") #Custom format | ||
end | ||
else | ||
time.strftime("%d/%m/%Y %H:%M") #Custom format | ||
self.class.connection.quoted_date(time) | ||
end | ||
else | ||
self.class.connection.quoted_date(time) | ||
end | ||
end | ||
|
||
# Returns a SQL clause for a date or datetime field. | ||
def date_clause(table, field, from, to, is_custom_filter) | ||
s = [] | ||
if from | ||
if from.is_a?(Date) | ||
from = date_for_user_time_zone(from.year, from.month, from.day).yesterday.end_of_day | ||
else | ||
from = from - 1 # second | ||
end | ||
if self.class.default_timezone == :utc | ||
from = from.utc | ||
end | ||
# Returns a SQL clause for a date or datetime field. | ||
def date_clause(table, field, from, to, is_custom_filter) | ||
s = [] | ||
if from | ||
if from.is_a?(Date) | ||
from = date_for_user_time_zone(from.year, from.month, from.day).yesterday.end_of_day | ||
else | ||
from = from - 1 # second | ||
end | ||
if self.class.default_timezone == :utc | ||
from = from.utc | ||
end | ||
|
||
### Patch Start | ||
if is_custom_filter && self.class.connection.adapter_name.downcase.to_sym == :postgresql && !Rails.env.test? | ||
s << ("to_timestamp(#{table}.#{field},'DD/MM/YYYY HH24:MI') > to_timestamp('%s','DD/MM/YYYY HH24:MI')" % [quoted_time(from, is_custom_filter)]) | ||
else | ||
if Rails.env.test? || table.classify.constantize.columns_hash[field].type == :date || table.classify.constantize.columns_hash[field].type == :datetime | ||
s << ("#{table}.#{field} > '%s'" % [quoted_time(from, is_custom_filter)]) | ||
### Patch Start | ||
if is_custom_filter && self.class.connection.adapter_name.downcase.to_sym == :postgresql && !Rails.env.test? | ||
s << ("to_timestamp(#{table}.#{field},'DD/MM/YYYY HH24:MI') > to_timestamp('%s','DD/MM/YYYY HH24:MI')" % [quoted_time(from, is_custom_filter)]) | ||
else | ||
s << ("STR_TO_DATE(#{table}.#{field},'%d/%m/%Y') > STR_TO_DATE('" + quoted_time(from, is_custom_filter) + "','%d/%m/%Y')") | ||
if Rails.env.test? || table.classify.constantize.columns_hash[field].type == :date || table.classify.constantize.columns_hash[field].type == :datetime | ||
s << ("#{table}.#{field} > '%s'" % [quoted_time(from, is_custom_filter)]) | ||
else | ||
s << ("STR_TO_DATE(#{table}.#{field},'%d/%m/%Y') > STR_TO_DATE('" + quoted_time(from, is_custom_filter) + "','%d/%m/%Y')") | ||
end | ||
end | ||
end | ||
### Patch End | ||
### Patch End | ||
|
||
end | ||
if to | ||
if to.is_a?(Date) | ||
to = date_for_user_time_zone(to.year, to.month, to.day).end_of_day | ||
end | ||
if self.class.default_timezone == :utc | ||
to = to.utc | ||
end | ||
if to | ||
if to.is_a?(Date) | ||
to = date_for_user_time_zone(to.year, to.month, to.day).end_of_day | ||
end | ||
if self.class.default_timezone == :utc | ||
to = to.utc | ||
end | ||
|
||
### Patch Start | ||
if is_custom_filter && self.class.connection.adapter_name.downcase.to_sym == :postgresql && !Rails.env.test? | ||
s << ("to_timestamp(#{table}.#{field},'DD/MM/YYYY HH24:MI') <= to_timestamp('%s','DD/MM/YYYY HH24:MI')" % [quoted_time(to, is_custom_filter)]) | ||
else | ||
if Rails.env.test? || table.classify.constantize.columns_hash[field].type == :date || table.classify.constantize.columns_hash[field].type == :datetime | ||
s << ("#{table}.#{field} <= '%s'" % [quoted_time(to, is_custom_filter)]) | ||
### Patch Start | ||
if is_custom_filter && self.class.connection.adapter_name.downcase.to_sym == :postgresql && !Rails.env.test? | ||
s << ("to_timestamp(#{table}.#{field},'DD/MM/YYYY HH24:MI') <= to_timestamp('%s','DD/MM/YYYY HH24:MI')" % [quoted_time(to, is_custom_filter)]) | ||
else | ||
s << ("STR_TO_DATE(#{table}.#{field},'%d/%m/%Y') <= STR_TO_DATE('" + quoted_time(to, is_custom_filter) + "','%d/%m/%Y')") | ||
if Rails.env.test? || table.classify.constantize.columns_hash[field].type == :date || table.classify.constantize.columns_hash[field].type == :datetime | ||
s << ("#{table}.#{field} <= '%s'" % [quoted_time(to, is_custom_filter)]) | ||
else | ||
s << ("STR_TO_DATE(#{table}.#{field},'%d/%m/%Y') <= STR_TO_DATE('" + quoted_time(to, is_custom_filter) + "','%d/%m/%Y')") | ||
end | ||
end | ||
end | ||
### Patch End | ||
### Patch End | ||
|
||
end | ||
s.join(' AND ') | ||
end | ||
s.join(' AND ') | ||
end | ||
|
||
end | ||
|
||
Query.send(:include, RedmineDatetimeCustomField::QueryPatch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters