custom_fields 2.12.1 → 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +5 -5
  4. data/lib/custom_fields/extensions/active_support.rb +22 -23
  5. data/lib/custom_fields/extensions/carrierwave.rb +9 -40
  6. data/lib/custom_fields/extensions/mongoid/association/referenced/has_many.rb +49 -0
  7. data/lib/custom_fields/extensions/mongoid/association/referenced/has_one.rb +17 -0
  8. data/lib/custom_fields/extensions/mongoid/association/relatable.rb +30 -0
  9. data/lib/custom_fields/extensions/mongoid/criteria/queryable/smash.rb +3 -6
  10. data/lib/custom_fields/extensions/mongoid/document.rb +4 -8
  11. data/lib/custom_fields/extensions/mongoid/factory.rb +7 -9
  12. data/lib/custom_fields/extensions/mongoid/fields/i18n.rb +17 -17
  13. data/lib/custom_fields/extensions/mongoid/fields/localized.rb +21 -21
  14. data/lib/custom_fields/extensions/mongoid/fields.rb +4 -7
  15. data/lib/custom_fields/extensions/mongoid/validatable/collection_size.rb +86 -0
  16. data/lib/custom_fields/extensions/mongoid/{validations → validatable}/macros.rb +3 -2
  17. data/lib/custom_fields/extensions/origin/smash.rb +4 -5
  18. data/lib/custom_fields/field.rb +39 -36
  19. data/lib/custom_fields/source.rb +46 -44
  20. data/lib/custom_fields/target.rb +11 -12
  21. data/lib/custom_fields/target_helpers.rb +21 -23
  22. data/lib/custom_fields/types/belongs_to.rb +6 -20
  23. data/lib/custom_fields/types/boolean.rb +5 -12
  24. data/lib/custom_fields/types/color.rb +4 -12
  25. data/lib/custom_fields/types/date.rb +20 -22
  26. data/lib/custom_fields/types/date_time.rb +16 -18
  27. data/lib/custom_fields/types/default.rb +16 -24
  28. data/lib/custom_fields/types/email.rb +6 -13
  29. data/lib/custom_fields/types/file.rb +26 -35
  30. data/lib/custom_fields/types/float.rb +10 -13
  31. data/lib/custom_fields/types/has_many.rb +12 -16
  32. data/lib/custom_fields/types/integer.rb +10 -13
  33. data/lib/custom_fields/types/json.rb +18 -29
  34. data/lib/custom_fields/types/many_to_many.rb +14 -19
  35. data/lib/custom_fields/types/money.rb +34 -35
  36. data/lib/custom_fields/types/password.rb +19 -23
  37. data/lib/custom_fields/types/relationship_default.rb +4 -16
  38. data/lib/custom_fields/types/select.rb +43 -51
  39. data/lib/custom_fields/types/string.rb +5 -13
  40. data/lib/custom_fields/types/tags.rb +11 -11
  41. data/lib/custom_fields/types/text.rb +4 -16
  42. data/lib/custom_fields/version.rb +4 -2
  43. data/lib/custom_fields.rb +16 -16
  44. metadata +39 -39
  45. data/lib/custom_fields/extensions/mongoid/relations/options.rb +0 -19
  46. data/lib/custom_fields/extensions/mongoid/relations/referenced/in.rb +0 -18
  47. data/lib/custom_fields/extensions/mongoid/relations/referenced/many.rb +0 -30
  48. data/lib/custom_fields/extensions/mongoid/validations/collection_size.rb +0 -43
@@ -1,30 +0,0 @@
1
- module Mongoid #:nodoc:
2
- module Relations #:nodoc:
3
- module Referenced #:nodoc:
4
-
5
- # This class defines the behaviour for all relations that are a
6
- # one-to-many between documents in different collections.
7
- class Many < Relations::Many
8
-
9
- def build_with_custom_fields(attributes = {}, type = nil)
10
- if base.respond_to?(:custom_fields_for?) && base.custom_fields_for?(relation_metadata.name)
11
- # all the information about how to build the custom class are stored here
12
- recipe = base.custom_fields_recipe_for(relation_metadata.name)
13
- attributes ||= {}
14
- attributes.merge!(custom_fields_recipe: recipe)
15
- # build the class with custom_fields for the first time
16
- type = relation_metadata.klass.klass_with_custom_fields(recipe)
17
- end
18
- build_without_custom_fields(attributes, type)
19
- end
20
-
21
- alias_method :build_without_custom_fields, :build
22
- alias_method :build, :build_with_custom_fields
23
-
24
- # new should point to the new build method
25
- alias :new :build_with_custom_fields
26
- end
27
-
28
- end
29
- end
30
- end
@@ -1,43 +0,0 @@
1
- module Mongoid
2
- module Validations
3
-
4
- # Validates that the specified collections do or do not match a certain
5
- # size.
6
- #
7
- # @example Set up the collection size validator.
8
- #
9
- # class Person
10
- # include Mongoid::Document
11
- # has_many :addresses
12
- #
13
- # validates_collection_size_of :addresses, in: 1..10
14
- # end
15
- class CollectionSizeValidator < Mongoid::Validatable::LengthValidator
16
-
17
- def validate_each_with_collection(record, attribute, value)
18
- value = collection_to_size(record, attribute)
19
-
20
- self.validate_each_without_collection(record, attribute, value)
21
- end
22
-
23
- alias_method :validate_each_without_collection, :validate_each
24
- alias_method :validate_each, :validate_each_with_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