Skip to content

Commit ab095f2

Browse files
authored
Merge pull request #783 from DavyJonesLocker/add-many-associations-support
Add support for many association validations
2 parents 4645082 + bb21e40 commit ab095f2

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## master / unreleased
44

55
* [FEATURE] Drop Ruby 2.3 support
6+
* [FEATURE] Add support for many association validations ([#783](https://github.com/DavyJonesLocker/client_side_validations/pull/783))
67

78
## 16.2.0 / 2020-04-10
89

lib/client_side_validations/action_view/form_helper.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ def add_validator(validator_hash, validation_hash, name, attr)
9999
if validation_hash.key?(attr)
100100
validator_hash[name] = validation_hash[attr]
101101
elsif attr.to_s.ends_with?('_id')
102-
add_validator_with_association validator_hash, validation_hash, name, attr
102+
association_name = attr.to_s.gsub(/_id\Z/, '').to_sym
103+
add_validator_with_association validator_hash, validation_hash, name, association_name
104+
elsif attr.to_s.ends_with?('_ids')
105+
association_name = attr.to_s.gsub(/_ids\Z/, '').pluralize.to_sym
106+
add_validator_with_association validator_hash, validation_hash, name, association_name
103107
end
104108
end
105109

106-
def add_validator_with_association(validator_hash, validation_hash, name, attr)
107-
association_name = attr.to_s.gsub(/_id\Z/, '').to_sym
110+
def add_validator_with_association(validator_hash, validation_hash, name, association_name)
108111
return unless validation_hash.key?(association_name)
109112

110113
validator_hash[name] = validation_hash[association_name]

test/action_view/cases/test_form_for_helpers.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,30 @@ def test_collection_select_with_association
383383
assert_dom_equal expected, output_buffer
384384
end
385385

386+
def test_collection_check_boxes_with_many_association
387+
form_for(@post, validate: true) do |f|
388+
concat f.collection_check_boxes(:tag_ids, [], :id, :title)
389+
end
390+
391+
validators = {
392+
'post[tag_ids]' => {
393+
length: [{
394+
messages: {
395+
minimum: 'is too short (minimum is 0 characters)',
396+
maximum: 'is too long (maximum is 3 characters)'
397+
},
398+
minimum: 0,
399+
maximum: 3
400+
}]
401+
}
402+
}
403+
404+
expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
405+
form_field('input', name: 'post[tag_ids][]', type: 'hidden', value: '')
406+
end
407+
assert_dom_equal expected, output_buffer
408+
end
409+
386410
def test_collection_select_with_validate_options
387411
form_for(@post, validate: true) do |f|
388412
concat f.collection_select(:cost, [], :id, :name, {}, validate: false)

test/action_view/models/post.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Post
99
attr_accessor :title, :author_name, :body, :secret, :written_on, :cost
1010
validates :cost, :body, presence: true
1111
validates :body, length: { minimum: 200 }
12+
validates :tags, length: { minimum: 0, maximum: 3 }
1213

1314
# Simulate default Rails 5's association
1415
validates :category, presence: { message: :required }
@@ -28,4 +29,7 @@ def comments_attributes=(attributes); end
2829

2930
attr_accessor :category, :category_id
3031
def category_attributes=(attributes); end
32+
33+
attr_accessor :tags, :tag_ids
34+
def tags_attributes=(attributes); end
3135
end

test/action_view/models/tag.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
class Tag
4+
extend ActiveModel::Naming
5+
extend ActiveModel::Translation
6+
include ActiveModel::Validations
7+
include ActiveModel::Conversion
8+
9+
attr_reader :id, :title, :description
10+
11+
def initialize(params = {})
12+
params.each do |attr, value|
13+
public_send("#{attr}=", value)
14+
end
15+
end
16+
17+
def persisted?
18+
false
19+
end
20+
end

0 commit comments

Comments
 (0)