Class: RailsI18nManager::Forms::TranslationFileForm
- Defined in:
- app/lib/rails_i18n_manager/forms/translation_file_form.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#mark_inactive_translations ⇒ Object
Returns the value of attribute mark_inactive_translations.
-
#overwrite_existing ⇒ Object
Returns the value of attribute overwrite_existing.
-
#translation_app_id ⇒ Object
Returns the value of attribute translation_app_id.
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
#file ⇒ Object
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_translations ⇒ Object
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_existing ⇒ Object
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_id ⇒ Object
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_file ⇒ Object
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 |