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.



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

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.



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

def attributes
  @attributes
end

Class Method Details

.define_get_method(attr_name) ⇒ Object



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

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



182
183
184
185
# File 'lib/active_admin_import/model.rb', line 182

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)


71
72
73
# File 'lib/active_admin_import/model.rb', line 71

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

#archive?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/active_admin_import/model.rb', line 87

def archive?
  file_type == CONST::ZIP_TYPE
end

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



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

def assign_attributes(args = {}, new_record = false)
  @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



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

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

#force_encoding?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/active_admin_import/model.rb', line 79

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

#new_record?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/active_admin_import/model.rb', line 75

def new_record?
  @new_record.present?
end

#persisted?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/active_admin_import/model.rb', line 83

def persisted?
  false
end

#read_attribute_for_validation(key) ⇒ Object



57
58
59
# File 'lib/active_admin_import/model.rb', line 57

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