almost-happy 0.2.3 → 0.3.0
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/almost-happy.gemspec +1 -1
- data/lib/almost_happy/convertable.rb +22 -4
- data/lib/almost_happy/convertor.rb +1 -1
- data/lib/almost_happy/format_validator.rb +6 -9
- data/lib/almost_happy.rb +1 -1
- metadata +3 -3
data/almost-happy.gemspec
CHANGED
@@ -12,9 +12,14 @@ module AlmostHappy
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
|
15
|
-
def validate_convertable_format
|
16
|
-
|
17
|
-
|
15
|
+
def validate_convertable_format(*attrs)
|
16
|
+
if attrs.empty?
|
17
|
+
attrs = [:format]
|
18
|
+
else
|
19
|
+
attrs = attrs.map { |a| a.to_s.gsub(/(_format)?$/, '_format').to_sym }
|
20
|
+
end
|
21
|
+
validates_presence_of *attrs
|
22
|
+
validates_with AlmostHappy::FormatValidator, :attributes => attrs
|
18
23
|
end
|
19
24
|
|
20
25
|
end
|
@@ -23,6 +28,19 @@ module AlmostHappy
|
|
23
28
|
|
24
29
|
protected
|
25
30
|
|
31
|
+
def format_field_for(field)
|
32
|
+
named_version = :"#{field}_format"
|
33
|
+
respond_to?(named_version) ? named_version : :format
|
34
|
+
end
|
35
|
+
|
36
|
+
def format_for(field)
|
37
|
+
send format_field_for(field)
|
38
|
+
end
|
39
|
+
|
40
|
+
def format_changed_for?(field)
|
41
|
+
send :"#{format_field_for(field)}_changed?"
|
42
|
+
end
|
43
|
+
|
26
44
|
def process_convertable_sections
|
27
45
|
convertable_fields.each do |field|
|
28
46
|
AlmostHappy::Convertor.new(self, field).render if should_render?(field)
|
@@ -30,7 +48,7 @@ module AlmostHappy
|
|
30
48
|
end
|
31
49
|
|
32
50
|
def should_render?(field)
|
33
|
-
send(:"rendered_#{field}").blank? ||
|
51
|
+
send(:"rendered_#{field}").blank? || format_changed_for?(field) || send(:"#{field}_changed?")
|
34
52
|
end
|
35
53
|
|
36
54
|
end
|
@@ -1,14 +1,11 @@
|
|
1
1
|
module AlmostHappy
|
2
|
-
class FormatValidator < ActiveModel::
|
2
|
+
class FormatValidator < ActiveModel::EachValidator
|
3
3
|
|
4
|
-
def
|
5
|
-
if
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
:default => 'is not a valid format (must be one of {{formats}})',
|
10
|
-
:formats => AlmostHappy::Convertor.format_names
|
11
|
-
end
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
if value.present? && !AlmostHappy::Convertor.valid_format?(value)
|
6
|
+
record.errors.add attribute, :invalid_format,
|
7
|
+
:default => 'is not a valid format (must be one of %{formats})',
|
8
|
+
:formats => AlmostHappy::Convertor.format_names
|
12
9
|
end
|
13
10
|
end
|
14
11
|
|
data/lib/almost_happy.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: almost-happy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 2
|
9
8
|
- 3
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Darcy Laycock
|