ardm 0.3.1 → 0.3.2

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.
@@ -15,9 +15,13 @@ module Ardm
15
15
 
16
16
  if Hash === fields.last
17
17
  ar = fields.pop.dup
18
- ar[:on] = ar.delete(:when) if ar[:when] && [:create, :update].include?(ar[:when])
19
- ar[:maximum] = ar.delete(:max) if ar[:max]
20
- ar[:in] = ar.delete(:set) if ar[:set]
18
+ w = Array(ar.delete(:when)).first
19
+ if w && [:create, :update].include?(w)
20
+ ar[:on] = w
21
+ end
22
+ ar[:maximum] = ar.delete(:max) if ar[:max]
23
+ ar[:minimum] = ar.delete(:min) if ar[:min]
24
+ ar[:in] = ar.delete(:set) if ar[:set]
21
25
 
22
26
  removed = ar.slice!(*keep)
23
27
  unless removed.empty?
@@ -40,7 +44,19 @@ module Ardm
40
44
 
41
45
  def validates_presence_of(*fields)
42
46
  fields, options = Ardm::Ar::Validations.extract_options(fields)
43
- validates *fields, presence: options
47
+
48
+ boolean_fields, non_boolean_fields = fields.partition { |f| self.properties[f.to_sym].is_a?(Ardm::Property::Boolean) }
49
+
50
+ if non_boolean_fields.any?
51
+ validates(*non_boolean_fields, presence: options)
52
+ end
53
+
54
+ if boolean_fields.any?
55
+ if options.any?
56
+ $stderr.puts "validates_presence_of options ignored: #{options.inspect}"
57
+ end
58
+ validates(*boolean_fields, inclusion: {in: [true, false]})
59
+ end
44
60
  end
45
61
 
46
62
  def validates_length_of(*fields)
@@ -97,11 +113,21 @@ module Ardm
97
113
  end
98
114
  end
99
115
 
100
- def validates_with_block(att=nil, &block)
101
- validate do |record|
102
- val, message = instance_eval(&block)
103
- unless val
104
- errors.add(att, message)
116
+ def validates_with_block(*args, &block)
117
+ options = args.extract_options!
118
+ attribute = args.shift
119
+
120
+ validate(options) do |_|
121
+ is_valid, message = instance_eval(&block)
122
+
123
+ unless is_valid
124
+ attribute ||= :base
125
+
126
+ if attribute.to_sym == :base
127
+ raise "message is blank #{args.inspect} #{self.inspect} #{block.inspect}" if message.blank?
128
+ end
129
+
130
+ errors.add(attribute, message)
105
131
  end
106
132
  end
107
133
  end
@@ -6,7 +6,12 @@ require 'ardm/dm/collection'
6
6
 
7
7
  module Ardm
8
8
  Record = Ardm::Dm::Record
9
- Validations = ::DataMapper::Validations
9
+ begin
10
+ Validations = ::DataMapper::Validations
11
+ rescue NameError
12
+ # DataMapper::Validations might not be included.
13
+ end
14
+
10
15
  SaveFailureError = ::DataMapper::SaveFailureError
11
16
  RecordNotFound = ::DataMapper::ObjectNotFoundError
12
17
  Property = ::DataMapper::Property
@@ -1,3 +1,3 @@
1
1
  module Ardm
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ardm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-02-02 00:00:00.000000000 Z
13
+ date: 2015-02-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport