Class: RailsAdminImport::ImportModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin_import/import_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abstract_model) ⇒ ImportModel

Returns a new instance of ImportModel.



6
7
8
9
10
# File 'lib/rails_admin_import/import_model.rb', line 6

def initialize(abstract_model)
  @abstract_model = abstract_model
  @config = abstract_model.config.import
  @model = abstract_model.model
end

Instance Attribute Details

#abstract_modelObject (readonly)

Returns the value of attribute abstract_model.



12
13
14
# File 'lib/rails_admin_import/import_model.rb', line 12

def abstract_model
  @abstract_model
end

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/rails_admin_import/import_model.rb', line 12

def config
  @config
end

#modelObject (readonly)

Returns the value of attribute model.



12
13
14
# File 'lib/rails_admin_import/import_model.rb', line 12

def model
  @model
end

Instance Method Details

#associated_config(field) ⇒ Object



83
84
85
# File 'lib/rails_admin_import/import_model.rb', line 83

def associated_config(field)
  field.associated_model_config.import
end

#associated_model_fields(field) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/rails_admin_import/import_model.rb', line 87

def associated_model_fields(field)
  @associated_fields ||= {}
  if associated_config(field).mapping_key_list.present?
    @associated_fields[field] ||= associated_config(field).mapping_key_list
  else
    @associated_fields[field] ||= associated_config(field).visible_fields.select { |f|
      !f.association?
    }.map(&:name)
  end
end

#associated_object(field, mapping_field, value) ⇒ Object



73
74
75
76
77
# File 'lib/rails_admin_import/import_model.rb', line 73

def associated_object(field, mapping_field, value)
  klass = association_class(field)
  klass.where(mapping_field => value).first or
    raise AssociationNotFound, "#{klass}.#{mapping_field} = #{value}"
end

#association_class(field) ⇒ Object



79
80
81
# File 'lib/rails_admin_import/import_model.rb', line 79

def association_class(field)
  field.association.klass
end

#association_fieldsObject



41
42
43
44
45
# File 'lib/rails_admin_import/import_model.rb', line 41

def association_fields
  @association_fields ||= importable_fields.select { |f|
    f.association? && !f.association.polymorphic?
  }
end

#belongs_to_fieldsObject



53
54
55
56
57
# File 'lib/rails_admin_import/import_model.rb', line 53

def belongs_to_fields
  @belongs_to_fields ||= single_association_fields.select { |f|
    f.type == :belongs_to_association
  }
end

#display_nameObject



14
15
16
# File 'lib/rails_admin_import/import_model.rb', line 14

def display_name
  abstract_model.config.label
end

#has_multiple_values?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/rails_admin_import/import_model.rb', line 98

def has_multiple_values?(field_name)
  plural_name = pluralize_field(field_name)
  many_association_fields.any? { |field| field.name == field_name || field.name == plural_name }
end

#importable_fields(model_config = config) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rails_admin_import/import_model.rb', line 26

def importable_fields(model_config = config)
  @importable_fields ||= {}
  @importable_fields[model_config] ||= model_config.visible_fields.reject do |f|
    # Exclude id, created_at and updated_at
    model_config.default_excluded_fields.include? f.name
  end
end

#label_for_model(object) ⇒ Object



18
19
20
# File 'lib/rails_admin_import/import_model.rb', line 18

def label_for_model(object)
  object.public_send(label_method)
end

#label_methodObject



22
23
24
# File 'lib/rails_admin_import/import_model.rb', line 22

def label_method
  @label_method ||= abstract_model.config.object_label_method
end

#many_association_fieldsObject



59
60
61
62
63
# File 'lib/rails_admin_import/import_model.rb', line 59

def many_association_fields
  @many_association_fields ||= association_fields.select { |f|
    f.multiple?
  }
end

#model_fields(model_config = config) ⇒ Object



34
35
36
37
38
39
# File 'lib/rails_admin_import/import_model.rb', line 34

def model_fields(model_config = config)
  @model_fields ||= {}
  @model_fields[model_config] ||= importable_fields(model_config).select { |f|
    !f.association? || f.association.polymorphic?
  }
end

#pluralize_field(field_name) ⇒ Object



103
104
105
106
107
108
# File 'lib/rails_admin_import/import_model.rb', line 103

def pluralize_field(field_name)
  @plural_fields ||= many_association_fields.map(&:name).each_with_object({}) { |name, h|
    h[name.to_s.singularize.to_sym] = name
  }
  @plural_fields[field_name] || field_name
end

#single_association_fieldsObject



47
48
49
50
51
# File 'lib/rails_admin_import/import_model.rb', line 47

def single_association_fields
  @single_association_fields ||= association_fields.select { |f|
    !f.multiple?
  }
end

#update_lookup_field_namesObject



65
66
67
68
69
70
71
# File 'lib/rails_admin_import/import_model.rb', line 65

def update_lookup_field_names
  if @config.mapping_key_list.present?
    @update_lookup_field_names = @config.mapping_key_list
  else
    @update_lookup_field_names ||= model_fields.map(&:name) + belongs_to_fields.map(&:associated_primary_key)
  end
end