Class: ThemeTranslationManager

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization
Defined in:
lib/theme_translation_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale:, key:, default:, theme:) ⇒ ThemeTranslationManager

Returns a new instance of ThemeTranslationManager.



26
27
28
29
30
31
# File 'lib/theme_translation_manager.rb', line 26

def initialize(locale:, key:, default:, theme:)
  @locale = locale
  @key = key
  @default = default
  @theme = theme
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/theme_translation_manager.rb', line 5

def default
  @default
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/theme_translation_manager.rb', line 5

def key
  @key
end

#themeObject (readonly)

Returns the value of attribute theme.



5
6
7
# File 'lib/theme_translation_manager.rb', line 5

def theme
  @theme
end

Class Method Details

.list_from_hash(locale:, hash:, theme:, parent_keys: []) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/theme_translation_manager.rb', line 7

def self.list_from_hash(locale:, hash:, theme:, parent_keys: [])
  list = []
  hash
    .map do |key, value|
      this_key_array = parent_keys + [key]
      if value.is_a?(Hash)
        self.list_from_hash(
          locale: locale,
          hash: value,
          theme: theme,
          parent_keys: this_key_array,
        )
      else
        self.new(locale: locale, theme: theme, key: this_key_array.join("."), default: value)
      end
    end
    .flatten
end

Instance Method Details

#create_record!(value) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/theme_translation_manager.rb', line 63

def create_record!(value)
  record =
    ThemeTranslationOverride.create!(
      locale: @locale,
      translation_key: @key,
      theme: @theme,
      value: value,
    )
end

#db_recordObject



53
54
55
56
57
# File 'lib/theme_translation_manager.rb', line 53

def db_record
  theme.theme_translation_overrides.to_a.find do |i|
    i.locale.to_s == @locale.to_s && i.translation_key.to_s == key.to_s
  end
end

#has_record?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/theme_translation_manager.rb', line 59

def has_record?
  db_record.present?
end

#valueObject



33
34
35
# File 'lib/theme_translation_manager.rb', line 33

def value
  has_record? ? db_record.value : default
end

#value=(new_value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/theme_translation_manager.rb', line 37

def value=(new_value)
  if new_value == @default
    db_record.destroy! if db_record
    new_value
  else
    if has_record?
      record = db_record
      record.value = new_value.to_s
      record.save!
    else
      record = create_record!(new_value.to_s)
    end
    record.value
  end
end