fides 0.0.6 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fides/version.rb +1 -1
- data/lib/fides.rb +88 -91
- data/test/lib/fides/fides_test.rb +3 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a7f704749dcd05ef1186b0a46f0cad900ab00ed
|
4
|
+
data.tar.gz: 9f933f47a0b8fd70fe54fb1cb816a376cd42730d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbe549233fb1bd8f5bdcd42dea5c2ab84e4a5e958b7322db74a35ae5c2ac439dd4c691ed4fa5c879a73efde0b671adf458c847ec8bda676b09411789f7dd4a5f
|
7
|
+
data.tar.gz: 692e61dafb6e55e1367bcb66ca473c1a80957bcb22f0770fe76539c60561fa74c1fcae0761ed3a44a4d1648d77c2f8cac297a9486a64a569e90c49717ad5697f
|
data/lib/fides/version.rb
CHANGED
data/lib/fides.rb
CHANGED
@@ -7,118 +7,115 @@ module Fides
|
|
7
7
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
|
-
|
10
|
+
def add_polymorphic_triggers(opts)
|
11
|
+
raise ArgumentError, "missing :associated_models from options hash" if !opts.has_key?(:associated_models)
|
12
|
+
raise ArgumentError, "missing :polymorphic_model from options hash" if !opts.has_key?(:polymorphic_model)
|
13
|
+
associated_models = opts[:associated_models]
|
14
|
+
polymorphic_model = opts[:polymorphic_model]
|
15
|
+
interface_name = opts.has_key?(:interface_name) ? opts[:interface_name] : interface_name(polymorphic_model)
|
11
16
|
|
12
|
-
|
13
|
-
|
14
|
-
raise ArgumentError, "missing :polymorphic_model from options hash" if !opts.has_key?(:polymorphic_model)
|
15
|
-
associated_models = opts[:associated_models]
|
16
|
-
polymorphic_model = opts[:polymorphic_model]
|
17
|
-
interface_name = opts.has_key?(:interface_name) ? opts[:interface_name] : interface_name(polymorphic_model)
|
17
|
+
sql = get_create_function_sql(interface_name, associated_models, polymorphic_model)
|
18
|
+
sql << get_delete_function_sql(interface_name, associated_models, polymorphic_model)
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
execute sql
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
def remove_polymorphic_triggers(opts)
|
24
|
+
polymorphic_model = opts[:polymorphic_model]
|
25
|
+
interface_name = opts.has_key?(:interface_name) ? opts[:interface_name] : interface_name(polymorphic_model)
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
execute %{
|
28
|
+
DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;
|
29
|
+
DROP FUNCTION IF EXISTS check_#{interface_name}_delete_integrity() CASCADE;
|
30
|
+
}
|
31
|
+
end
|
28
32
|
|
29
|
-
|
30
|
-
DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;
|
31
|
-
DROP FUNCTION IF EXISTS check_#{interface_name}_delete_integrity() CASCADE;
|
32
|
-
}
|
33
|
-
end
|
33
|
+
private
|
34
34
|
|
35
|
-
|
35
|
+
# TODO: Is it safe to just grab the first polymorphic association?
|
36
|
+
def interface_name(model_name)
|
37
|
+
model_name.constantize.reflect_on_all_associations.select { |r| r if r.options[:polymorphic] }.first.name
|
38
|
+
end
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
def get_create_function_sql(interface_name, models, polymorphic_model)
|
41
|
+
sql = "DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;"
|
42
|
+
|
43
|
+
sql << %{
|
44
|
+
CREATE FUNCTION check_#{interface_name}_create_integrity() RETURNS TRIGGER AS '
|
45
|
+
BEGIN
|
46
|
+
IF NEW.#{interface_name}_type = ''#{models[0]}'' AND EXISTS (
|
47
|
+
SELECT id FROM #{models[0].constantize.table_name} WHERE id = NEW.#{interface_name}_id) THEN
|
48
|
+
RETURN NEW;
|
49
|
+
}
|
41
50
|
|
42
|
-
|
43
|
-
sql = "DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;"
|
44
|
-
|
51
|
+
models[1..-1].each do |model|
|
45
52
|
sql << %{
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
SELECT id FROM #{models[0].constantize.table_name} WHERE id = NEW.#{interface_name}_id) THEN
|
50
|
-
RETURN NEW;
|
53
|
+
ELSEIF NEW.#{interface_name}_type = ''#{model}'' AND EXISTS (
|
54
|
+
SELECT id FROM #{model.constantize.table_name} WHERE id = NEW.#{interface_name}_id) THEN
|
55
|
+
RETURN NEW;
|
51
56
|
}
|
57
|
+
end
|
58
|
+
|
59
|
+
sql << %{
|
60
|
+
ELSE
|
61
|
+
RAISE EXCEPTION ''No % model with id %.'', NEW.#{interface_name}_type, NEW.#{interface_name}_id;
|
62
|
+
RETURN NULL;
|
63
|
+
END IF;
|
64
|
+
END'
|
65
|
+
LANGUAGE plpgsql;
|
52
66
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
RETURN NEW;
|
58
|
-
}
|
59
|
-
end
|
60
|
-
|
61
|
-
sql << %{
|
62
|
-
ELSE
|
63
|
-
RAISE EXCEPTION ''No % model with id %.'', NEW.#{interface_name}_type, NEW.#{interface_name}_id;
|
64
|
-
RETURN NULL;
|
65
|
-
END IF;
|
66
|
-
END'
|
67
|
-
LANGUAGE plpgsql;
|
68
|
-
|
69
|
-
CREATE TRIGGER check_#{interface_name}_create_integrity_trigger
|
70
|
-
BEFORE INSERT OR UPDATE ON #{polymorphic_model.constantize.table_name}
|
71
|
-
FOR EACH ROW EXECUTE PROCEDURE check_#{interface_name}_create_integrity();
|
72
|
-
}
|
67
|
+
CREATE TRIGGER check_#{interface_name}_create_integrity_trigger
|
68
|
+
BEFORE INSERT OR UPDATE ON #{polymorphic_model.constantize.table_name}
|
69
|
+
FOR EACH ROW EXECUTE PROCEDURE check_#{interface_name}_create_integrity();
|
70
|
+
}
|
73
71
|
|
74
|
-
|
75
|
-
|
72
|
+
return sql
|
73
|
+
end
|
76
74
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
def get_delete_function_sql(interface_name, models, polymorphic_model)
|
76
|
+
polymorphic_model_table_name = polymorphic_model.constantize.table_name
|
77
|
+
|
78
|
+
sql = ""
|
79
|
+
sql << %{
|
80
|
+
CREATE FUNCTION check_#{interface_name}_delete_integrity() RETURNS TRIGGER AS '
|
81
|
+
BEGIN
|
82
|
+
IF TG_TABLE_NAME = ''#{models[0].constantize.table_name}'' AND EXISTS (
|
83
|
+
SELECT id FROM #{polymorphic_model_table_name}
|
84
|
+
WHERE #{interface_name}_type = ''#{models[0]}'' AND #{interface_name}_id = OLD.id) THEN
|
85
|
+
RAISE EXCEPTION ''There are records in #{polymorphic_model_table_name} that refer to %. You must delete those records first.'', OLD;
|
86
|
+
}
|
87
|
+
|
88
|
+
models[1..-1].each do |model|
|
81
89
|
sql << %{
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
WHERE #{interface_name}_type = ''#{models[0]}'' AND #{interface_name}_id = OLD.id) THEN
|
87
|
-
RAISE EXCEPTION ''There are records in #{polymorphic_model_table_name} that refer to %. You must delete those records first.'', OLD;
|
90
|
+
ELSEIF TG_TABLE_NAME = ''#{model.constantize.table_name}'' AND EXISTS (
|
91
|
+
SELECT id FROM #{polymorphic_model_table_name}
|
92
|
+
WHERE #{interface_name}_type = ''#{model}'' AND #{interface_name}_id = OLD.id) THEN
|
93
|
+
RAISE EXCEPTION ''There are records in #{polymorphic_model_table_name} that refer to %. You must delete those records first.'', OLD;
|
88
94
|
}
|
95
|
+
end
|
89
96
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
97
|
+
sql << %{
|
98
|
+
ELSE
|
99
|
+
RETURN NULL;
|
100
|
+
END IF;
|
101
|
+
END'
|
102
|
+
LANGUAGE plpgsql;
|
103
|
+
}
|
98
104
|
|
105
|
+
models.each do |model|
|
106
|
+
table_name = model.constantize.table_name
|
107
|
+
|
99
108
|
sql << %{
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
END'
|
104
|
-
LANGUAGE plpgsql;
|
109
|
+
CREATE TRIGGER check_#{table_name}_delete_integrity_trigger
|
110
|
+
BEFORE DELETE ON #{table_name}
|
111
|
+
FOR EACH ROW EXECUTE PROCEDURE check_#{interface_name}_delete_integrity();
|
105
112
|
}
|
106
|
-
|
107
|
-
models.each do |model|
|
108
|
-
table_name = model.constantize.table_name
|
109
|
-
|
110
|
-
sql << %{
|
111
|
-
CREATE TRIGGER check_#{table_name}_delete_integrity_trigger
|
112
|
-
BEFORE DELETE ON #{table_name}
|
113
|
-
FOR EACH ROW EXECUTE PROCEDURE check_#{interface_name}_delete_integrity();
|
114
|
-
}
|
115
|
-
end
|
116
|
-
|
117
|
-
return sql
|
118
113
|
end
|
119
114
|
|
115
|
+
return sql
|
120
116
|
end
|
121
|
-
|
122
117
|
end
|
123
118
|
|
124
|
-
ActiveRecord::
|
119
|
+
class ActiveRecord::Migration
|
120
|
+
include Fides
|
121
|
+
end
|
@@ -15,14 +15,15 @@ describe Fides do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
@my_test_association = MyTestAssociaiton.new
|
18
|
+
@my_test_migration = MyTestMigration.new
|
18
19
|
end
|
19
20
|
|
20
21
|
it "responds to add_polymorphic_triggers" do
|
21
|
-
assert_respond_to
|
22
|
+
assert_respond_to @my_test_migration, :add_polymorphic_triggers
|
22
23
|
end
|
23
24
|
|
24
25
|
it "responds to remove_polymorphic_triggers" do
|
25
|
-
assert_respond_to
|
26
|
+
assert_respond_to @my_test_migration, :remove_polymorphic_triggers
|
26
27
|
end
|
27
28
|
|
28
29
|
it "includes the ability to use of the constantize method" do
|