Class: Lit::Source

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/lit/source.rb

Constant Summary collapse

LOCALES_PATH =
'/api/v1/locales.json'
LOCALIZATION_KEYS_PATH =
'/api/v1/localization_keys.json'
LOCALIZATIONS_PATH =
'/api/v1/localizations.json'
LAST_CHANGE_PATH =
'/api/v1/last_change.json'

Instance Method Summary collapse

Instance Method Details

#api_keyObject

VALIDATIONS



13
14
# File 'app/models/lit/source.rb', line 13

validates :api_key, :identifier, :url,
presence: true

#get_last_changeObject



26
27
28
29
# File 'app/models/lit/source.rb', line 26

def get_last_change
  result = get_from_remote(LAST_CHANGE_PATH)
  result['last_change'] unless result.nil?
end

#incomming_localizationsObject

ASSOCIATIONS



10
# File 'app/models/lit/source.rb', line 10

has_many :incomming_localizations

#synchronizeObject



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
# File 'app/models/lit/source.rb', line 31

def synchronize
  after = last_updated_at.nil? ? nil : last_updated_at.to_s(:db)
  result = get_from_remote(LOCALIZATIONS_PATH, after: after)
  unless result.nil?
    if result.is_a?(Array)
      result.each do |r|
        il = IncommingLocalization.new
        if ::Rails::VERSION::MAJOR < 4
          il = IncommingLocalization.where(incomming_id: r['id']).first_or_initialize
        else
          il = IncommingLocalization.find_or_initialize_by(incomming_id: r['id'])
        end
        il.source = self
        il.locale_str = r['locale_str']
        il.locale = Locale.where(locale: il.locale_str).first
        il.localization_key_str = r['localization_key_str']
        il.localization_key = LocalizationKey.where(localization_key: il.localization_key_str).first
        unless il.is_duplicate?(r['value'])
          il.save!
          IncommingLocalization.where(id: il.id).
            update_all(translated_value: r['value'])
        end
      end
      last_change = get_last_change
      last_change = DateTime.parse(last_change) unless last_change.nil?
      touch_last_updated_at(last_change)
      update_column(:sync_complete, true)
      save
    end
  end
end

#touch_last_updated_at!Object



63
64
65
66
# File 'app/models/lit/source.rb', line 63

def touch_last_updated_at!
  touch_last_updated_at
  save
end