Class: ActiveAdminImport::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/active_admin_import/model.rb

Defined Under Namespace

Modules: CONST

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Model

Returns a new instance of Model.



44
45
46
47
48
# File 'lib/active_admin_import/model.rb', line 44

def initialize(args = {})
  @new_record = true
  @attributes = {}
  assign_attributes(default_attributes.merge(args), true)
end

Instance Attribute Details

#attributesObject (readonly) Also known as: to_hash

Returns the value of attribute attributes.



42
43
44
# File 'lib/active_admin_import/model.rb', line 42

def attributes
  @attributes
end

Class Method Details

.define_get_method(attr_name) ⇒ Object



191
192
193
194
# File 'lib/active_admin_import/model.rb', line 191

def define_get_method(attr_name)
  return if method_defined? "#{attr_name}="
  define_method("#{attr_name}=") { |new_value| @attributes[attr_name] = new_value }
end

.define_set_method(attr_name) ⇒ Object



186
187
188
189
# File 'lib/active_admin_import/model.rb', line 186

def define_set_method(attr_name)
  return if method_defined? attr_name
  define_method(attr_name) { attributes[attr_name] }
end

Instance Method Details

#allow_archive?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_admin_import/model.rb', line 73

def allow_archive?
  attributes[:allow_archive].present?
end

#archive?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/active_admin_import/model.rb', line 89

def archive?
  file_type == CONST::ZIP_TYPE
end

#assign_attributes(args = {}, new_record = false) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/active_admin_import/model.rb', line 50

def assign_attributes(args = {}, new_record = false)
  args[:file] = nil unless args.key?(:file)
  @attributes.merge!(args)
  @new_record = new_record
  args.keys.each do |key|
    define_methods_for(key.to_sym)
  end if args.is_a?(Hash)
end

#default_attributesObject



63
64
65
66
67
68
69
70
71
# File 'lib/active_admin_import/model.rb', line 63

def default_attributes
  {
    allow_archive: true,
    csv_headers: [],
    file: nil,
    force_encoding: 'UTF-8',
    hint: ''
  }
end

#force_encoding?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/active_admin_import/model.rb', line 81

def force_encoding?
  attributes[:force_encoding].present?
end

#new_record?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_admin_import/model.rb', line 77

def new_record?
  @new_record.present?
end

#persisted?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/active_admin_import/model.rb', line 85

def persisted?
  false
end

#read_attribute_for_validation(key) ⇒ Object



59
60
61
# File 'lib/active_admin_import/model.rb', line 59

def read_attribute_for_validation(key)
  @attributes[key.to_sym]
end