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



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

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



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

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)


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

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

#archive?Boolean

Returns:

  • (Boolean)


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

def archive?
  file_type == CONST::ZIP_TYPE
end

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



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

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



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

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

#force_encoding?Boolean

Returns:

  • (Boolean)


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

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

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  @new_record.present?
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  false
end

#read_attribute_for_validation(key) ⇒ Object



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

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