geert 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/geert.gemspec +1 -1
- data/lib/geert/association.rb +41 -10
- data/lib/geert/model.rb +1 -1
- data/lib/geert/project.rb +27 -1
- data/lib/geert.rb +0 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/geert.gemspec
CHANGED
data/lib/geert/association.rb
CHANGED
@@ -4,7 +4,7 @@ module Geert
|
|
4
4
|
|
5
5
|
|
6
6
|
def migration
|
7
|
-
"
|
7
|
+
":#{source}, :#{target}#{options_string}" unless exclude?
|
8
8
|
end
|
9
9
|
|
10
10
|
def polymorphic?
|
@@ -15,13 +15,18 @@ module Geert
|
|
15
15
|
reflection.primary_key_name
|
16
16
|
end
|
17
17
|
|
18
|
-
|
18
|
+
def target
|
19
|
+
reflection.table_name
|
20
|
+
end
|
19
21
|
|
20
22
|
def exclude?
|
21
23
|
existing? or polymorphic? or uses_polymorphic?
|
22
24
|
end
|
23
25
|
|
26
|
+
private
|
27
|
+
|
24
28
|
def existing?
|
29
|
+
require "foreigner"
|
25
30
|
ActiveRecord::Base.connection.foreign_keys(source).any? do |fk|
|
26
31
|
fk.options[:column].to_s == column.to_s
|
27
32
|
end
|
@@ -35,16 +40,16 @@ module Geert
|
|
35
40
|
model.table_name
|
36
41
|
end
|
37
42
|
|
38
|
-
def
|
39
|
-
|
43
|
+
def options
|
44
|
+
{ :column => column_option, :dependent => dependency_type }.reject { |key, value| value.nil? }
|
40
45
|
end
|
41
46
|
|
42
|
-
def
|
43
|
-
|
47
|
+
def column_option
|
48
|
+
column if column != "#{target.singularize}_id"
|
44
49
|
end
|
45
50
|
|
46
51
|
def dependency
|
47
|
-
{ :dependent => dependency_type }
|
52
|
+
{ :dependent => dependency_type }
|
48
53
|
end
|
49
54
|
|
50
55
|
def dependency_type
|
@@ -60,12 +65,38 @@ module Geert
|
|
60
65
|
end
|
61
66
|
|
62
67
|
def dependency_option
|
63
|
-
|
68
|
+
opposite_relation.options[:dependent] if opposite_relation
|
69
|
+
end
|
70
|
+
|
71
|
+
def opposite_relation
|
72
|
+
opposite_associations.find do |association|
|
73
|
+
association.table_name == source && association.primary_key_name == column
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def opposite_associations
|
78
|
+
[:has_many, :has_one].map { |type| target_model.reflect_on_all_associations(type) }.flatten
|
79
|
+
end
|
80
|
+
|
81
|
+
def target_model
|
82
|
+
target.singularize.camelize.constantize
|
64
83
|
end
|
65
84
|
|
66
|
-
# Just to make it pretty, remove the curly braces
|
67
85
|
def options_string
|
68
|
-
options.
|
86
|
+
options.empty? ? "" : ", " + space + pretty_options
|
87
|
+
end
|
88
|
+
|
89
|
+
def space
|
90
|
+
" " * (longest_target_size - target.size)
|
91
|
+
end
|
92
|
+
|
93
|
+
def longest_target_size
|
94
|
+
@longest_target_size ||= model.associations.map { |association| association.target.size }.sort.last.to_i
|
95
|
+
end
|
96
|
+
|
97
|
+
# Just to make it pretty, remove the curly braces, and add some spaces
|
98
|
+
def pretty_options
|
99
|
+
options.inspect.gsub(/\A\{(.*)\}\z/, '\\1').gsub("=>", " => ")
|
69
100
|
end
|
70
101
|
|
71
102
|
attr_reader :reflection, :model
|
data/lib/geert/model.rb
CHANGED
data/lib/geert/project.rb
CHANGED
@@ -10,7 +10,33 @@ module Geert
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def migration
|
13
|
-
|
13
|
+
<<-MIGRATION
|
14
|
+
class AddMissingForeignKeys < ActiveRecord::Migration
|
15
|
+
def self.up
|
16
|
+
#{migration_up}
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
#{migration_down}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
MIGRATION
|
24
|
+
end
|
25
|
+
|
26
|
+
def migration_up
|
27
|
+
migration_lines.map(&modify_line("add"))
|
28
|
+
end
|
29
|
+
|
30
|
+
def migration_down
|
31
|
+
migration_lines.split("\n").reverse.join("\n").map(&modify_line("remove"))
|
32
|
+
end
|
33
|
+
|
34
|
+
def migration_lines
|
35
|
+
@migration_lines ||= models.map(&:migration).select(&:present?).join("\n\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
def modify_line(command)
|
39
|
+
lambda { |line| line.present? ? " #{command}_foreign_key #{line}" : "\n" }
|
14
40
|
end
|
15
41
|
|
16
42
|
private
|
data/lib/geert.rb
CHANGED