forked from drogus/tolk
/
translation.rb
71 lines (54 loc) · 1.53 KB
/
translation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Tolk
class Translation < ActiveRecord::Base
set_table_name "tolk_translations"
serialize :text
validates_presence_of :text, :unless => proc {|r| r.primary }
validates_uniqueness_of :phrase_id, :scope => :locale_id
belongs_to :phrase, :class_name => 'Tolk::Phrase'
belongs_to :locale, :class_name => 'Tolk::Locale'
attr_accessor :force_set_primary_update
before_save :set_primary_updated
before_save :set_previous_text
attr_accessor :primary
before_validation :fix_text_type, :unless => proc {|r| r.primary }
def primary_translation
@_primary_translation ||= begin
if locale && !locale.primary?
phrase.translations.primary
end
end
end
def text=(value)
super unless value.to_s == text
end
def value
if text.is_a?(String)
YAML.load text
else
text
end
end
private
def fix_text_type
if primary_translation.present?
if self.text.is_a?(String) && !primary_translation.text.is_a?(String)
self.text = begin
YAML.load(self.text.strip)
rescue ArgumentError
nil
end
end
self.text = nil if primary_translation.text.class != self.text.class
end
true
end
def set_primary_updated
self.primary_updated = self.force_set_primary_update ? true : false
true
end
def set_previous_text
self.previous_text = self.text_was if text_changed?
true
end
end
end