Module: ActiverecordDataImporter

Extended by:
ActiveSupport::Concern
Defined in:
lib/activerecord_data_importer.rb,
lib/activerecord_data_importer/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: CsvConverter

Constant Summary collapse

VERSION =
"0.1.3"

Instance Method Summary collapse

Instance Method Details

#delete_remain_relation(remain_instances) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/activerecord_data_importer.rb', line 131

def delete_remain_relation(remain_instances)
  return false if remain_instances.blank?
  remain_instances.each do |remain_instance|
    if self.class.respond_to? :with_writable
      remain_instance.class.with_writable { remain_instance.destroy }
    else
      remain_instance.destroy
    end
  end
  true
end

#update_origin(self_attributes, latest_version) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/activerecord_data_importer.rb', line 88

def update_origin(self_attributes, latest_version)
  self_attributes
  self.attributes = self_attributes
  return false unless self.changed?
  self.update_with_version latest_version
  true
end

#update_relation(all_relation_attributes) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/activerecord_data_importer.rb', line 96

def update_relation(all_relation_attributes)
  latest_version = self.class.get_latest_version
  all_relation_attributes.select do |key, value|
    case value
    when Hash
      relation_instance = self.try(key) || self.try("build_#{key}")
      relation_instance.update_row_data value, latest_version
    when Array
      relation_instances = self.try(key).to_a
      updated_flag = false
      value.each_with_index do |relation_attributes, i|
        relation_instance = relation_instances[i] || self.try(key).build
        relation_updated = relation_instance.update_row_data relation_attributes, latest_version
        updated_flag ||= relation_updated
      end
      relation_deleted = self.delete_remain_relation(relation_instances[value.size..-1])
      updated_flag || relation_deleted
    else
      false
    end
  end.present?
end

#update_row_data(row_data, latest_version) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/activerecord_data_importer.rb', line 71

def update_row_data(row_data, latest_version)
  origin_updated = false
  relation_updated = false
  self_attributes, another_attributes = self.class.divide_attributes row_data
  origin_updated = self.update_origin self_attributes, latest_version
  return origin_updated if another_attributes.blank?
  relation_attributes = self.class.divide_relation_key another_attributes
  return origin_updated if relation_attributes.blank?
  relation_updated = self.update_relation another_attributes
  if (!origin_updated && relation_updated)
    self.update_with_version latest_version
    true
  else
    false
  end
end

#update_with_version(latest_version) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/activerecord_data_importer.rb', line 120

def update_with_version(latest_version)
  if self.respond_to?(:version)
    self.version = latest_version + 1
  end
  if self.class.respond_to? :with_writable
    self.class.with_writable { self.save }
  else
    self.save
  end
end