gravis-acts_as_archive 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ module ActsAsArchive
14
14
  end
15
15
 
16
16
  def self.archive_indexes
17
- #{Array(options[:indexes]).map{|index| index.is_a?(Array) ? index.map(&:to_s) : index.to_s}.inspect}
17
+ #{Array(options[:indexes]).map{|index| index.is_a?(Array) ? index.map(&:to_sym) : index.to_sym}.inspect}
18
18
  end
19
19
 
20
20
  class Archive < ActiveRecord::Base
@@ -12,9 +12,9 @@ module ActsAsArchive
12
12
  current_index = 0
13
13
  indexes.each do |index|
14
14
  if index['Seq_in_index'] != '1'
15
- final_indexes[current_index-1] = Array(final_indexes[current_index-1]).flatten.concat(Array(index['Column_name']))
15
+ final_indexes[current_index-1] = Array(final_indexes[current_index-1]).flatten.concat(Array(index['Column_name']).to_sym)
16
16
  else
17
- final_indexes[current_index] = index['Column_name']
17
+ final_indexes[current_index] = index['Column_name'].to_sym
18
18
  current_index += 1
19
19
  end
20
20
  end
@@ -34,7 +34,7 @@ order by
34
34
  SQL
35
35
 
36
36
  indexes = connection.select_all(index_query).collect do |r|
37
- r["column_names"].split(", ").size > 1 ? r["column_names"].split(", ") : r["column_names"]
37
+ r["column_names"].split(", ").size > 1 ? r["column_names"].split(", ").map(&:to_sym) : r["column_names"].to_sym
38
38
  end
39
39
  end
40
40
  end
@@ -43,18 +43,10 @@ module ActsAsArchive
43
43
  indexes = archive_table_indexed_columns
44
44
 
45
45
  (archive_indexes - indexes).each do |index|
46
- begin
47
- connection.add_index("archived_#{table_name}", index)
48
- rescue ActiveRecord::StatementInvalid => e
49
- Rails.logger.warn "Can't add index : #{index.inspect} on #{table_name} (#{e.to_s})"
50
- end
46
+ connection.add_index("archived_#{table_name}", index, :name => index_name_for(table_name, index)) if table_has_columns(index)
51
47
  end
52
48
  (indexes - archive_indexes).each do |index|
53
- begin
54
- connection.remove_index("archived_#{table_name}", index)
55
- rescue ActiveRecord::StatementInvalid => e
56
- Rails.logger.warn "Can't remove index : #{index.inspect} on #{table_name} (#{e.to_s})"
57
- end
49
+ connection.remove_index("archived_#{table_name}", index) if table_has_columns(index)
58
50
  end
59
51
  end
60
52
  end
@@ -74,21 +66,13 @@ module ActsAsArchive
74
66
 
75
67
  private
76
68
 
77
- def archive_table_indexed_columns
78
- case connection.class.to_s
79
- when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
80
- index_query = "SHOW INDEX FROM archived_#{table_name}"
81
- indexes = connection.select_all(index_query).collect do |r|
82
- r["Column_name"]
83
- end
84
- when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
85
- index_query = "SELECT indexname FROM pg_indexes WHERE tablename = '#{table_name}'"
86
- indexes = connection.select_all(index_query).collect do |r|
87
- r["indexname"].split("_on_").last.split("_and_")
88
- end
89
- else
90
- raise "Unsupported Database"
91
- end
69
+ def table_has_columns(columns)
70
+ self::Archive.reset_column_information
71
+ !Array(columns).select {|current_column| self::Archive.column_names.include?(current_column.to_s)}.empty?
72
+ end
73
+
74
+ def index_name_for(table_name, index)
75
+ "index_by_#{Array(index).join("_and_")}".to_sym if "index_archived_#{table_name}_on_#{Array(index).join("_and_")}".length > 63
92
76
  end
93
77
  end
94
78
 
data/require.rb CHANGED
@@ -17,7 +17,7 @@ Require do
17
17
  name 'gravis-acts_as_archive'
18
18
  homepage "http://github.com/gravis/#{name}"
19
19
  summary "Don't delete your records, move them to a different table"
20
- version '0.2.6'
20
+ version '0.2.9'
21
21
  end
22
22
 
23
23
  lib do
@@ -36,13 +36,13 @@ describe ActsAsArchive::Base::Table do
36
36
  end
37
37
 
38
38
  it "should create archive indexes" do
39
- indexes.to_set.should == [ "id", ["subject_id", "subject_type"], "deleted_at", "column_that_does_not_exist_yet" ].to_set
39
+ indexes.to_set.should == [ :id, [:subject_id, :subject_type], :deleted_at, :column_that_does_not_exist_yet ].to_set
40
40
  end
41
41
 
42
42
  it "should destroy archive indexes" do
43
43
  Article.class_eval { acts_as_archive }
44
44
  Article.create_archive_indexes
45
- indexes.should == ["column_that_does_not_exist_yet"]
45
+ indexes.should == [:column_that_does_not_exist_yet]
46
46
  end
47
47
  end
48
48
 
@@ -14,7 +14,7 @@ describe ActsAsArchive::Base do
14
14
 
15
15
  it "should add self.archive_indexes to the model" do
16
16
  Article.respond_to?(:archive_indexes).should == true
17
- Article.archive_indexes.should == [ 'id', ['subject_id','subject_type'], 'deleted_at' ]
17
+ Article.archive_indexes.should == [ :id, [ :subject_id, :subject_type ], :deleted_at ]
18
18
  end
19
19
 
20
20
  it "should add Archive class to the model" do
data/spec/spec_helper.rb CHANGED
@@ -80,7 +80,7 @@ def establish_test_db
80
80
  end
81
81
 
82
82
  def indexes
83
- Article.send(:archive_table_indexed_columns).concat(['column_that_does_not_exist_yet'])
83
+ Article.send(:archive_table_indexed_columns).concat([:column_that_does_not_exist_yet])
84
84
  end
85
85
 
86
86
  def migrate_up(directory='migrate')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gravis-acts_as_archive
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 6
10
- version: 0.2.6
9
+ - 7
10
+ version: 0.2.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Winton Welsh
@@ -16,7 +16,7 @@ bindir: bin
16
16
  cert_chain: []
17
17
 
18
18
  date: 2010-05-31 00:00:00 +02:00
19
- default_executable:
19
+ default_executable: acts_as_archive
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: require