Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 17:30:29 +01:00
Merge branch 'master' into sprockets-fix
Dieser Commit ist enthalten in:
Commit
28400ee24e
6 geänderte Dateien mit 99 neuen und 12 gelöschten Zeilen
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
{{d-button
|
{{d-button
|
||||||
label="refresh"
|
label="refresh"
|
||||||
icon="refresh"
|
icon="sync"
|
||||||
action="refresh"
|
action="refresh"
|
||||||
class="refresh"}}
|
class="refresh"}}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,3 +1,42 @@
|
||||||
import DateInput from "discourse/components/date-input";
|
import DateInput from "discourse/components/date-input";
|
||||||
|
import loadScript from "discourse/lib/load-script";
|
||||||
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
|
import I18n from "I18n";
|
||||||
|
/* global Pikaday:true */
|
||||||
|
|
||||||
export default DateInput.extend();
|
export default DateInput.extend({
|
||||||
|
useNativePicker: false,
|
||||||
|
|
||||||
|
@discourseComputed()
|
||||||
|
placeholder() {
|
||||||
|
return this.format;
|
||||||
|
},
|
||||||
|
|
||||||
|
_loadPikadayPicker(container) {
|
||||||
|
return loadScript("/javascripts/pikaday.js").then(() => {
|
||||||
|
let defaultOptions = {
|
||||||
|
field: this.element.querySelector(".date-picker"),
|
||||||
|
container: container || this.element.querySelector(".picker-container"),
|
||||||
|
bound: container === null,
|
||||||
|
format: this.format,
|
||||||
|
firstDay: 1,
|
||||||
|
i18n: {
|
||||||
|
previousMonth: I18n.t("dates.previous_month"),
|
||||||
|
nextMonth: I18n.t("dates.next_month"),
|
||||||
|
months: moment.months(),
|
||||||
|
weekdays: moment.weekdays(),
|
||||||
|
weekdaysShort: moment.weekdaysShort(),
|
||||||
|
},
|
||||||
|
onSelect: (date) => this._handleSelection(date),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.relativeDate) {
|
||||||
|
defaultOptions = Object.assign({}, defaultOptions, {
|
||||||
|
minDate: moment(this.relativeDate).toDate(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Pikaday(Object.assign({}, defaultOptions, this._opts()));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
|
@ -2,4 +2,5 @@
|
||||||
date=date
|
date=date
|
||||||
onChange=(action "onChange")
|
onChange=(action "onChange")
|
||||||
tabindex=field.tabindex
|
tabindex=field.tabindex
|
||||||
|
format=field.format
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -52,7 +52,7 @@ class ::CustomWizard::UpdateValidator
|
||||||
@updater.errors.add(field_id, I18n.t('wizard.field.invalid_file', label: label, types: file_types))
|
@updater.errors.add(field_id, I18n.t('wizard.field.invalid_file', label: label, types: file_types))
|
||||||
end
|
end
|
||||||
|
|
||||||
if ['date', 'date_time'].include?(type) && value.present? && !validate_date(value)
|
if ['date', 'date_time'].include?(type) && value.present? && !validate_date(value, format)
|
||||||
@updater.errors.add(field_id, I18n.t('wizard.field.invalid_date'))
|
@updater.errors.add(field_id, I18n.t('wizard.field.invalid_date'))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -88,13 +88,8 @@ class ::CustomWizard::UpdateValidator
|
||||||
.include?(File.extname(value['original_filename'])[1..-1])
|
.include?(File.extname(value['original_filename'])[1..-1])
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_date(value)
|
def validate_date(value, format)
|
||||||
begin
|
v8.eval("moment('#{value}', '#{format}', true).isValid()")
|
||||||
Date.parse(value)
|
|
||||||
true
|
|
||||||
rescue ArgumentError
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_time(value)
|
def validate_time(value)
|
||||||
|
@ -126,4 +121,12 @@ class ::CustomWizard::UpdateValidator
|
||||||
def standardise_boolean(value)
|
def standardise_boolean(value)
|
||||||
ActiveRecord::Type::Boolean.new.cast(value)
|
ActiveRecord::Type::Boolean.new.cast(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def v8
|
||||||
|
return @ctx if @ctx
|
||||||
|
|
||||||
|
@ctx = PrettyText.v8
|
||||||
|
PrettyText.ctx_load(@ctx, "#{Rails.root}/vendor/assets/javascripts/moment.js")
|
||||||
|
@ctx
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,11 +8,11 @@ class CustomWizard::WizardSerializer < CustomWizard::BasicWizardSerializer
|
||||||
:completed,
|
:completed,
|
||||||
:required,
|
:required,
|
||||||
:permitted,
|
:permitted,
|
||||||
:uncategorized_category_id
|
:uncategorized_category_id,
|
||||||
|
:categories
|
||||||
|
|
||||||
has_many :steps, serializer: ::CustomWizard::StepSerializer, embed: :objects
|
has_many :steps, serializer: ::CustomWizard::StepSerializer, embed: :objects
|
||||||
has_one :user, serializer: ::BasicUserSerializer, embed: :objects
|
has_one :user, serializer: ::BasicUserSerializer, embed: :objects
|
||||||
has_many :categories, serializer: ::BasicCategorySerializer, embed: :objects
|
|
||||||
has_many :groups, serializer: ::BasicGroupSerializer, embed: :objects
|
has_many :groups, serializer: ::BasicGroupSerializer, embed: :objects
|
||||||
|
|
||||||
def completed
|
def completed
|
||||||
|
@ -56,4 +56,8 @@ class CustomWizard::WizardSerializer < CustomWizard::BasicWizardSerializer
|
||||||
def include_uncategorized_category_id?
|
def include_uncategorized_category_id?
|
||||||
object.needs_categories
|
object.needs_categories
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def categories
|
||||||
|
object.categories.map { |c| c.to_h }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -132,4 +132,44 @@ describe CustomWizard::UpdateValidator do
|
||||||
updater.errors.messages[:step_2_field_6].first
|
updater.errors.messages[:step_2_field_6].first
|
||||||
).to eq(nil)
|
).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'validates date fields' do
|
||||||
|
@template[:steps][1][:fields][0][:format] = "DD-MM-YYYY"
|
||||||
|
CustomWizard::Template.save(@template)
|
||||||
|
|
||||||
|
updater = perform_validation('step_2', step_2_field_1: '13-11-2021')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_1].first
|
||||||
|
).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'doesn\'t validate date field if the format is not respected' do
|
||||||
|
@template[:steps][1][:fields][0][:format] = "MM-DD-YYYY"
|
||||||
|
CustomWizard::Template.save(@template)
|
||||||
|
|
||||||
|
updater = perform_validation('step_2', step_2_field_1: '13-11-2021')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_1].first
|
||||||
|
).to eq(I18n.t('wizard.field.invalid_date'))
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'validates date time fields' do
|
||||||
|
@template[:steps][1][:fields][2][:format] = "DD-MM-YYYY HH:mm:ss"
|
||||||
|
CustomWizard::Template.save(@template)
|
||||||
|
|
||||||
|
updater = perform_validation('step_2', step_2_field_3: '13-11-2021 09:15:00')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_3].first
|
||||||
|
).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'doesn\'t validate date time field if the format is not respected' do
|
||||||
|
@template[:steps][1][:fields][2][:format] = "MM-DD-YYYY HH:mm:ss"
|
||||||
|
CustomWizard::Template.save(@template)
|
||||||
|
|
||||||
|
updater = perform_validation('step_2', step_2_field_3: '13-11-2021 09:15')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_3].first
|
||||||
|
).to eq(I18n.t('wizard.field.invalid_date'))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Laden …
In neuem Issue referenzieren