custom_fields 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/config/locales/en.yml +7 -2
- data/config/locales/fr.yml +3 -1
- data/lib/custom_fields/extensions/mongoid/validations/collection_size.rb +43 -0
- data/lib/custom_fields/extensions/mongoid/validations/macros.rb +25 -0
- data/lib/custom_fields/extensions/origin/smash.rb +33 -0
- data/lib/custom_fields/types/has_many.rb +1 -1
- data/lib/custom_fields/types/many_to_many.rb +5 -1
- data/lib/custom_fields/version.rb +1 -1
- data/lib/custom_fields.rb +3 -0
- metadata +6 -3
data/config/locales/en.yml
CHANGED
@@ -6,11 +6,16 @@ en:
|
|
6
6
|
select: Select
|
7
7
|
boolean: Checkbox
|
8
8
|
date: Date
|
9
|
-
integer: Integer
|
10
9
|
file: File
|
11
10
|
integer: Integer
|
12
11
|
float: Float
|
13
12
|
money: Money
|
13
|
+
email: E-mail
|
14
|
+
tags: Tags
|
14
15
|
belongs_to: Belongs to
|
15
16
|
has_many: Has many
|
16
|
-
many_to_many: Many To Many
|
17
|
+
many_to_many: Many To Many
|
18
|
+
|
19
|
+
errors:
|
20
|
+
messages:
|
21
|
+
at_least_one_element: "must have at least one element"
|
data/config/locales/fr.yml
CHANGED
@@ -11,6 +11,7 @@ fr:
|
|
11
11
|
integer: Entier
|
12
12
|
float: Décimal
|
13
13
|
money: Monnaie
|
14
|
+
tags: Libellés
|
14
15
|
belongs_to: Appartient à
|
15
16
|
has_many: A plusieurs
|
16
17
|
many_to_many: A plusieurs et appartient à
|
@@ -20,4 +21,5 @@ fr:
|
|
20
21
|
|
21
22
|
errors:
|
22
23
|
messages:
|
23
|
-
blank: "doit être rempli(e)"
|
24
|
+
blank: "doit être rempli(e)"
|
25
|
+
at_least_one_element: "doit avoir au moins un élément"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Validations
|
4
|
+
|
5
|
+
# Validates that the specified collections do or do not match a certain
|
6
|
+
# size.
|
7
|
+
#
|
8
|
+
# @example Set up the collection size validator.
|
9
|
+
#
|
10
|
+
# class Person
|
11
|
+
# include Mongoid::Document
|
12
|
+
# has_many :addresses
|
13
|
+
#
|
14
|
+
# validates_collection_size_of :addresses, in: 1..10
|
15
|
+
# end
|
16
|
+
class CollectionSizeValidator < LengthValidator
|
17
|
+
|
18
|
+
def validate_each_with_collection(record, attribute, value)
|
19
|
+
value = collection_to_size(record, attribute)
|
20
|
+
|
21
|
+
self.validate_each_without_collection(record, attribute, value)
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method_chain :validate_each, :collection
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def collection_to_size(record, attribute)
|
29
|
+
relation = record.relations[attribute.to_s]
|
30
|
+
|
31
|
+
source = case relation.macro
|
32
|
+
when :embeds_many, :has_many
|
33
|
+
record.send(attribute)
|
34
|
+
when :has_and_belongs_to_many
|
35
|
+
record.send(relation.key.to_sym)
|
36
|
+
end
|
37
|
+
|
38
|
+
OpenStruct.new(length: source.try(:size) || 0)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Validations
|
3
|
+
module Macros
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Validates the size of a collection.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# class Person
|
10
|
+
# include Mongoid::Document
|
11
|
+
# has_many :addresses
|
12
|
+
#
|
13
|
+
# validates_collection_size_of :addresses, minimum: 1
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# @param [ Array ] args The names of the fields to validate.
|
17
|
+
#
|
18
|
+
# @since 2.4.0
|
19
|
+
def validates_collection_size_of(*args)
|
20
|
+
validates_with(Mongoid::Validations::CollectionSizeValidator, _merge_attributes(args))
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Origin
|
3
|
+
|
4
|
+
# This is a smart hash for use with options and selectors.
|
5
|
+
class Smash < Hash
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
# Get the normalized value for the key. If localization is in play the
|
10
|
+
# current locale will be appended to the key in MongoDB dot notation.
|
11
|
+
#
|
12
|
+
# FIXME (Did).
|
13
|
+
# This version DOES NOT USE ::I18n.locale directly.
|
14
|
+
# See the localized.rb file for more explanation.
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
#
|
18
|
+
# @example Get the normalized key name.
|
19
|
+
# smash.normalized_key("field", serializer)
|
20
|
+
#
|
21
|
+
# @param [ String ] name The name of the field.
|
22
|
+
# @param [ Object ] serializer The optional field serializer.
|
23
|
+
#
|
24
|
+
# @return [ String ] The normalized key.
|
25
|
+
#
|
26
|
+
# @since 1.0.0
|
27
|
+
def normalized_key(name, serializer)
|
28
|
+
# serializer && serializer.localized? ? "#{name}.#{::I18n.locale}" : name
|
29
|
+
serializer && serializer.localized? ? "#{name}.#{::Mongoid::Fields::I18n.locale}" : name
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -59,7 +59,7 @@ module CustomFields
|
|
59
59
|
klass.accepts_nested_attributes_for rule['name'], allow_destroy: true
|
60
60
|
|
61
61
|
if rule['required']
|
62
|
-
klass.
|
62
|
+
klass.validates_collection_size_of rule['name'], minimum: 1, message: :at_least_one_element, on: :update
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -38,6 +38,10 @@ module CustomFields
|
|
38
38
|
|
39
39
|
klass.has_and_belongs_to_many rule['name'], class_name: rule['class_name'], inverse_of: rule['inverse_of'], order: rule['order_by'] do
|
40
40
|
|
41
|
+
# def at_least_one_element?
|
42
|
+
# (base.send(metadata.key.to_sym).try(:size) || 0) > 0
|
43
|
+
# end
|
44
|
+
|
41
45
|
def filtered(conditions = {}, order_by = nil)
|
42
46
|
list = conditions.empty? ? self : self.where(conditions)
|
43
47
|
|
@@ -56,7 +60,7 @@ module CustomFields
|
|
56
60
|
end
|
57
61
|
|
58
62
|
if rule['required']
|
59
|
-
klass.
|
63
|
+
klass.validates_collection_size_of rule['name'], minimum: 1, message: :at_least_one_element
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
data/lib/custom_fields.rb
CHANGED
@@ -31,6 +31,9 @@ end
|
|
31
31
|
extensions/mongoid/fields.rb
|
32
32
|
extensions/mongoid/fields/i18n.rb
|
33
33
|
extensions/mongoid/fields/localized.rb
|
34
|
+
extensions/mongoid/validations/collection_size.rb
|
35
|
+
extensions/mongoid/validations/macros.rb
|
36
|
+
extensions/origin/smash.rb
|
34
37
|
types/default
|
35
38
|
types/string
|
36
39
|
types/text
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06
|
12
|
+
date: 2013-07-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -208,6 +208,9 @@ files:
|
|
208
208
|
- lib/custom_fields/extensions/mongoid/fields.rb
|
209
209
|
- lib/custom_fields/extensions/mongoid/relations/referenced/in.rb
|
210
210
|
- lib/custom_fields/extensions/mongoid/relations/referenced/many.rb
|
211
|
+
- lib/custom_fields/extensions/mongoid/validations/collection_size.rb
|
212
|
+
- lib/custom_fields/extensions/mongoid/validations/macros.rb
|
213
|
+
- lib/custom_fields/extensions/origin/smash.rb
|
211
214
|
- lib/custom_fields/field.rb
|
212
215
|
- lib/custom_fields/source.rb
|
213
216
|
- lib/custom_fields/target.rb
|
@@ -249,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
249
252
|
version: '0'
|
250
253
|
segments:
|
251
254
|
- 0
|
252
|
-
hash:
|
255
|
+
hash: 4168219824119967597
|
253
256
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
254
257
|
none: false
|
255
258
|
requirements:
|