Class: Breeze::Translation

Inherits:
Object
  • Object
show all
Defined in:
app/models/breeze/translation.rb

Overview

gives a handle on the translations

actual translations are stored in the object (section/card) but dealing with hashes of hashes quickly gets unwieldy, hence this class

Strings are written lasily into the object, ie no translation, no text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, language) ⇒ Translation

Returns a new instance of Translation.



17
18
19
20
21
22
# File 'app/models/breeze/translation.rb', line 17

def initialize(object , language)
  @object = object
  @language = language
  raise "no object type" unless object.object_type
  raise "not configured language #{language}" unless Breeze.has_language(language)
end

Instance Attribute Details

#objectObject (readonly)

fields :objectid , :object_type # poly: card or section (id without _, no association)

fields :language # the language for which the is a translation
fields :text , :header, :sub_header #actual text fields


15
16
17
# File 'app/models/breeze/translation.rb', line 15

def object
  @object
end

Class Method Details

.find(id) ⇒ Object

find according to id, ie class_num



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/breeze/translation.rb', line 85

def self.find(id)
  clazz , lang , num = id.split("-")
  num = num.to_i
  case clazz
  when "Section"
    object = Section.find(num)
  when "Card"
    object = Card.find( num )
  else
    raise "clazz not recognized #{clazz}"
  end
  object.get_translation(lang)
end

Instance Method Details

#get_headerObject



51
52
53
54
# File 'app/models/breeze/translation.rb', line 51

def get_header
  return "" unless data = object.data[language]
  data[:header] || ""
end

#get_textObject



40
41
42
43
# File 'app/models/breeze/translation.rb', line 40

def get_text
  return "" unless data = object.data[language]
  data[:text] || ""
end

#headerObject



34
35
36
37
38
# File 'app/models/breeze/translation.rb', line 34

def header
  t = get_header
  return t unless t.blank?
  return object.header
end

#idObject



24
25
26
# File 'app/models/breeze/translation.rb', line 24

def id
  "#{object.object_type}-#{language}-#{object.id}"
end

#is_old?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'app/models/breeze/translation.rb', line 62

def is_old?
  return false if(text.blank? and header.blank?)
  return true unless self.updated_at
  object.updated_at > self.updated_at
end

#set_header(header) ⇒ Object



56
57
58
59
60
# File 'app/models/breeze/translation.rb', line 56

def set_header(header)
  object.data[language] = {} unless object.data[language]
  object.data[language][:updated_at] = Time.now
  object.data[language][:header] = header
end

#set_text(text) ⇒ Object



45
46
47
48
49
# File 'app/models/breeze/translation.rb', line 45

def set_text(text)
  object.data[language] = {} unless object.data[language]
  object.data[language][:updated_at] = Time.now
  object.data[language][:text] = text
end

#textObject



28
29
30
31
32
# File 'app/models/breeze/translation.rb', line 28

def text
  t = get_text
  return t unless t.blank?
  return object.text
end

#update(hash, editor) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'app/models/breeze/translation.rb', line 73

def update(hash , editor)
  return unless hash
  set_text(hash[:text]) if hash.has_key?(:text)
  set_header(hash[:header]) if hash.has_key?(:header)
  object.data[language][:updated_by] = editor
  object.data[language][:updated_at] = Time.now

  ChangeSet.current.add(self.class.name , "#{language}:#{object.change_name}", editor)
  object.class.save_all
end

#updated_atObject



68
69
70
71
# File 'app/models/breeze/translation.rb', line 68

def updated_at
  return nil unless object.data[language]
  object.data[language][:updated_at]
end