Skip to content

Commit

Permalink
did requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalRemis committed Apr 26, 2020
1 parent 36b94c7 commit 2b98f23
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 110 deletions.
3 changes: 2 additions & 1 deletion dist/client-side-validations.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ var cleanNestedElementName = function cleanNestedElementName(elementName, nested
};

var cleanElementName = function cleanElementName(elementName, validators) {
elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]').replace(/\(\di\)/g, ''); // date/time_select _1/2/3/4/5i fields
elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]');
elementName = elementName.replace(/\(\di\)/g, ''); // date/time_select (1/2/3/4/5i) fields

var nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/);

Expand Down
3 changes: 2 additions & 1 deletion dist/client-side-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@
};

var cleanElementName = function cleanElementName(elementName, validators) {
elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]').replace(/\(\di\)/g, ''); // date/time_select _1/2/3/4/5i fields
elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]');
elementName = elementName.replace(/\(\di\)/g, ''); // date/time_select (1/2/3/4/5i) fields

var nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/);

Expand Down
26 changes: 6 additions & 20 deletions lib/client_side_validations/action_view/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

# rubocop:disable Metrics/ModuleLength
module ClientSideValidations
module ActionView
module Helpers
Expand Down Expand Up @@ -103,25 +102,13 @@ def file_field(method, options = {})
super(method, options)
end

def date_select(method, options = {}, html_options = {})
build_validation_options(method, options)
html_options.delete(:validate)

super(method, options, html_options)
end

def time_select(method, options = {}, html_options = {})
build_validation_options(method, options)
html_options.delete(:validate)

super(method, options, html_options)
end

def datetime_select(method, options = {}, html_options = {})
build_validation_options(method, options)
html_options.delete(:validate)
%i[date_select time_select datetime_select].each do |method_name|
define_method method_name do |method, options = {}, html_options = {}|
build_validation_options(method, options)
html_options.delete(:validate)

super(method, options, html_options)
super(method, options, html_options)
end
end

private
Expand All @@ -142,4 +129,3 @@ def build_validation_options(method, options = {})
end
end
end
# rubocop:enable Metrics/ModuleLength
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $.fn.disableClientSideValidations = function () {

$.fn.enableClientSideValidations = function () {
const selectors = { forms: 'form', inputs: 'input' }

for (var selector in selectors) {
const enablers = selectors[selector]

Expand Down Expand Up @@ -81,7 +82,7 @@ const cleanNestedElementName = (elementName, nestedMatches, validators) => {

const cleanElementName = (elementName, validators) => {
elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]')
.replace(/\(\di\)/g, '') // date/time_select _1/2/3/4/5i fields
elementName = elementName.replace(/\(\di\)/g, '') // date/time_select (1/2/3/4/5i) fields

const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/)

Expand Down
32 changes: 0 additions & 32 deletions test/action_view/cases/test_form_for_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,38 +480,6 @@ def test_datetime_select
assert_dom_equal expected, output_buffer
end

def test_date_field
input_html = ''

form_for(@post, validate: true) do |f|
input_html = f.date_field(:cost)
concat input_html
end

validators = { 'post[cost]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
input_html
end

assert_dom_equal expected, output_buffer
end

def test_date_time_field
input_html = ''

form_for(@post, validate: true) do |f|
input_html = f.datetime_field(:cost)
concat input_html
end

validators = { 'post[cost]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
input_html
end

assert_dom_equal expected, output_buffer
end

def test_as_form_option_with_new_record_rails
form_for(@post, as: :article, validate: true) do
concat content_tag(:span, 'Dummy Content')
Expand Down
48 changes: 48 additions & 0 deletions test/action_view/cases/test_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,54 @@ def test_form_with_time_zone_select_with_validate_options
assert_dom_equal expected, output_buffer
end

def test_date_select
input_html = ''

form_with(model: @post, validate: true) do |f|
input_html = f.date_select(:cost)
concat input_html
end

validators = { 'post[cost]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
input_html
end

assert_dom_equal expected, output_buffer
end

def test_time_select
input_html = ''

form_with(model: @post, validate: true) do |f|
input_html = f.time_select(:cost)
concat input_html
end

validators = { 'post[cost]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
input_html
end

assert_dom_equal expected, output_buffer
end

def test_datetime_select
input_html = ''

form_with(model: @post, validate: true) do |f|
input_html = f.datetime_select(:cost)
concat input_html
end

validators = { 'post[cost]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
input_html
end

assert_dom_equal expected, output_buffer
end

def test_form_with_as_form_option_with_new_record_rails
form_with(model: @post, as: :article, validate: true) do
concat content_tag(:span, 'Dummy Content')
Expand Down
36 changes: 36 additions & 0 deletions test/action_view/cases/test_legacy_form_for_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@ def test_file_field
assert_dom_equal expected, output_buffer
end

def test_date_select
form_for(@post) do |f|
concat f.date_select(:cost)
end

expected = whole_form_for('/posts', 'new_post', 'new_post') do
date_select :post, :cost
end

assert_dom_equal expected, output_buffer
end

def test_time_select
form_for(@post) do |f|
concat f.time_select(:cost)
end

expected = whole_form_for('/posts', 'new_post', 'new_post') do
time_select :post, :cost
end

assert_dom_equal expected, output_buffer
end

def test_datetime_select
form_for(@post) do |f|
concat f.datetime_select(:cost)
end

expected = whole_form_for('/posts', 'new_post', 'new_post') do
datetime_select :post, :cost
end

assert_dom_equal expected, output_buffer
end

def test_check_box
form_for(@post) do |f|
concat f.check_box(:cost)
Expand Down
36 changes: 36 additions & 0 deletions test/action_view/cases/test_legacy_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ def test_form_with_file_field
assert_dom_equal expected, output_buffer
end

def test_date_select
form_with(model: @post) do |f|
concat f.date_select(:cost)
end

expected = whole_form_with('/posts') do
date_select :post, :cost
end

assert_dom_equal expected, output_buffer
end

def test_time_select
form_with(model: @post) do |f|
concat f.time_select(:cost)
end

expected = whole_form_with('/posts') do
time_select :post, :cost
end

assert_dom_equal expected, output_buffer
end

def test_datetime_select
form_with(model: @post) do |f|
concat f.datetime_select(:cost)
end

expected = whole_form_with('/posts') do
datetime_select :post, :cost
end

assert_dom_equal expected, output_buffer
end

def test_form_with_check_box
form_with(model: @post) do |f|
concat f.check_box(:cost)
Expand Down
Loading

0 comments on commit 2b98f23

Please sign in to comment.