active_git 0.0.2 → 0.0.3

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/lib/active_git.rb CHANGED
@@ -20,8 +20,6 @@ require 'active_git/active_record_extension'
20
20
  require 'active_git/configuration'
21
21
  require 'active_git/commands'
22
22
 
23
- GitWrapper.logger = ActiveRecord::Base.logger
24
-
25
23
  module ActiveGit
26
24
  extend Commands
27
25
 
@@ -7,22 +7,23 @@ module ActiveGit
7
7
  end
8
8
  end
9
9
 
10
- def dump_db
11
- events = [FolderRemove.new(ActiveGit.configuration.working_path)]
10
+ def dump_db(*models)
11
+ events = (Dir["#{ActiveGit.configuration.working_path}/*"] - ActiveGit.models.map(&:git_folder)).map do |folder|
12
+ FolderRemove.new(folder)
13
+ end
12
14
 
13
- ActiveGit.models.each do |model|
14
- model.all.each do |record|
15
- events << FileSave.new(record)
16
- end
15
+ (models.any? ? models : ActiveGit.models).each do |model|
16
+ events << FolderRemove.new(model.git_folder)
17
+ events = events + model.all.map { |r| FileSave.new r }
17
18
  end
18
19
 
19
20
  Synchronizer.synchronize events
20
21
  end
21
22
 
22
- def load_files
23
+ def load_files(*models)
23
24
  events = []
24
25
 
25
- ActiveGit.models.each do |model|
26
+ (models.any? ? models : ActiveGit.models).each do |model|
26
27
  events << DbDeleteAll.new(model)
27
28
  Dir.glob("#{model.git_folder}/*.json").each do |file_name|
28
29
  events << DbCreate.new(file_name)
@@ -56,7 +57,7 @@ module ActiveGit
56
57
  begin
57
58
  synchronize_diffs diffs
58
59
  rescue => e
59
- ::ActiveRecord::Base.logger.error "[ActiveGit] #{e}"
60
+ ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
60
61
  reset last_log.commit_hash
61
62
  return false
62
63
  end
@@ -96,7 +97,7 @@ module ActiveGit
96
97
  begin
97
98
  synchronize_diffs diffs
98
99
  rescue SynchronizationError => e
99
- ::ActiveRecord::Base.logger.error "[ActiveGit] #{e}"
100
+ ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
100
101
  repository.checkout current
101
102
  return false
102
103
  end
@@ -112,7 +113,7 @@ module ActiveGit
112
113
  begin
113
114
  synchronize_diffs diffs
114
115
  rescue SynchronizationError => e
115
- ::ActiveRecord::Base.logger.error "[ActiveGit] #{e}"
116
+ ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
116
117
  #TODO: Rollback reset
117
118
  return false
118
119
  end
@@ -15,7 +15,15 @@ module ActiveGit
15
15
 
16
16
  def bare_path=(path)
17
17
  @bare_path = path
18
- end
18
+ end
19
+
20
+ def logger
21
+ GitWrapper.logger
22
+ end
23
+
24
+ def logger=(logger)
25
+ GitWrapper.logger = logger
26
+ end
19
27
 
20
28
  end
21
29
  end
@@ -3,7 +3,7 @@ module ActiveGit
3
3
 
4
4
  def synchronize(synchronizer)
5
5
  synchronizer.define_job do
6
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Deleting #{model.model_name} #{model_id}"
6
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting #{model.model_name} #{model_id}"
7
7
  record = model.find_by_id(model_id)
8
8
  record.delete if record
9
9
  end
@@ -7,7 +7,7 @@ module ActiveGit
7
7
 
8
8
  def synchronize(synchronizer)
9
9
  synchronizer.define_job do
10
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Deleting all #{@model.model_name} models"
10
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting all #{@model.model_name} models"
11
11
  @model.delete_all
12
12
  end
13
13
  end
@@ -5,7 +5,7 @@ module ActiveGit
5
5
  synchronizer.bulk_insert data
6
6
 
7
7
  synchronizer.define_job do
8
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Deleting #{data.class.model_name} #{data.id}"
8
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting #{data.class.model_name} #{data.id}"
9
9
  record = data.class.find_by_id(data.id)
10
10
  record.delete if record
11
11
  end
@@ -3,7 +3,7 @@ module ActiveGit
3
3
 
4
4
  def synchronize(synchronizer)
5
5
  synchronizer.define_job do
6
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Deleting file #{file_name}"
6
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting file #{file_name}"
7
7
  File.delete(file_name) if File.exist?(file_name)
8
8
  end
9
9
  end
@@ -3,7 +3,7 @@ module ActiveGit
3
3
 
4
4
  def synchronize(synchronizer)
5
5
  synchronizer.define_job do
6
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Writing file #{file_name}"
6
+ ActiveGit.configuration.logger.debug "[ActiveGit] Writing file #{file_name}"
7
7
  FileUtils.mkpath(File.dirname(file_name)) unless Dir.exist?(File.dirname(file_name))
8
8
  File.open(file_name, 'w') { |f| f.puts json }
9
9
  end
@@ -7,7 +7,7 @@ module ActiveGit
7
7
 
8
8
  def synchronize(synchronizer)
9
9
  synchronizer.define_job do
10
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Removing working folder #{@path}"
10
+ ActiveGit.configuration.logger.debug "[ActiveGit] Removing folder #{@path}"
11
11
  FileUtils.rm_rf @path
12
12
  end
13
13
  end
@@ -15,7 +15,7 @@ module ActiveGit
15
15
  unless bulk_inserts.empty?
16
16
  define_job do
17
17
  bulk_inserts.each do |model, records|
18
- ::ActiveRecord::Base.logger.debug "[ActiveGit] Inserting #{model.model_name} models"
18
+ ActiveGit.configuration.logger.debug "[ActiveGit] Inserting #{model.model_name} models"
19
19
  import_result = model.import records, timestamps: false, validate: false
20
20
  raise SynchronizationError.new(import_result.failed_instances) unless import_result.failed_instances.empty?
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveGit
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -37,6 +37,25 @@ describe ActiveGit::Commands do
37
37
  end
38
38
  end
39
39
 
40
+ it 'Dump single table to files' do
41
+ language = Language.create! name: 'Spanish'
42
+ country = Country.create! name: 'Argentina'
43
+
44
+ Dir["#{ActiveGit.configuration.working_path}/*"].each { |f| FileUtils.rm_rf f }
45
+
46
+ Dir.exists?("#{ActiveGit.configuration.working_path}/languages").should be_false
47
+ File.exists?("#{ActiveGit.configuration.working_path}/languages/#{language.id}.json").should be_false
48
+ Dir.exists?("#{ActiveGit.configuration.working_path}/countries").should be_false
49
+ File.exists?("#{ActiveGit.configuration.working_path}/countries/#{country.id}.json").should be_false
50
+
51
+ ActiveGit.dump_db Language
52
+
53
+ Dir.exists?("#{ActiveGit.configuration.working_path}/languages").should be_true
54
+ File.exists?("#{ActiveGit.configuration.working_path}/languages/#{language.id}.json").should be_true
55
+ Dir.exists?("#{ActiveGit.configuration.working_path}/countries").should be_false
56
+ File.exists?("#{ActiveGit.configuration.working_path}/countries/#{country.id}.json").should be_false
57
+ end
58
+
40
59
  it 'Load all files to db' do
41
60
  languages = [
42
61
  Language.create!(name: 'Spanish'),
@@ -58,6 +77,22 @@ describe ActiveGit::Commands do
58
77
  end
59
78
  end
60
79
 
80
+ it 'Load single model files to db' do
81
+ language = Language.create! name: 'Spanish'
82
+ brand = Brand.create! name: 'Coca Cola'
83
+
84
+ File.exists?(language.git_file).should be_true
85
+ File.exists?(brand.git_file).should be_true
86
+
87
+ language.delete
88
+ brand.delete
89
+
90
+ ActiveGit.load_files Language
91
+
92
+ Language.find_by_id(language.id).should_not be_nil
93
+ Brand.find_by_id(brand.id).should be_nil
94
+ end
95
+
61
96
  end
62
97
 
63
98
  context 'Git synchronization' do
@@ -174,7 +209,7 @@ describe ActiveGit::Commands do
174
209
  bare.init_bare
175
210
 
176
211
  ActiveGit.add_remote 'bare', bare.location
177
-
212
+
178
213
  spanish = Language.create! name: 'Spanish'
179
214
 
180
215
  ActiveGit.commit_all 'Commit v1'
@@ -1,7 +1,7 @@
1
1
  class CreateCountries < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :countries do |t|
4
- t.string :name, :null => false
4
+ t.string :name, null: false
5
5
 
6
6
  t.timestamps
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class CreateLanguages < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :languages do |t|
4
- t.string :name, :null => false
4
+ t.string :name, null: false
5
5
 
6
6
  t.timestamps
7
7
  end
@@ -0,0 +1,9 @@
1
+ class CreateBrands < ActiveRecord::Migration
2
+ def change
3
+ create_table :brands do |t|
4
+ t.string :name, null: false
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class Brand < ActiveRecord::Base
2
+ git_versioned
3
+
4
+ attr_accessible :name
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_git
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-29 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &28809840 !ruby/object:Gem::Requirement
16
+ requirement: &25039440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *28809840
24
+ version_requirements: *25039440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: git_wrapper
27
- requirement: &28809540 !ruby/object:Gem::Requirement
27
+ requirement: &25038948 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *28809540
35
+ version_requirements: *25038948
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activerecord-import
38
- requirement: &28809312 !ruby/object:Gem::Requirement
38
+ requirement: &25038588 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *28809312
46
+ version_requirements: *25038588
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: easy_diff
49
- requirement: &28809036 !ruby/object:Gem::Requirement
49
+ requirement: &25038180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *28809036
57
+ version_requirements: *25038180
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &28808784 !ruby/object:Gem::Requirement
60
+ requirement: &25037856 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *28808784
68
+ version_requirements: *25037856
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &28808532 !ruby/object:Gem::Requirement
71
+ requirement: &25037460 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *28808532
79
+ version_requirements: *25037460
80
80
  description: DB and GIT synchronization via ActiveRecord and GitWrapper
81
81
  email:
82
82
  - gabynaiman@gmail.com
@@ -110,8 +110,10 @@ files:
110
110
  - spec/commands_spec.rb
111
111
  - spec/migrations/20121004135939_create_countries.rb
112
112
  - spec/migrations/20121004135940_create_languages.rb
113
+ - spec/migrations/20121030163114_create_brands.rb
113
114
  - spec/spec_helper.rb
114
115
  - spec/support/helpers/file_helper.rb
116
+ - spec/support/models/brand.rb
115
117
  - spec/support/models/country.rb
116
118
  - spec/support/models/language.rb
117
119
  - spec/synchronization_spec.rb
@@ -144,8 +146,10 @@ test_files:
144
146
  - spec/commands_spec.rb
145
147
  - spec/migrations/20121004135939_create_countries.rb
146
148
  - spec/migrations/20121004135940_create_languages.rb
149
+ - spec/migrations/20121030163114_create_brands.rb
147
150
  - spec/spec_helper.rb
148
151
  - spec/support/helpers/file_helper.rb
152
+ - spec/support/models/brand.rb
149
153
  - spec/support/models/country.rb
150
154
  - spec/support/models/language.rb
151
155
  - spec/synchronization_spec.rb