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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11810cdba23bbe7edf554320cab120f33c466d94
4
- data.tar.gz: 0af72bcf6dcaf6a0d9264884f952d29735d9a0de
3
+ metadata.gz: 7a7f704749dcd05ef1186b0a46f0cad900ab00ed
4
+ data.tar.gz: 9f933f47a0b8fd70fe54fb1cb816a376cd42730d
5
5
  SHA512:
6
- metadata.gz: 640ee69bf5632a32caffd9195d41e8b86374c9f8d77fd7b1c024bc22104cca22a96b359ea1ec91886531b0bfd28dd04097b8a80af82c42d6ef7bda2f55326876
7
- data.tar.gz: 9a250b1639d0a41c949ce6e9dcb9d8767b0d7e956e76aeec3f9ba46ea67cfc615a51c599edcec508d7698b73c4438ff2cd17a9bf958647ace9ad40831a785ce7
6
+ metadata.gz: dbe549233fb1bd8f5bdcd42dea5c2ab84e4a5e958b7322db74a35ae5c2ac439dd4c691ed4fa5c879a73efde0b671adf458c847ec8bda676b09411789f7dd4a5f
7
+ data.tar.gz: 692e61dafb6e55e1367bcb66ca473c1a80957bcb22f0770fe76539c60561fa74c1fcae0761ed3a44a4d1648d77c2f8cac297a9486a64a569e90c49717ad5697f
data/lib/fides/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fides
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.9"
3
3
  end
data/lib/fides.rb CHANGED
@@ -7,118 +7,115 @@ module Fides
7
7
 
8
8
  extend ActiveSupport::Concern
9
9
 
10
- module ClassMethods
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
- def add_polymorphic_triggers(opts)
13
- raise ArgumentError, "missing :associated_models from options hash" if !opts.has_key?(:associated_models)
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
- sql = get_create_function_sql(interface_name, associated_models, polymorphic_model)
20
- sql << get_delete_function_sql(interface_name, associated_models, polymorphic_model)
20
+ execute sql
21
+ end
21
22
 
22
- execute sql
23
- end
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
- def remove_polymorphic_triggers(opts)
26
- polymorphic_model = opts[:polymorphic_model]
27
- interface_name = opts.has_key?(:interface_name) ? opts[:interface_name] : interface_name(polymorphic_model)
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
- execute %{
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
- private
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
- # TODO: Is it safe to just grab the first polymorphic association?
38
- def interface_name(model_name)
39
- model_name.constantize.reflect_on_all_associations.select { |r| r if r.options[:polymorphic] }.first.name
40
- end
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
- def get_create_function_sql(interface_name, models, polymorphic_model)
43
- sql = "DROP FUNCTION IF EXISTS check_#{interface_name}_create_integrity() CASCADE;"
44
-
51
+ models[1..-1].each do |model|
45
52
  sql << %{
46
- CREATE FUNCTION check_#{interface_name}_create_integrity() RETURNS TRIGGER AS '
47
- BEGIN
48
- IF NEW.#{interface_name}_type = ''#{models[0]}'' AND EXISTS (
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
- models[1..-1].each do |model|
54
- sql << %{
55
- ELSEIF NEW.#{interface_name}_type = ''#{model}'' AND EXISTS (
56
- SELECT id FROM #{model.constantize.table_name} WHERE id = NEW.#{interface_name}_id) THEN
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
- return sql
75
- end
72
+ return sql
73
+ end
76
74
 
77
- def get_delete_function_sql(interface_name, models, polymorphic_model)
78
- polymorphic_model_table_name = polymorphic_model.constantize.table_name
79
-
80
- sql = ""
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
- CREATE FUNCTION check_#{interface_name}_delete_integrity() RETURNS TRIGGER AS '
83
- BEGIN
84
- IF TG_TABLE_NAME = ''#{models[0].constantize.table_name}'' AND EXISTS (
85
- SELECT id FROM #{polymorphic_model_table_name}
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
- models[1..-1].each do |model|
91
- sql << %{
92
- ELSEIF TG_TABLE_NAME = ''#{model.constantize.table_name}'' AND EXISTS (
93
- SELECT id FROM #{polymorphic_model_table_name}
94
- WHERE #{interface_name}_type = ''#{model}'' AND #{interface_name}_id = OLD.id) THEN
95
- RAISE EXCEPTION ''There are records in #{polymorphic_model_table_name} that refer to %. You must delete those records first.'', OLD;
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
- ELSE
101
- RETURN NULL;
102
- END IF;
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::Base.send(:include, Fides)
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 ActiveRecord::Migration, :add_polymorphic_triggers
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 ActiveRecord::Migration, :remove_polymorphic_triggers
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fides
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Kraft