Class: RailsI18nManager::Forms::TranslationFileForm

Inherits:
Base
  • Object
show all
Defined in:
app/lib/rails_i18n_manager/forms/translation_file_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #model_name, #to_key

Constructor Details

This class inherits a constructor from RailsI18nManager::Forms::Base

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 5

def file
  @file
end

#mark_inactive_translationsObject

Returns the value of attribute mark_inactive_translations.



6
7
8
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 6

def mark_inactive_translations
  @mark_inactive_translations
end

#overwrite_existingObject

Returns the value of attribute overwrite_existing.



5
6
7
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 5

def overwrite_existing
  @overwrite_existing
end

#translation_app_idObject

Returns the value of attribute translation_app_id.



5
6
7
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 5

def translation_app_id
  @translation_app_id
end

Instance Method Details

#validate_fileObject



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
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 20

def validate_file
  if file.blank?
    errors.add(:file, "Must upload a valid translation file.")
    return
  end

  if [".yml", ".json"].exclude?(File.extname(file))
    errors.add(:file, "Invalid file format. Must be yml or json file.")
    return
  end

  if File.read(file).blank?
    errors.add(:file, "Empty file provided.")
    return
  end

  case File.extname(file)
  when ".yml"
    if !YAML.safe_load(File.read(file)).is_a?(Hash)
      errors.add(:file, "Invalid yml file.")
      return
    end

  when ".json"
    begin
      JSON.parse(File.read(file))
    rescue JSON::ParserError
      errors.add(:file, "Invalid json file.")
      return
    end
  end
end