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

#file_contents_stringObject



24
25
26
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 24

def file_contents_string
  @file_contents_string ||= file.read
end

#file_extnameObject



20
21
22
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 20

def file_extname
  @file_extname ||= File.extname(file)
end

#parsed_file_contentsObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 28

def parsed_file_contents
  if defined?(@parsed_file_contents)
    return @parsed_file_contents
  end

  case file_extname
  when ".yml", ".yaml"
    @parsed_file_contents = YAML.safe_load(file_contents_string)
  when ".json"
    @parsed_file_contents = JSON.parse(file_contents_string)
  end
end

#validate_fileObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/lib/rails_i18n_manager/forms/translation_file_form.rb', line 41

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

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

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

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

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