has_safe_dates 0.0.2 → 0.0.3

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.
@@ -8,27 +8,30 @@ module HasSafeDates
8
8
  module ClassMethods
9
9
 
10
10
  def has_safe_fields_config
11
- @has_safe_fields_config ||= {}
11
+ @@has_safe_fields_config ||= {}
12
12
  end
13
13
 
14
14
  def has_safe_dates(*args)
15
15
  options = args.extract_options!
16
+ has_safe_fields_config[self] = options
16
17
 
17
18
  if options[:error_message].present?
18
- has_safe_fields_config[:error_message] = options[:error_message]
19
+ has_safe_fields_config[self][:error_message] = options[:error_message]
19
20
  else
20
- has_safe_fields_config[:error_message] = I18n.translate('activerecord.errors.messages')[:invalid] || 'is invalid'
21
+ has_safe_fields_config[self][:error_message] = I18n.translate('activerecord.errors.messages')[:invalid] || 'is invalid'
21
22
  end
22
23
 
23
24
  if args.blank?
24
25
  raise ArgumentError, 'Must define the fields you want to be converted to safe dates with "has_safe_dates :my_field_name_date, :my_other_field_name_date"'
25
26
  end
26
- has_safe_fields_config[:fields] = args.map(&:to_s)
27
- has_safe_fields_config[:fields].each do |field|
27
+ has_safe_fields_config[self][:fields] = args.map(&:to_s)
28
+ has_safe_fields_config[self][:fields].each do |field|
28
29
  define_method "#{field.to_s}=" do |value|
29
30
  if value.present?
30
31
  value = Chronic.parse(value.to_s)
31
- self.errors.add(field, self.class.has_safe_fields_config[:error_message]) unless value.present?
32
+ unless value.present?
33
+ self.errors.add(field, self.class.has_safe_fields_config[self.class][:error_message])
34
+ end
32
35
  end
33
36
  super value
34
37
  end
@@ -37,7 +40,11 @@ module HasSafeDates
37
40
  end
38
41
 
39
42
  def read_value_from_parameter(name, values_hash_from_param)
40
- if self.class.has_safe_fields_config[:fields].include?(name.to_s)
43
+ if self.class.has_safe_fields_config[self.class]
44
+ fields = self.class.has_safe_fields_config[self.class][:fields]
45
+ end
46
+
47
+ if fields.present? && fields.include?(name.to_s)
41
48
 
42
49
  max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param, 6)
43
50
  return nil if (1..3).any? {|position| values_hash_from_param[position].blank?}
@@ -1,3 +1,3 @@
1
1
  module HasSafeDates
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_safe_dates
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - kylejginavan