consistency_fail 0.2.1 → 0.2.2

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/README.md CHANGED
@@ -25,7 +25,10 @@ For Rails 3:
25
25
 
26
26
  For Rails 2.3:
27
27
 
28
- gem install consistency_fail -v=0.1.0
28
+ gem install consistency_fail -v=0.1.1
29
+
30
+ Or, even better if you're using bundler, add it to your Gemfile, with the
31
+ appropriate version number for your Rails version.
29
32
 
30
33
  ## Limitations
31
34
 
@@ -61,6 +64,11 @@ Gemfile, or by some other mechanism.
61
64
  This mega-fail mode is nice to have if you have a large team and want to ensure
62
65
  that new models or validations/associations follow the rules.
63
66
 
67
+ If you're using the `Enforcer`, depending on your project, you may need to
68
+ delay the initializer until later, so that model files can be loaded only once
69
+ gem dependencies have been satisfied. One possible way is to move the code above
70
+ to the end of `environment.rb` or to the more specific `config/environment/*` files.
71
+
64
72
  ## License
65
73
 
66
74
  Released under the MIT License. See the LICENSE file for further details.
@@ -18,9 +18,14 @@ module ConsistencyFail
18
18
  private :desired_indexes
19
19
 
20
20
  def missing_indexes(model)
21
- existing_indexes = TableData.new.unique_indexes(model)
21
+ desired = desired_indexes(model)
22
22
 
23
- desired_indexes(model).reject do |index|
23
+ existing_indexes = desired.inject([]) do |acc, d|
24
+ # TODO: This assumes the models share a database. Need to make that configurable somehow.
25
+ acc += TableData.new.unique_indexes_by_table(model.connection, d.table_name)
26
+ end
27
+
28
+ desired.reject do |index|
24
29
  existing_indexes.include?(index)
25
30
  end
26
31
  end
@@ -6,9 +6,13 @@ module ConsistencyFail
6
6
  def unique_indexes(model)
7
7
  return [] if !model.table_exists?
8
8
 
9
- ar_indexes = model.connection.indexes(model.table_name).select(&:unique)
9
+ unique_indexes_by_table(model.connection, model.table_name)
10
+ end
11
+
12
+ def unique_indexes_by_table(connection, table_name)
13
+ ar_indexes = connection.indexes(table_name).select(&:unique)
10
14
  ar_indexes.map do |index|
11
- ConsistencyFail::Index.new(model.table_name, index.columns)
15
+ ConsistencyFail::Index.new(table_name, index.columns)
12
16
  end
13
17
  end
14
18
  end
@@ -24,13 +24,7 @@ module ConsistencyFail
24
24
  end
25
25
 
26
26
  def all
27
- models = []
28
- ObjectSpace.each_object do |o|
29
- models << o if o.class == Class &&
30
- o.ancestors.include?(ActiveRecord::Base) &&
31
- o != ActiveRecord::Base
32
- end
33
- models.sort_by(&:name)
27
+ ActiveRecord::Base.send(:descendants).sort_by(&:name)
34
28
  end
35
29
 
36
30
  end
@@ -1,3 +1,3 @@
1
1
  module ConsistencyFail
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -42,7 +42,7 @@ describe ConsistencyFail::Introspectors::HasOne do
42
42
 
43
43
  it "finds one" do
44
44
  @association.stub!(:table_name => :addresses, :primary_key_name => "user_id")
45
- @model.stub_chain(:connection, :indexes).with("users").and_return([])
45
+ @model.stub_chain(:connection, :indexes).with("addresses").and_return([])
46
46
 
47
47
  indexes = subject.missing_indexes(@model)
48
48
  indexes.should == [ConsistencyFail::Index.new("addresses", ["user_id"])]
@@ -51,7 +51,12 @@ describe ConsistencyFail::Introspectors::HasOne do
51
51
  it "finds none when they're already in place" do
52
52
  @association.stub!(:table_name => :addresses, :primary_key_name => "user_id")
53
53
  index = ConsistencyFail::Index.new("addresses", ["user_id"])
54
- ConsistencyFail::Introspectors::TableData.stub_chain(:new, :unique_indexes).
54
+
55
+ fake_connection = double("connection")
56
+ @model.stub_chain(:connection).and_return(fake_connection)
57
+
58
+ ConsistencyFail::Introspectors::TableData.stub_chain(:new, :unique_indexes_by_table).
59
+ with(fake_connection, "addresses").
55
60
  and_return([index])
56
61
 
57
62
  subject.missing_indexes(@model).should == []
data/spec/models_spec.rb CHANGED
@@ -32,24 +32,12 @@ describe ConsistencyFail::Models do
32
32
  end
33
33
 
34
34
  it "gets all models" do
35
- model_a = double("model_a",
36
- :ancestors => [ActiveRecord::Base],
37
- :class => Class,
38
- :name => "ModelA")
39
- model_b = double("model_b",
40
- :ancestors => [ActiveRecord::Base],
41
- :class => Class,
42
- :name => "ModelB")
43
- model_c = double("model_c",
44
- :ancestors => [ActiveRecord::Base],
45
- :class => ActiveRecord::Base,
46
- :name => "ModelC")
47
-
48
- ObjectSpace.stub(:each_object).
49
- and_yield(model_c).
50
- and_yield(model_b).
51
- and_yield(model_a)
52
-
53
- models([]).all.should == [model_a, model_b]
35
+ model_a = double(:name => "animal")
36
+ model_b = double(:name => "cat")
37
+ model_c = double(:name => "beach_ball")
38
+
39
+ ActiveRecord::Base.stub(:send).with(:descendants).and_return([model_a, model_b, model_c])
40
+
41
+ models([]).all.should == [model_a, model_c, model_b]
54
42
  end
55
43
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consistency_fail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Colin Jones
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-17 00:00:00 -05:00
19
- default_executable:
18
+ date: 2012-01-04 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: activerecord
@@ -92,7 +91,6 @@ files:
92
91
  - spec/models_spec.rb
93
92
  - spec/reporter_spec.rb
94
93
  - spec/spec_helper.rb
95
- has_rdoc: true
96
94
  homepage: http://github.com/trptcolin/consistency_fail
97
95
  licenses: []
98
96
 
@@ -122,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
120
  requirements: []
123
121
 
124
122
  rubyforge_project:
125
- rubygems_version: 1.5.2
123
+ rubygems_version: 1.8.10
126
124
  signing_key:
127
125
  specification_version: 3
128
126
  summary: A tool to detect missing unique indexes