model-builder 2.2.0 → 2.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2583711abc52652aa9cc69ef15f9562a291c7a2
4
- data.tar.gz: 65e5037c934f0b60e7e739fa7d84b345e36d3c49
3
+ metadata.gz: ace51db947f2b525e1d80a6fa90b71e6a3115adc
4
+ data.tar.gz: c1cb1febe8be2491f961bba40059f86c72bc8f82
5
5
  SHA512:
6
- metadata.gz: 7aa46d725a9c5eafd7b17ae90915faa58a1cac85079201953a404fb96d01497f3a7a4ebf2e18708769ab4c22655d832cf495172daccfd4cad296ab2dd9d964c0
7
- data.tar.gz: 29d785d220246d6bb8eaa67552f9091d4214554112363fd6174df42d1527033055d5858fd1c5c4a61ca7ec4e42bf0454edce4de7ea706b157fa2710679d3b427
6
+ metadata.gz: 9280f72f29461438d2ee0411f0a3411b0ade28b9057a56ff620995366366f02f4525341558af750ec5a3f935cba0a792a233724c5c07510ce2b9813497e47d99
7
+ data.tar.gz: 5b0d1755b6742ac1f38b6b12576053d88a8293ab09bc0b15f092b35af3250c354a102d0b9e4d62c4d4684a77b9ee42152d078e97a4bb3e506ed0d747a4f6d6dd
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Build active record models on the fly.
4
4
 
5
- Handy when following TDD to develop a reusable component, that isn't strictly related to a project business intelligence (therefore should not reference it).
5
+ Handy when following TDD to develop a reusable component, that isn't strictly related to a project business layer (therefore should not reference it).
6
6
 
7
7
  == Scenario
8
8
 
@@ -57,14 +57,37 @@ Now your 'MappableName' model is able to receive NameUtils tests:
57
57
 
58
58
  MappableName.map_all_names
59
59
 
60
+ And, in order to clean database:
61
+
62
+ # Add this to your spec_helper.rb or rails_helper.rb
63
+
64
+ RSpec.configure do |config|
65
+ config.after(:suite) do
66
+ ModelBuilder.clean
67
+ end
68
+ end
69
+
60
70
  See full test examples here[https://github.com/r4z3c/model-builder/blob/master/spec/examples/name_utils_spec.rb] and here[https://github.com/r4z3c/model-builder/blob/master/spec/lib/model_builder_spec.rb].
61
71
 
62
72
  == Changelog
63
73
 
74
+ === 2.2.1
75
+
76
+ - Bug fixes and improvements for creating/dropping tables
77
+ - Bug fixes and improvements for cleaning strategy: now you have to call 'clean' method at 'after :suite' instead of 'before :all'
78
+
79
+ # Add this to your spec_helper.rb or rails_helper.rb
80
+
81
+ RSpec.configure do |config|
82
+ config.after(:suite) do
83
+ ModelBuilder.clean
84
+ end
85
+ end
86
+
64
87
  === 2.2.0
65
88
 
66
89
  - Bug fixes and improvements for creating/dropping tables
67
- - Bug fixes and improvements for cleaning strategy: now you should call 'clean' methods in 'before :all' instead of 'before :each'
90
+ - Bug fixes and improvements for cleaning strategy: now you have to 'clean' method at 'before :all' instead of 'before :each'
68
91
 
69
92
  === 2.1.0
70
93
 
@@ -1,3 +1,3 @@
1
1
  module ModelBuilder
2
- VERSION = '2.2.0'
2
+ VERSION = '2.2.1'
3
3
  end
data/lib/model_builder.rb CHANGED
@@ -2,6 +2,8 @@ Dir["#{File.dirname(__FILE__)}/**/*.rb"].each {|file| require file }
2
2
 
3
3
  module ModelBuilder
4
4
 
5
+ TABLE_PREFIX = "model_builder/"
6
+
5
7
  @@dynamic_models ||= []
6
8
 
7
9
  def self.build(class_full_name, opts={})
@@ -11,6 +13,7 @@ module ModelBuilder
11
13
 
12
14
  unless table_already_exists class_full_name
13
15
  create_table class_full_name, opts
16
+ define_table_name klass, class_full_name
14
17
  define_validations klass, opts[:validates]
15
18
  end
16
19
 
@@ -25,11 +28,24 @@ module ModelBuilder
25
28
  end
26
29
 
27
30
  def self.table_already_exists(class_full_name)
28
- ActiveRecord::Base.connection.tables.include? table_name_for(class_full_name)
31
+ relevant_database_tables.include? table_name_for(class_full_name)
32
+ end
33
+
34
+ def self.relevant_database_tables
35
+ database_tables.select {|table| table.starts_with? TABLE_PREFIX }
36
+ end
37
+
38
+ def self.database_tables
39
+ database_connection.tables
40
+ end
41
+
42
+ def self.database_connection
43
+ ActiveRecord::Base.connection
29
44
  end
30
45
 
31
46
  def self.table_name_for(class_full_name)
32
- class_full_name.tableize.gsub(/\//,'_')
47
+ table_name = class_full_name.tableize.gsub(/\//,'_')
48
+ "#{TABLE_PREFIX}#{table_name}"
33
49
  end
34
50
 
35
51
  def self.create_table(class_full_name, opts)
@@ -57,6 +73,10 @@ module ModelBuilder
57
73
  migration.send(type, key, opts)
58
74
  end
59
75
 
76
+ def self.define_table_name(klass, class_full_name)
77
+ klass.table_name = table_name_for(class_full_name)
78
+ end
79
+
60
80
  def self.define_validations(klass, validations)
61
81
  return if validations.nil? or !validations.kind_of?(Array) or validations.empty?
62
82
  validations = [validations] unless validations.first.kind_of? Array
@@ -64,7 +84,7 @@ module ModelBuilder
64
84
  end
65
85
 
66
86
  def self.clean
67
- dynamic_models.map {|c| drop_table c }
87
+ relevant_database_tables.map {|table| drop_table table }
68
88
  @@dynamic_models = []
69
89
  end
70
90
 
@@ -72,8 +92,8 @@ module ModelBuilder
72
92
  @@dynamic_models
73
93
  end
74
94
 
75
- def self.drop_table(class_full_name)
76
- ActiveRecord::Migration.drop_table(table_name_for(class_full_name))
95
+ def self.drop_table(table)
96
+ ActiveRecord::Migration.drop_table(table)
77
97
  end
78
98
 
79
99
  end
@@ -2,8 +2,6 @@ require 'spec_helper'
2
2
  require 'support/database_connection'
3
3
  require 'support/dummy_module'
4
4
 
5
- Spec::Support::DatabaseConnection.establish_sqlite_connection
6
-
7
5
  describe ModelBuilder do
8
6
 
9
7
  let(:builder) { ModelBuilder }
@@ -29,8 +27,6 @@ describe ModelBuilder do
29
27
 
30
28
  before { @build_result = builder.build name, options }
31
29
 
32
- after(:all) { ModelBuilder.clean }
33
-
34
30
  describe '.build' do
35
31
 
36
32
  subject { @build_result }
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,15 @@
1
1
  require 'active_record'
2
2
  require 'simplecov'
3
+ require 'support/database_connection'
3
4
 
4
5
  SimpleCov.start
5
6
 
6
- require(File.expand_path '.','lib/model_builder')
7
+ require 'model_builder'
8
+
9
+ Spec::Support::DatabaseConnection.establish_sqlite_connection
10
+
11
+ RSpec.configure do |config|
12
+ config.after(:suite) do
13
+ ModelBuilder.clean
14
+ end
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - r4z3c
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-16 00:00:00.000000000 Z
11
+ date: 2016-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler