active_git 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -18
- data/.travis.yml +7 -0
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +31 -29
- data/Rakefile +2 -2
- data/active_git.gemspec +25 -25
- data/lib/active_git.rb +47 -46
- data/lib/active_git/active_record_extension.rb +34 -49
- data/lib/active_git/commands.rb +146 -146
- data/lib/active_git/configuration.rb +28 -28
- data/lib/active_git/events/db_create.rb +8 -8
- data/lib/active_git/events/db_delete.rb +12 -12
- data/lib/active_git/events/db_delete_all.rb +15 -15
- data/lib/active_git/events/db_event.rb +28 -27
- data/lib/active_git/events/db_update.rb +14 -14
- data/lib/active_git/events/file_delete.rb +11 -11
- data/lib/active_git/events/file_event.rb +31 -31
- data/lib/active_git/events/file_save.rb +13 -13
- data/lib/active_git/events/folder_remove.rb +15 -15
- data/lib/active_git/inflector.rb +21 -0
- data/lib/active_git/synchronization_error.rb +32 -32
- data/lib/active_git/synchronizer.rb +53 -53
- data/lib/active_git/version.rb +3 -3
- data/spec/active_record_extension_spec.rb +60 -50
- data/spec/commands_spec.rb +245 -245
- data/spec/inflector_spec.rb +37 -0
- data/spec/migrations/20121004135939_create_countries.rb +9 -9
- data/spec/migrations/20121004135940_create_languages.rb +9 -9
- data/spec/migrations/20121030163114_create_brands.rb +9 -9
- data/spec/migrations/20130315192821_create_customers.rb +9 -0
- data/spec/spec_helper.rb +35 -23
- data/spec/support/helpers/file_helper.rb +47 -43
- data/spec/support/models/brand.rb +2 -4
- data/spec/support/models/country.rb +1 -2
- data/spec/support/models/customer.rb +6 -0
- data/spec/support/models/language.rb +2 -4
- data/spec/synchronization_spec.rb +100 -100
- metadata +56 -17
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveGit::Inflector do
|
4
|
+
|
5
|
+
let(:working_path) { '/home/git' }
|
6
|
+
|
7
|
+
it 'Git path' do
|
8
|
+
ActiveGit::Inflector.dirname(Country, working_path).should eq "#{working_path}/countries"
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'Git path for nested model' do
|
12
|
+
ActiveGit::Inflector.dirname(Crm::Customer, working_path).should eq "#{working_path}/crm/customers"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'Git file' do
|
16
|
+
country = Country.create! name: 'Argentina'
|
17
|
+
ActiveGit::Inflector.filename(country, working_path).should eq "#{working_path}/countries/#{country.id}.json"
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'Git file for nested model' do
|
21
|
+
customer = Crm::Customer.create! name: 'Monster Inc.'
|
22
|
+
ActiveGit::Inflector.filename(customer, working_path).should eq "#{working_path}/crm/customers/#{customer.id}.json"
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'Model from filename' do
|
26
|
+
ActiveGit::Inflector.model("#{working_path}/countries/1.json", working_path).should be Country
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'Model from filename' do
|
30
|
+
ActiveGit::Inflector.model("#{working_path}/crm/customers/1.json", working_path).should be Crm::Customer
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'Model id' do
|
34
|
+
ActiveGit::Inflector.model_id("#{working_path}/crm/customers/1.json").should eq '1'
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
class CreateCountries < ActiveRecord::Migration
|
2
|
-
def change
|
3
|
-
create_table :countries do |t|
|
4
|
-
t.string :name, null: false
|
5
|
-
|
6
|
-
t.timestamps
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
class CreateCountries < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :countries do |t|
|
4
|
+
t.string :name, null: false
|
5
|
+
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
class CreateLanguages < ActiveRecord::Migration
|
2
|
-
def change
|
3
|
-
create_table :languages do |t|
|
4
|
-
t.string :name, null: false
|
5
|
-
|
6
|
-
t.timestamps
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
class CreateLanguages < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :languages do |t|
|
4
|
+
t.string :name, null: false
|
5
|
+
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -1,9 +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
|
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
|
data/spec/spec_helper.rb
CHANGED
@@ -1,24 +1,36 @@
|
|
1
|
-
require 'active_git'
|
2
|
-
require 'logger'
|
3
|
-
|
4
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
ActiveRecord::
|
23
|
-
|
1
|
+
require 'active_git'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
5
|
+
|
6
|
+
ActiveRecord::Base.logger = Logger.new($stdout)
|
7
|
+
ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
|
8
|
+
|
9
|
+
module InflectorHelper
|
10
|
+
def git_dirname(model, working_path=nil)
|
11
|
+
ActiveGit::Inflector.dirname(model, working_path || ActiveGit.configuration.working_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def git_filename(instance, working_path=nil)
|
15
|
+
ActiveGit::Inflector.filename(instance, working_path || ActiveGit.configuration.working_path)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
|
21
|
+
config.around do |example|
|
22
|
+
ActiveRecord::Base.transaction do
|
23
|
+
example.run
|
24
|
+
raise ActiveRecord::Rollback
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
config.before :all do
|
29
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ":memory:"
|
30
|
+
ActiveRecord::Base.connection
|
31
|
+
ActiveRecord::Migrator.migrate ActiveRecord::Migrator.migrations_path
|
32
|
+
end
|
33
|
+
|
34
|
+
include InflectorHelper
|
35
|
+
|
24
36
|
end
|
@@ -1,44 +1,48 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
|
3
|
-
class FileHelper
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@temp_folders = []
|
7
|
-
end
|
8
|
-
|
9
|
-
def create_temp_folder
|
10
|
-
folder_name = "#{
|
11
|
-
@temp_folders << folder_name
|
12
|
-
|
13
|
-
folder_name
|
14
|
-
end
|
15
|
-
|
16
|
-
def remove_temp_folders
|
17
|
-
@temp_folders.each do |folder_name|
|
18
|
-
FileUtils.rm_rf folder_name
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def create_temp_file(folder, content)
|
23
|
-
file_name = "#{folder}/file #{timestamp}.txt"
|
24
|
-
write_file file_name, content
|
25
|
-
file_name
|
26
|
-
end
|
27
|
-
|
28
|
-
def write_file(file_name, content)
|
29
|
-
Dir.mkdir File.dirname(file_name) unless Dir.exist? File.dirname(file_name)
|
30
|
-
File.open(file_name, 'w') { |f| f.puts content }
|
31
|
-
end
|
32
|
-
|
33
|
-
def read_file(file_name)
|
34
|
-
File.open(file_name, 'r') { |f| f.readlines.join("\n").strip }
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
class FileHelper
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@temp_folders = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_temp_folder
|
10
|
+
folder_name = "#{temp_path}/#{timestamp}"
|
11
|
+
@temp_folders << folder_name
|
12
|
+
FileUtils.mkpath folder_name
|
13
|
+
folder_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove_temp_folders
|
17
|
+
@temp_folders.each do |folder_name|
|
18
|
+
FileUtils.rm_rf folder_name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_temp_file(folder, content)
|
23
|
+
file_name = "#{folder}/file #{timestamp}.txt"
|
24
|
+
write_file file_name, content
|
25
|
+
file_name
|
26
|
+
end
|
27
|
+
|
28
|
+
def write_file(file_name, content)
|
29
|
+
Dir.mkdir File.dirname(file_name) unless Dir.exist? File.dirname(file_name)
|
30
|
+
File.open(file_name, 'w') { |f| f.puts content }
|
31
|
+
end
|
32
|
+
|
33
|
+
def read_file(file_name)
|
34
|
+
File.open(file_name, 'r') { |f| f.readlines.join("\n").strip }
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def temp_path
|
40
|
+
Pathname.new("#{File.dirname(__FILE__)}/../../tmp").expand_path.to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
def timestamp
|
44
|
+
sleep(0.01)
|
45
|
+
(Time.now.to_f * 1000).to_i
|
46
|
+
end
|
47
|
+
|
44
48
|
end
|
@@ -1,101 +1,101 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'ActiveGit::Synchronizers' do
|
4
|
-
|
5
|
-
before :each do
|
6
|
-
@file_helper = FileHelper.new
|
7
|
-
end
|
8
|
-
|
9
|
-
after :each do
|
10
|
-
@file_helper.remove_temp_folders
|
11
|
-
end
|
12
|
-
|
13
|
-
context 'Target GIT' do
|
14
|
-
|
15
|
-
it 'Create' do
|
16
|
-
country = Country.create! name: 'Argentina'
|
17
|
-
|
18
|
-
working_path = @file_helper.create_temp_folder
|
19
|
-
|
20
|
-
File.exist?("#{working_path}/countries/#{country.id}.json").should be_false
|
21
|
-
|
22
|
-
ActiveGit::Synchronizer.synchronize ActiveGit::FileSave.new(country, working_path)
|
23
|
-
|
24
|
-
File.exist?("#{working_path}/countries/#{country.id}.json").should be_true
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'Update' do
|
28
|
-
country = Country.create! name: 'Argentina'
|
29
|
-
|
30
|
-
working_path = @file_helper.create_temp_folder
|
31
|
-
|
32
|
-
file_name = "#{working_path}/countries/#{country.id}.json"
|
33
|
-
@file_helper.write_file file_name, 'test'
|
34
|
-
|
35
|
-
@file_helper.read_file(file_name).should eq 'test'
|
36
|
-
|
37
|
-
ActiveGit::Synchronizer.synchronize ActiveGit::FileSave.new(country, working_path)
|
38
|
-
|
39
|
-
json = JSON.parse @file_helper.read_file(file_name)
|
40
|
-
json['id'].should eq country.id
|
41
|
-
json['name'].should eq country.name
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'Destroy' do
|
45
|
-
country = Country.create! name: 'Argentina'
|
46
|
-
|
47
|
-
working_path = @file_helper.create_temp_folder
|
48
|
-
|
49
|
-
file_name = "#{working_path}/countries/#{country.id}.json"
|
50
|
-
@file_helper.write_file file_name, 'test'
|
51
|
-
|
52
|
-
ActiveGit::Synchronizer.synchronize ActiveGit::FileDelete.new(country, working_path)
|
53
|
-
|
54
|
-
File.exist?("#{working_path}/countries/#{country.id}.json").should be_false
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'Target DB' do
|
60
|
-
|
61
|
-
it 'Create' do
|
62
|
-
working_path = @file_helper.create_temp_folder
|
63
|
-
|
64
|
-
file_name = "#{working_path}/countries/1.json"
|
65
|
-
@file_helper.write_file file_name, {id: 1, name: 'Argentina', created_at: '2012-04-20T11:24:11-03:00', updated_at: '2012-04-20T11:24:11-03:00'}.to_json
|
66
|
-
|
67
|
-
Country.count.should eq 0
|
68
|
-
|
69
|
-
ActiveGit::Synchronizer.synchronize ActiveGit::DbCreate.new(file_name)
|
70
|
-
|
71
|
-
Country.find(1).name.should eq 'Argentina'
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'Update' do
|
75
|
-
working_path = @file_helper.create_temp_folder
|
76
|
-
|
77
|
-
country = Country.create! name: 'Argentina'
|
78
|
-
|
79
|
-
file_name = "#{working_path}/countries/#{country.id}.json"
|
80
|
-
@file_helper.write_file file_name, country.attributes.merge('name' => 'Brasil').to_json
|
81
|
-
|
82
|
-
ActiveGit::Synchronizer.synchronize ActiveGit::DbUpdate.new(file_name)
|
83
|
-
|
84
|
-
country.reload.name.should eq 'Brasil'
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'Destroy' do
|
88
|
-
working_path = @file_helper.create_temp_folder
|
89
|
-
|
90
|
-
country = Country.create! name: 'Argentina'
|
91
|
-
|
92
|
-
file_name = "#{working_path}/countries/#{country.id}.json"
|
93
|
-
|
94
|
-
ActiveGit::Synchronizer.synchronize ActiveGit::DbDelete.new(file_name)
|
95
|
-
|
96
|
-
Country.find_by_id(country.id).should be_nil
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'ActiveGit::Synchronizers' do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@file_helper = FileHelper.new
|
7
|
+
end
|
8
|
+
|
9
|
+
after :each do
|
10
|
+
@file_helper.remove_temp_folders
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'Target GIT' do
|
14
|
+
|
15
|
+
it 'Create' do
|
16
|
+
country = Country.create! name: 'Argentina'
|
17
|
+
|
18
|
+
working_path = @file_helper.create_temp_folder
|
19
|
+
|
20
|
+
File.exist?("#{working_path}/countries/#{country.id}.json").should be_false
|
21
|
+
|
22
|
+
ActiveGit::Synchronizer.synchronize ActiveGit::FileSave.new(country, working_path)
|
23
|
+
|
24
|
+
File.exist?("#{working_path}/countries/#{country.id}.json").should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'Update' do
|
28
|
+
country = Country.create! name: 'Argentina'
|
29
|
+
|
30
|
+
working_path = @file_helper.create_temp_folder
|
31
|
+
|
32
|
+
file_name = "#{working_path}/countries/#{country.id}.json"
|
33
|
+
@file_helper.write_file file_name, 'test'
|
34
|
+
|
35
|
+
@file_helper.read_file(file_name).should eq 'test'
|
36
|
+
|
37
|
+
ActiveGit::Synchronizer.synchronize ActiveGit::FileSave.new(country, working_path)
|
38
|
+
|
39
|
+
json = JSON.parse @file_helper.read_file(file_name)
|
40
|
+
json['id'].should eq country.id
|
41
|
+
json['name'].should eq country.name
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'Destroy' do
|
45
|
+
country = Country.create! name: 'Argentina'
|
46
|
+
|
47
|
+
working_path = @file_helper.create_temp_folder
|
48
|
+
|
49
|
+
file_name = "#{working_path}/countries/#{country.id}.json"
|
50
|
+
@file_helper.write_file file_name, 'test'
|
51
|
+
|
52
|
+
ActiveGit::Synchronizer.synchronize ActiveGit::FileDelete.new(country, working_path)
|
53
|
+
|
54
|
+
File.exist?("#{working_path}/countries/#{country.id}.json").should be_false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'Target DB' do
|
60
|
+
|
61
|
+
it 'Create' do
|
62
|
+
working_path = @file_helper.create_temp_folder
|
63
|
+
|
64
|
+
file_name = "#{working_path}/countries/1.json"
|
65
|
+
@file_helper.write_file file_name, {id: 1, name: 'Argentina', created_at: '2012-04-20T11:24:11-03:00', updated_at: '2012-04-20T11:24:11-03:00'}.to_json
|
66
|
+
|
67
|
+
Country.count.should eq 0
|
68
|
+
|
69
|
+
ActiveGit::Synchronizer.synchronize ActiveGit::DbCreate.new(file_name, working_path)
|
70
|
+
|
71
|
+
Country.find(1).name.should eq 'Argentina'
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'Update' do
|
75
|
+
working_path = @file_helper.create_temp_folder
|
76
|
+
|
77
|
+
country = Country.create! name: 'Argentina'
|
78
|
+
|
79
|
+
file_name = "#{working_path}/countries/#{country.id}.json"
|
80
|
+
@file_helper.write_file file_name, country.attributes.merge('name' => 'Brasil').to_json
|
81
|
+
|
82
|
+
ActiveGit::Synchronizer.synchronize ActiveGit::DbUpdate.new(file_name, working_path)
|
83
|
+
|
84
|
+
country.reload.name.should eq 'Brasil'
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'Destroy' do
|
88
|
+
working_path = @file_helper.create_temp_folder
|
89
|
+
|
90
|
+
country = Country.create! name: 'Argentina'
|
91
|
+
|
92
|
+
file_name = "#{working_path}/countries/#{country.id}.json"
|
93
|
+
|
94
|
+
ActiveGit::Synchronizer.synchronize ActiveGit::DbDelete.new(file_name, working_path)
|
95
|
+
|
96
|
+
Country.find_by_id(country.id).should be_nil
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
101
|
end
|