Class: Workarea::DataFile::Format
- Inherits:
-
Object
- Object
- Workarea::DataFile::Format
- Defined in:
- app/models/workarea/data_file/format.rb
Instance Attribute Summary collapse
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
Instance Method Summary collapse
-
#assign_password(model, password = nil) ⇒ Object
Special case for Users, since
password
is a hashed field. - #clean_ignored_fields(object) ⇒ Object
-
#initialize(operation = nil) ⇒ Format
constructor
A new instance of Format.
-
#model_class_for(attrs = {}) ⇒ Class
Return the class specified by the
_type
attribute, or the defaultmodel_class
for the operation. -
#serialize(models) ⇒ String
This method exists for showing sample file content and for testing.
- #slug ⇒ Object
- #unit ⇒ Object
Constructor Details
#initialize(operation = nil) ⇒ Format
Returns a new instance of Format.
7 8 9 |
# File 'app/models/workarea/data_file/format.rb', line 7 def initialize(operation = nil) @operation = operation end |
Instance Attribute Details
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
4 5 6 |
# File 'app/models/workarea/data_file/format.rb', line 4 def operation @operation end |
Instance Method Details
#assign_password(model, password = nil) ⇒ Object
Special case for Users, since password
is a hashed field. Set the password attribute from the column if given, otherwise assign a randomized password to the User so they can be imported via CSV/JSON without knowing what the hashed result of their intended password is.
63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/workarea/data_file/format.rb', line 63 def assign_password(model, password = nil) return unless model.is_a?(User::Passwords) return if model.persisted? && password.blank? model.password = if password.present? password elsif model.new_record? "#{SecureRandom.hex(10)}_aA1" end end |
#clean_ignored_fields(object) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/workarea/data_file/format.rb', line 44 def clean_ignored_fields(object) if object.is_a?(Hash) Hash[ object.except(*Workarea.config.data_file_ignored_fields).map do |key, value| [key, clean_ignored_fields(value)] end ] elsif object.is_a?(Array) object.map { |o| clean_ignored_fields(o) } else object end end |
#model_class_for(attrs = {}) ⇒ Class
Return the class specified by the _type
attribute, or the default model_class
for the operation.
79 80 81 82 |
# File 'app/models/workarea/data_file/format.rb', line 79 def model_class_for(attrs = {}) type = attrs.symbolize_keys.slice(:type, :_type).values.first type&.constantize || model_class end |
#serialize(models) ⇒ String
This method exists for showing sample file content and for testing.
All the funkiness is so this doesn’t have to be written for each format. Simply implement your #export method and #serialize will come along for the ride.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/workarea/data_file/format.rb', line 28 def serialize(models) file = Tempfile.new(self.class.name) copy = self.class.new scoped_models = Array.wrap(models) copy.define_singleton_method(:models) { scoped_models } copy.define_singleton_method(:tempfile) { file } copy.export! file.rewind file.read ensure file.close file.unlink end |
#slug ⇒ Object
11 12 13 |
# File 'app/models/workarea/data_file/format.rb', line 11 def slug self.class.name.demodulize.underscore end |
#unit ⇒ Object
15 16 17 |
# File 'app/models/workarea/data_file/format.rb', line 15 def unit I18n.t("workarea.data_file.#{self.class.name.demodulize.underscore}.unit") end |