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
52
53
54
55
# 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

  file_extname = File.extname(file)

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

  file_contents = File.read(file)

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

  case file_extname
  when ".yml", ".yaml"
    if !YAML.safe_load(file_contents).is_a?(Hash)
      errors.add(:file, "Invalid #{file_extname.sub(".","")} file.")
      return
    end

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