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 CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{almost-happy}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Darcy Laycock"]
@@ -12,9 +12,14 @@ module AlmostHappy
12
12
 
13
13
  module ClassMethods
14
14
 
15
- def validate_convertable_format
16
- validates_presence_of :format
17
- validates_with AlmostHappy::FormatValidator
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? || format_changed? || send(:"#{field}_changed?")
51
+ send(:"rendered_#{field}").blank? || format_changed_for?(field) || send(:"#{field}_changed?")
34
52
  end
35
53
 
36
54
  end
@@ -72,7 +72,7 @@ module AlmostHappy
72
72
  end
73
73
 
74
74
  def to_html
75
- conversion_from = @object.format rescue nil
75
+ conversion_from = @object.send(:format_for, @source_field) rescue nil
76
76
  self.class.render_for(conversion_from, original_content)
77
77
  end
78
78
 
@@ -1,14 +1,11 @@
1
1
  module AlmostHappy
2
- class FormatValidator < ActiveModel::Validator
2
+ class FormatValidator < ActiveModel::EachValidator
3
3
 
4
- def validate(record)
5
- if record && record.respond_to?(:format)
6
- format = record.format
7
- if format.present? && !AlmostHappy::Convertor.valid_format?(format)
8
- record.errors.add :format, :invalid_format,
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
@@ -4,7 +4,7 @@ require 'active_support/core_ext/class/attribute'
4
4
  require 'active_support/concern'
5
5
 
6
6
  module AlmostHappy
7
- VERSION = '0.2.3'.freeze
7
+ VERSION = '0.3.0'.freeze
8
8
 
9
9
  extend ActiveSupport::Autoload
10
10
 
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: 17
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
8
  - 3
10
- version: 0.2.3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Darcy Laycock