Skip to content

Commit 8922264

Browse files
authored
Merge branch 'develop' into added_gems
2 parents 4051ac9 + cce4bb1 commit 8922264

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ matrix:
1616
before_install:
1717
- rvm @global do gem install bundler
1818
script:
19-
- bundle exec rspec
19+
- bundle exec rubocop && bundle exec rspec

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source 'https://rubygems.org'
22

3-
gem 'rake', '>= 10.4.2', require: false
3+
gem 'rake', require: false
44
gem 'activerecord', '>= 4.2.5', require: false
55
gem 'ruby_dep', '1.3.1'
66

@@ -21,6 +21,7 @@ group :development, :test do
2121
gem 'rubocop', '~> 0.46.0', require: false unless RUBY_VERSION =~ /^1.8/
2222
gem 'coveralls'
2323
gem 'codeclimate-test-reporter'
24+
gem 'ruby_dep', '1.3.1'
2425

2526
platforms :mri, :mingw do
2627
gem 'pry', require: false

annotate.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ Gem::Specification.new do |s|
2626
s.specification_version = 4
2727

2828
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
29-
s.add_runtime_dependency(%q<rake>, [">= 10.4", "< 12.0"])
29+
s.add_runtime_dependency(%q<rake>, [">= 10.4", "< 12.1"])
3030
s.add_runtime_dependency(%q<activerecord>, [">= 3.2", "< 6.0"])
3131
else
32-
s.add_dependency(%q<rake>, [">= 10.4", "< 12.0"])
32+
s.add_dependency(%q<rake>, [">= 10.4", "< 12.1"])
3333
s.add_dependency(%q<activerecord>, [">= 3.2", "< 6.0"])
3434
end
3535
else

lib/annotate/annotate_models.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ def schema_default(klass, column)
181181
quote(klass.column_defaults[column.name])
182182
end
183183

184+
def retrieve_indexes_from_table(klass)
185+
table_name = klass.table_name
186+
return [] unless table_name
187+
188+
indexes = klass.connection.indexes(table_name)
189+
return indexes if indexes.any? || !klass.table_name_prefix
190+
191+
# Try to search the table without prefix
192+
table_name.to_s.slice!(klass.table_name_prefix)
193+
klass.connection.indexes(table_name)
194+
end
195+
184196
# Use the column information in an ActiveRecord class
185197
# to create a comment block containing a line for
186198
# each column. The line contains the column name,
@@ -244,7 +256,7 @@ def get_schema_info(klass, header, options = {})
244256
# Check if the column has indices and print "indexed" if true
245257
# If the index includes another column, print it too.
246258
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
247-
indices = klass.connection.indexes(klass.table_name)
259+
indices = retrieve_indexes_from_table(klass)
248260
if indices = indices.select { |ind| ind.columns.include? col.name }
249261
indices.sort_by(&:name).each do |ind|
250262
ind = ind.columns.reject! { |i| i == col.name }
@@ -305,7 +317,7 @@ def get_index_info(klass, options={})
305317
index_info = "#\n# Indexes\n#\n"
306318
end
307319

308-
indexes = klass.connection.indexes(klass.table_name)
320+
indexes = retrieve_indexes_from_table(klass)
309321
return '' if indexes.empty?
310322

311323
max_size = indexes.collect{|index| index.name.size}.max + 1

spec/annotate/annotate_models_spec.rb

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
require 'active_support/core_ext/string'
66

77
describe AnnotateModels do
8+
def mock_index(name, columns = [], unique = false)
9+
double("IndexKeyDefinition",
10+
:name => name,
11+
:columns => columns,
12+
:unique => unique
13+
)
14+
end
15+
816
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
917
double("ForeignKeyDefinition",
1018
:name => name,
@@ -20,21 +28,22 @@ def mock_connection(indexes = [], foreign_keys = [])
2028
double("Conn",
2129
:indexes => indexes,
2230
:foreign_keys => foreign_keys,
23-
:supports_foreign_keys? => true,
31+
:supports_foreign_keys? => true
2432
)
2533
end
2634

27-
def mock_class(table_name, primary_key, columns, foreign_keys = [])
35+
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
2836
options = {
29-
:connection => mock_connection([], foreign_keys),
37+
:connection => mock_connection(indexes, foreign_keys),
3038
:table_exists? => true,
3139
:table_name => table_name,
3240
:primary_key => primary_key,
3341
:column_names => columns.map { |col| col.name.to_s },
3442
:columns => columns,
3543
:column_defaults => Hash[columns.map { |col|
3644
[col.name, col.default]
37-
}]
45+
}],
46+
:table_name_prefix => '',
3847
}
3948

4049
double("An ActiveRecord class", options)
@@ -181,7 +190,7 @@ def mock_column(name, type, options={})
181190
klass = mock_class(:users, :id, [
182191
mock_column(:id, :integer),
183192
mock_column(:foreign_thing_id, :integer),
184-
],
193+
], [],
185194
[
186195
mock_foreign_key(
187196
'fk_rails_cf2568e89e',
@@ -220,7 +229,7 @@ def mock_column(name, type, options={})
220229
klass = mock_class(:users, :id, [
221230
mock_column(:id, :integer),
222231
mock_column(:foreign_thing_id, :integer),
223-
],
232+
], [],
224233
[
225234
mock_foreign_key(
226235
'fk_rails_02e851e3b7',
@@ -246,6 +255,44 @@ def mock_column(name, type, options={})
246255
EOS
247256
end
248257

258+
it "should get indexes keys" do
259+
klass = mock_class(:users, :id, [
260+
mock_column(:id, :integer),
261+
mock_column(:foreign_thing_id, :integer),
262+
], [mock_index('index_rails_02e851e3b7', ['id']),
263+
mock_index('index_rails_02e851e3b8', ['foreign_thing_id'])])
264+
expect(AnnotateModels.get_schema_info(klass, "Schema Info", :show_indexes => true)).to eql(<<-EOS)
265+
# Schema Info
266+
#
267+
# Table name: users
268+
#
269+
# id :integer not null, primary key
270+
# foreign_thing_id :integer not null
271+
#
272+
# Indexes
273+
#
274+
# index_rails_02e851e3b7 (id)
275+
# index_rails_02e851e3b8 (foreign_thing_id)
276+
#
277+
EOS
278+
end
279+
280+
it "should not crash getting indexes keys" do
281+
klass = mock_class(:users, :id, [
282+
mock_column(:id, :integer),
283+
mock_column(:foreign_thing_id, :integer),
284+
], [])
285+
expect(AnnotateModels.get_schema_info(klass, "Schema Info", :show_indexes => true)).to eql(<<-EOS)
286+
# Schema Info
287+
#
288+
# Table name: users
289+
#
290+
# id :integer not null, primary key
291+
# foreign_thing_id :integer not null
292+
#
293+
EOS
294+
end
295+
249296
it "should get schema info as RDoc" do
250297
klass = mock_class(:users, :id, [
251298
mock_column(:id, :integer),

spec/spec_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
require 'byebug'
3131
require 'rubocop/rake_task'
32-
RuboCop::RakeTask.new
33-
Rake::Task['rubocop'].invoke
3432

3533
module Annotate
3634
module Integration

0 commit comments

Comments
 (0)