data_active 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/data_active.rb +27 -18
- data/lib/data_active/version.rb +1 -1
- data/test_apps/book_store_rails_32x/Gemfile +1 -1
- metadata +3 -3
data/lib/data_active.rb
CHANGED
@@ -35,12 +35,13 @@ module DataActive
|
|
35
35
|
if options.include? :update or options.include? :sync or options.include? :create
|
36
36
|
assign_attributes_from current_node, :to => active_record
|
37
37
|
if options.include? :fail_on_invalid and !active_record.valid?
|
38
|
-
messages = active_record.errors.messages.map {|attribute, messages| "#{attribute} #{messages.map{|message| message }.join(', ')}"}.join(', ')
|
38
|
+
messages = active_record.errors.messages.map { |attribute, messages| "#{attribute} #{messages.map { |message| message }.join(', ')}" }.join(', ')
|
39
39
|
raise "Found an invalid #{active_record.class.name} with the following errors: #{messages}. Source: #{current_node.to_s}"
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
# Check through associations and apply sync appropriately
|
44
|
+
associations = self.reflect_on_all_associations
|
44
45
|
self.reflect_on_all_associations.each do |association|
|
45
46
|
foreign_key = foreign_key_from(association)
|
46
47
|
klass = association.klass
|
@@ -69,25 +70,31 @@ module DataActive
|
|
69
70
|
end
|
70
71
|
|
71
72
|
when association.macro == :has_one
|
72
|
-
pk_value = active_record.new_record? ? 0 : active_record.attributes[self.primary_key.to_s]
|
73
|
-
single_objects = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_value}]/#{association.name}")
|
74
73
|
klass = association.klass
|
75
|
-
|
74
|
+
if active_record.new_record?
|
75
|
+
single_objects = current_node.xpath(".//#{association.name}")
|
76
|
+
else
|
77
|
+
record = klass.where(foreign_key => active_record.attributes[self.primary_key.to_s]).all
|
78
|
+
single_objects = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{active_record.attributes[self.primary_key.to_s]}]/#{association.name}")
|
79
|
+
end
|
80
|
+
|
76
81
|
if single_objects.count == 1
|
77
82
|
# Check to see if the already record exists
|
78
|
-
if record.
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
83
|
+
if record.present?
|
84
|
+
if record.count == 1
|
85
|
+
db_pk_value = record[0][klass.primary_key]
|
86
|
+
xml_pk_value = Integer(single_objects[0].element_children.xpath("//#{self.name.underscore}/#{klass.primary_key}").text)
|
87
|
+
|
88
|
+
if db_pk_value != xml_pk_value
|
89
|
+
# Different record in xml
|
90
|
+
if options.include?(:sync) or options.include?(:destroy)
|
91
|
+
# Delete the one in the database
|
92
|
+
klass.destroy(record[0][klass.primary_key])
|
93
|
+
end
|
87
94
|
end
|
95
|
+
elsif record.count > 1
|
96
|
+
raise "Too many records for one to one association in the database. Found #{record.count} records of '#{association.name}' for association with '#{self.name}'"
|
88
97
|
end
|
89
|
-
elsif record.count > 1
|
90
|
-
raise "Too many records for one to one association in the database. Found #{record.count} records of '#{association.name}' for association with '#{self.name}'"
|
91
98
|
end
|
92
99
|
|
93
100
|
if options.include?(:create) or options.include?(:update) or options.include?(:sync)
|
@@ -102,9 +109,11 @@ module DataActive
|
|
102
109
|
raise "Too many records for one to one association in the provided XML. Found #{single_objects.count} records of '#{association.name}' for association with '#{self.name}'"
|
103
110
|
else
|
104
111
|
# There are no records in the XML
|
105
|
-
if record.
|
106
|
-
|
107
|
-
|
112
|
+
if record.present?
|
113
|
+
if record.count > 0 and options.include?(:sync) or options.include?(:destroy)
|
114
|
+
# Found some in the database: destroy then
|
115
|
+
klass.destroy_all("#{foreign_key} = #{active_record.attributes[self.primary_key.to_s]}")
|
116
|
+
end
|
108
117
|
end
|
109
118
|
end
|
110
119
|
|
data/lib/data_active/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_active
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-04-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -280,7 +280,7 @@ rubyforge_project: data_active
|
|
280
280
|
rubygems_version: 1.8.22
|
281
281
|
signing_key:
|
282
282
|
specification_version: 3
|
283
|
-
summary: data_active 0.0.
|
283
|
+
summary: data_active 0.0.5
|
284
284
|
test_files:
|
285
285
|
- features/remove_records_missing_in_xml.feature
|
286
286
|
- features/step_definitions/remove_records_missing_in_xml.rb
|