custom_fields 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/MIT-LICENSE +1 -1
  2. data/README.textile +4 -4
  3. data/config/locales/de.yml +11 -5
  4. data/config/locales/en.yml +4 -0
  5. data/config/locales/fr.yml +4 -0
  6. data/config/locales/ru.yml +15 -0
  7. data/lib/custom_fields.rb +37 -27
  8. data/lib/custom_fields/extensions/active_support.rb +1 -1
  9. data/lib/custom_fields/extensions/carrierwave.rb +2 -2
  10. data/lib/custom_fields/extensions/mongoid/factory.rb +3 -3
  11. data/lib/custom_fields/extensions/mongoid/fields.rb +4 -3
  12. data/lib/custom_fields/extensions/mongoid/fields/localized.rb +39 -0
  13. data/lib/custom_fields/extensions/mongoid/relations/referenced/in.rb +1 -1
  14. data/lib/custom_fields/extensions/mongoid/relations/referenced/many.rb +3 -3
  15. data/lib/custom_fields/field.rb +14 -8
  16. data/lib/custom_fields/source.rb +23 -14
  17. data/lib/custom_fields/target.rb +16 -8
  18. data/lib/custom_fields/target_helpers.rb +6 -6
  19. data/lib/custom_fields/types/belongs_to.rb +4 -4
  20. data/lib/custom_fields/types/boolean.rb +1 -1
  21. data/lib/custom_fields/types/date.rb +2 -2
  22. data/lib/custom_fields/types/default.rb +5 -6
  23. data/lib/custom_fields/types/email.rb +60 -0
  24. data/lib/custom_fields/types/file.rb +2 -2
  25. data/lib/custom_fields/types/float.rb +52 -0
  26. data/lib/custom_fields/types/has_many.rb +6 -8
  27. data/lib/custom_fields/types/integer.rb +54 -0
  28. data/lib/custom_fields/types/many_to_many.rb +3 -3
  29. data/lib/custom_fields/types/money.rb +146 -0
  30. data/lib/custom_fields/types/relationship_default.rb +2 -2
  31. data/lib/custom_fields/types/select.rb +16 -13
  32. data/lib/custom_fields/types/tags.rb +35 -0
  33. data/lib/custom_fields/types/text.rb +1 -1
  34. data/lib/custom_fields/version.rb +1 -1
  35. metadata +65 -76
  36. data/init.rb +0 -2
  37. data/lib/custom_fields/extensions/mongoid/fields/internal/localized.rb +0 -86
data/init.rb DELETED
@@ -1,2 +0,0 @@
1
- # Init
2
- require File.dirname(__FILE__) + '/lib/custom_fields'
@@ -1,86 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Fields #:nodoc:
4
- module Internal #:nodoc:
5
-
6
- # Defines the behaviour for localized string fields.
7
- class Localized
8
-
9
- attr_accessor :original_field_type
10
-
11
- class << self
12
-
13
- # Instantiate 2 field types:
14
- # - a wrapper in charge of dealing with the translations
15
- # - the original field type to help to serialize / deserialize types
16
- #
17
- # @param [ Hash ] options The field options.
18
- #
19
- # @option options [ Class ] :type The class of the field.
20
- # @option options [ Object ] :default The default value for the field.
21
- # @option options [ String ] :label The field's label.
22
- #
23
- def instantiate_with_localize(name, options = {})
24
- instantiate_without_localize(name, options).tap do |field|
25
- field.original_field_type = Mappings.for(options[:type], options[:identity]).instantiate(name, options)
26
- end
27
- end
28
-
29
- alias_method_chain :instantiate, :localize
30
-
31
- end
32
-
33
- # Deserialize the object based on the current locale. Will look in the
34
- # hash for the current locale.
35
- #
36
- # @example Get the deserialized value.
37
- # field.deserialize({ "en" => "testing" })
38
- #
39
- # @param [ Hash ] object The hash of translations.
40
- #
41
- # @return [ String ] The value for the current locale.
42
- #
43
- # @since 2.3.0
44
- def deserialize(object)
45
- return nil if object.nil?
46
-
47
- # puts "deserializing...#{locale.inspect} / #{object.inspect}" # DEBUG
48
- value = if !object.respond_to?(:keys) # if no translation hash is given, we return the object itself
49
- object
50
- elsif I18n.fallbacks?
51
- object[I18n.fallbacks[locale.to_sym].map(&:to_s).find { |loc| !object[loc].nil? }]
52
- else
53
- object[locale.to_s]
54
- end
55
-
56
- self.original_field_type.deserialize(value)
57
- end
58
-
59
- # Convert the provided string into a hash for the locale.
60
- #
61
- # @example Serialize the value.
62
- # field.serialize("testing")
63
- #
64
- # @param [ String ] object The string to convert.
65
- #
66
- # @return [ Hash ] The locale with string translation.
67
- #
68
- # @since 2.3.0
69
- def serialize(object)
70
- # puts "serializing...#{locale} / #{object.inspect} / #{options.inspect}" # DEBUG
71
-
72
- value = self.original_field_type.serialize(object)
73
-
74
- { locale.to_s => value }
75
- end
76
-
77
- protected
78
-
79
- def locale
80
- I18n.locale
81
- end
82
-
83
- end
84
- end
85
- end
86
- end