seeder 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/seeder.rb +20 -4
- data/lib/seeder/version.rb +1 -1
- metadata +5 -5
data/lib/seeder.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
class Seeder
|
2
2
|
|
3
3
|
class << self
|
4
|
+
QUOTING = {
|
5
|
+
"ActiveRecord::ConnectionAdapters::MysqlAdapter" => "`",
|
6
|
+
"ActiveRecord::ConnectionAdapters::Mysql2Adapter" => "`"
|
7
|
+
}
|
4
8
|
|
5
9
|
def existing_data_keys(ar_model)
|
6
10
|
keys = @primary_keys[ar_model.name]
|
@@ -25,17 +29,29 @@ class Seeder
|
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
32
|
+
def quote(value)
|
33
|
+
q = QUOTING[ActiveRecord::Base.connection.class.to_s] || "'"
|
34
|
+
"#{q}#{value}#{q}"
|
35
|
+
end
|
36
|
+
|
28
37
|
def add_new_or_changed_data(ar_model)
|
29
38
|
keys = @primary_keys[ar_model.name]
|
30
39
|
find_method = "find_by_#{keys.join('_and_')}"
|
40
|
+
|
31
41
|
@data[ar_model.name].collect do |attributes|
|
32
42
|
relevant_keys = attributes.keys & ar_model.column_names.map(&:to_sym)
|
33
43
|
existing = ar_model.send(find_method, *attributes.values_at(*keys))
|
34
|
-
next if existing && attributes.values_at(*relevant_keys) == existing.attributes.symbolize_keys.values_at(*relevant_keys)
|
35
44
|
values = attributes.values_at(*relevant_keys)
|
36
|
-
|
37
|
-
|
38
|
-
|
45
|
+
|
46
|
+
if existing
|
47
|
+
updates = attributes.slice(*relevant_keys).map { |k,v| "#{quote(k)} = #{value_to_sql(v)}" }.join(", ")
|
48
|
+
conditions = keys.map { |k| "#{quote(k)} = #{value_to_sql(existing.send(k))}" }.join(" AND ")
|
49
|
+
%(UPDATE #{ar_model.table_name} SET #{updates} WHERE #{conditions})
|
50
|
+
else
|
51
|
+
columns = relevant_keys.join(", ")
|
52
|
+
inserts = attributes.slice(*relevant_keys).values.map { |v| value_to_sql(v) }.join(", ")
|
53
|
+
%(INSERT INTO #{ar_model.table_name} (#{columns}) VALUES (#{inserts}))
|
54
|
+
end
|
39
55
|
end.compact
|
40
56
|
end
|
41
57
|
|
data/lib/seeder/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seeder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-09-
|
13
|
+
date: 2011-09-16 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: &
|
17
|
+
requirement: &2166523000 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
version: 2.6.0
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2166523000
|
26
26
|
description: Keep your app's seed data in one file and update it easily, while respecting
|
27
27
|
key constraints
|
28
28
|
email: bsingh@wegowise.com
|
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
version: 1.3.6
|
61
61
|
requirements: []
|
62
62
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.8.
|
63
|
+
rubygems_version: 1.8.10
|
64
64
|
signing_key:
|
65
65
|
specification_version: 3
|
66
66
|
summary: Seed your data
|