active_table 0.0.4 → 0.0.5

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.
@@ -1,11 +1,10 @@
1
1
  module ActiveTable
2
2
  class Base < ActiveRecord::Base
3
- @@tables = {}
3
+ @@insert_statements = []
4
4
  def self.active_table(&block)
5
+ @table = {}
5
6
  block.call
6
- ActiveRecord::Base.connection_pool.connections.each do |connection|
7
- create_tables(connection)
8
- end
7
+ create_table!
9
8
  self.reset_column_information
10
9
  self.define_attribute_methods
11
10
  end
@@ -13,35 +12,38 @@ module ActiveTable
13
12
  def self.create_table(name, options = {}, &block)
14
13
  class_name = self.name
15
14
  self.set_table_name name
16
- @@tables[class_name] = {:name => name, :options => options, :block => block, :rows => []}
15
+ @table = {:name => name, :options => options, :block => block, :rows => []}
17
16
  end
18
17
 
19
18
  def self.insert(params)
20
19
  class_name = self.name
21
- @@tables[class_name][:rows] << params
20
+ @table[:rows] << params
22
21
  end
23
22
 
24
- def self.create_tables(connection)
25
- started_inside_a_transaction = !connection.outside_transaction?
26
- begin
27
- connection.execute("COMMIT") if started_inside_a_transaction
28
- rescue ActiveRecord::StatementInvalid
29
- started_inside_a_transaction = false
23
+ def self.create_table!
24
+ connection = ActiveRecord::Base.connection
25
+ connection.execute "DROP TABLE IF EXISTS #{connection.quote_table_name(@table[:name])}"
26
+
27
+ connection.create_table @table[:name], @table[:options] do |t|
28
+ @table[:block].call(t)
30
29
  end
31
30
 
32
- @@tables.each_value do |table|
33
- next if connection.active_table_loaded_for?(table[:name])
34
- connection.create_table table[:name], table[:options].merge(:temporary => true) do |t|
35
- table[:block].call(t)
36
- end
31
+ @@insert_statements.reject!{|statement| statement[:table_name] == @table[:name]}
32
+ @table[:rows].each do |row|
33
+ insert_sql = generate_insert_sql_for_hash(connection, @table[:name], row)
34
+ connection.execute insert_sql
35
+ @@insert_statements << {:table_name => @table[:name], :sql => insert_sql}
36
+ end
37
+ end
37
38
 
38
- table[:rows].each do |row|
39
- connection.execute generate_insert_sql_for_hash(connection, table[:name], row)
39
+ def self.seed!
40
+ connection = ActiveRecord::Base.connection
41
+ @@insert_statements.each do |insert|
42
+ begin
43
+ connection.execute(insert[:sql])
44
+ rescue ActiveRecord::RecordNotUnique
40
45
  end
41
- connection.mark_active_table_as_loaded(table[:name])
42
46
  end
43
-
44
- connection.execute("BEGIN") if started_inside_a_transaction
45
47
  end
46
48
 
47
49
  def self.generate_insert_sql_for_hash(connection, table_name, params)
@@ -49,9 +51,5 @@ module ActiveTable
49
51
  values = params.values.map {|v| connection.quote(v.to_s)}.join(", ")
50
52
  "INSERT INTO #{connection.quote_table_name(table_name.to_s)} (#{keys}) VALUES (#{values})"
51
53
  end
52
-
53
- def self.reset_table_information
54
- @@tables = {}
55
- end
56
54
  end
57
55
  end
@@ -0,0 +1,14 @@
1
+ module ActiveTable
2
+ class Railtie < Rails::Railtie
3
+ initializer "active_table", :after => "casebook2_extensions" do
4
+ Dir.glob("#{Rails.root}/app/models/**/*.rb").each do |file|
5
+ File.open(file).readlines.each do |line|
6
+ if (line =~ /ActiveTable::Base/)
7
+ require file if Rails.env.dev?
8
+ next
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveTable
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/active_table.rb CHANGED
@@ -1,7 +1,4 @@
1
1
  require 'active_record'
2
- require 'active_record/connection_adapters/abstract_adapter'
3
- require 'active_record/connection_adapters/abstract_adapter_extensions'
4
- require 'active_record/connection_adapters/sqlite_adapter'
5
- require 'active_record/connection_adapters/sqlite_adapter_extensions'
6
- require 'active_record/connection_adapters/abstract/connection_pool_extensions'
2
+
7
3
  require 'active_table/base'
4
+ require 'active_table/railtie' if defined?(Rails)
@@ -3,11 +3,11 @@ require 'spec_helper'
3
3
  describe "a model created with active_table" do
4
4
  before(:each) do
5
5
  @connection_pool = ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
6
+ @connection = @connection_pool.connection
6
7
  end
7
8
 
8
9
  after(:each) do
9
10
  @connection_pool.disconnect!
10
- ActiveTable::Base.reset_table_information
11
11
  end
12
12
 
13
13
  it "should inherit from ActiveRecord::Base" do
@@ -15,9 +15,10 @@ describe "a model created with active_table" do
15
15
  end
16
16
 
17
17
  it "should behave like an ActiveRecord model" do
18
- class TemporaryModel < ActiveTable::Base
18
+
19
+ class AwesomeModel < ActiveTable::Base
19
20
  active_table do
20
- create_table :temporary_models do |t|
21
+ create_table :awesome_models do |t|
21
22
  t.string :name
22
23
  end
23
24
 
@@ -26,41 +27,16 @@ describe "a model created with active_table" do
26
27
  end
27
28
  end
28
29
 
29
- connection = @connection_pool.connection
30
-
31
- TemporaryModel.count.should == 2
32
- end
33
-
34
- it "should only create the table once for a given connection" do
35
- connection = @connection_pool.checkout
36
-
37
- connection.active_table_loaded_for?(:temporary_models).should be_false
38
-
39
- class TemporaryModel < ActiveTable::Base
40
- active_table do
41
- create_table :temporary_models do |t|
42
- t.string :name
43
- end
44
- end
45
- end
46
-
47
- ActiveTable::Base.create_tables(connection)
48
- connection.active_table_loaded_for?(:temporary_models).should be_true
49
-
50
- lambda {
51
- ActiveTable::Base.create_tables(connection)
52
- }.should_not raise_error
30
+ AwesomeModel.count.should == 2
53
31
  end
54
32
 
55
33
  context "#active_table" do
56
34
  it "should perform oprerations immediately and correctly reload attributes" do
57
- connection = @connection_pool.checkout
58
-
59
35
  lambda {
60
- class TemporaryModel < ActiveTable::Base
36
+ class AwesomeModel < ActiveTable::Base
61
37
  active_table do
62
- self.table_name = "temporary_models"
63
- create_table :temporary_models do |t|
38
+ self.table_name = "awesome_models"
39
+ create_table :awesome_models do |t|
64
40
  t.string :name
65
41
  end
66
42
  end
@@ -71,52 +47,69 @@ describe "a model created with active_table" do
71
47
  end
72
48
  end
73
49
 
50
+ context "#seed!" do
51
+ it "should attempt to re-insert all rows" do
52
+ class AwesomeModel < ActiveTable::Base
53
+ active_table do
54
+ create_table :awesome_models do |t|
55
+ t.string :name
56
+ end
57
+
58
+ insert :id => 1, :name => "foo"
59
+ insert :id => 4, :name => "baz"
60
+ end
61
+ end
62
+
63
+ @connection.execute("SELECT * FROM awesome_models").size.should == 2
64
+ @connection.execute("DELETE FROM awesome_models")
65
+ @connection.execute("SELECT * FROM awesome_models").size.should == 0
66
+ ActiveTable::Base.seed!
67
+ @connection.execute("SELECT * FROM awesome_models").size.should == 2
68
+ end
69
+ end
70
+
74
71
  context "when a connection is opened" do
75
72
  it "should create a table from its model definition" do
76
- class TemporaryModel < ActiveTable::Base
73
+ class AwesomeModel < ActiveTable::Base
77
74
  active_table do
78
- create_table :temporary_models do |t|
75
+ create_table :awesome_models do |t|
79
76
  t.string :name
80
77
  end
81
78
  end
82
79
  end
83
80
 
84
- connection = @connection_pool.connection
85
-
86
81
  lambda {
87
- connection.execute("SELECT * FROM temporary_models")
82
+ @connection.execute("SELECT * FROM awesome_models")
88
83
  }.should_not raise_error
89
84
  end
90
85
 
91
86
  it "should create multiple tables at the same time" do
92
- class TemporaryModel < ActiveTable::Base
87
+ class AwesomeModel < ActiveTable::Base
93
88
  active_table do
94
- create_table :temporary_models do |t|
89
+ create_table :awesome_models do |t|
95
90
  t.string :name
96
91
  end
97
92
  end
98
93
  end
99
94
 
100
- class TemporaryModelTwo < ActiveTable::Base
95
+ class AwesomeModelTwo < ActiveTable::Base
101
96
  active_table do
102
- create_table :temporary_models_two do |t|
97
+ create_table :awesome_models_two do |t|
103
98
  t.string :name
104
99
  end
105
100
  end
106
101
  end
107
102
 
108
- connection = @connection_pool.connection
109
-
110
103
  lambda {
111
- connection.execute("SELECT * FROM temporary_models")
112
- connection.execute("SELECT * FROM temporary_models_two")
104
+ @connection.execute("SELECT * FROM awesome_models")
105
+ @connection.execute("SELECT * FROM awesome_models_two")
113
106
  }.should_not raise_error
114
107
  end
115
108
 
116
109
  it "should insert rows of data specified within the model" do
117
- class TemporaryModel < ActiveTable::Base
110
+ class AwesomeModel < ActiveTable::Base
118
111
  active_table do
119
- create_table :temporary_models do |t|
112
+ create_table :awesome_models do |t|
120
113
  t.string :name
121
114
  end
122
115
 
@@ -125,19 +118,17 @@ describe "a model created with active_table" do
125
118
  end
126
119
  end
127
120
 
128
- connection = @connection_pool.connection
129
-
130
- connection.execute("SELECT * FROM temporary_models").size.should == 2
131
- connection.execute("SELECT * FROM temporary_models WHERE id = 1").first["name"].should == "foo"
132
- connection.execute("SELECT * FROM temporary_models WHERE name = 'baz'").first["id"].should == 4
121
+ @connection.execute("SELECT * FROM awesome_models").size.should == 2
122
+ @connection.execute("SELECT * FROM awesome_models WHERE id = 1").first["name"].should == "foo"
123
+ @connection.execute("SELECT * FROM awesome_models WHERE name = 'baz'").first["id"].should == 4
133
124
  end
134
125
  end
135
126
 
136
127
  context "when inserting data" do
137
128
  it "should handle apostrophes without blowing up" do
138
- class TemporaryModel < ActiveTable::Base
129
+ class AwesomeModel < ActiveTable::Base
139
130
  active_table do
140
- create_table :temporary_models do |t|
131
+ create_table :awesome_models do |t|
141
132
  t.string :name
142
133
  end
143
134
 
@@ -145,33 +136,7 @@ describe "a model created with active_table" do
145
136
  end
146
137
  end
147
138
 
148
- connection = @connection_pool.connection
149
-
150
- connection.execute("SELECT * FROM temporary_models WHERE id = 1").first["name"].should == "foo's the boss?"
151
- end
152
- end
153
-
154
- context "with multiple connections" do
155
- it "should create a temporary table for each connection" do
156
- class TemporaryModel < ActiveTable::Base
157
- active_table do
158
- create_table :temporary_models do |t|
159
- t.string :name
160
- end
161
- end
162
- end
163
-
164
- connections = []
165
- 3.times do
166
- connections << @connection_pool.checkout
167
- end
168
-
169
- connections.first.should_not == connections.last
170
- connections.each do |connection|
171
- lambda {
172
- connection.execute("SELECT * FROM temporary_models")
173
- }.should_not raise_error
174
- end
139
+ @connection.execute("SELECT * FROM awesome_models WHERE id = 1").first["name"].should == "foo's the boss?"
175
140
  end
176
141
  end
177
142
  end
metadata CHANGED
@@ -1,42 +1,38 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: active_table
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
4
5
  prerelease:
5
- version: 0.0.4
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Case Commons LLC
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-07-01 00:00:00 -04:00
12
+ date: 2011-07-08 00:00:00.000000000 -04:00
14
13
  default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
17
16
  name: activerecord
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2152706580 !ruby/object:Gem::Requirement
20
18
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
24
22
  version: 2.3.5
25
23
  - - <
26
- - !ruby/object:Gem::Version
24
+ - !ruby/object:Gem::Version
27
25
  version: 4.0.0
28
26
  type: :runtime
29
- version_requirements: *id001
27
+ prerelease: false
28
+ version_requirements: *2152706580
30
29
  description: Dynamically-populated ActiveRecord models based on static data
31
- email:
30
+ email:
32
31
  - casecommons-dev@googlegroups.com
33
32
  executables: []
34
-
35
33
  extensions: []
36
-
37
34
  extra_rdoc_files: []
38
-
39
- files:
35
+ files:
40
36
  - .gitignore
41
37
  - Gemfile
42
38
  - README.md
@@ -45,42 +41,37 @@ files:
45
41
  - gemfiles/Gemfile.common
46
42
  - gemfiles/rspec1/Gemfile
47
43
  - gemfiles/rspec2/Gemfile
48
- - lib/active_record/connection_adapters/abstract/connection_pool_extensions.rb
49
- - lib/active_record/connection_adapters/abstract_adapter_extensions.rb
50
- - lib/active_record/connection_adapters/sqlite_adapter_extensions.rb
51
44
  - lib/active_table.rb
52
45
  - lib/active_table/base.rb
46
+ - lib/active_table/railtie.rb
53
47
  - lib/active_table/version.rb
54
48
  - spec/active_table_spec.rb
55
49
  - spec/spec_helper.rb
56
50
  has_rdoc: true
57
51
  homepage: https://github.com/Casecommons/active_table
58
52
  licenses: []
59
-
60
53
  post_install_message:
61
54
  rdoc_options: []
62
-
63
- require_paths:
55
+ require_paths:
64
56
  - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
57
+ required_ruby_version: !ruby/object:Gem::Requirement
66
58
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: "0"
71
- required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
64
  none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: "0"
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
77
69
  requirements: []
78
-
79
70
  rubyforge_project: active_table
80
71
  rubygems_version: 1.6.2
81
72
  signing_key:
82
73
  specification_version: 3
83
74
  summary: Dynamically-populated ActiveRecord models based on static data
84
- test_files:
75
+ test_files:
85
76
  - spec/active_table_spec.rb
86
77
  - spec/spec_helper.rb
@@ -1,11 +0,0 @@
1
- ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
2
- attr_reader :connections
3
-
4
- private
5
- def new_connection
6
- connection = ActiveRecord::Base.send(spec.adapter_method, spec.config)
7
- ActiveTable::Base.create_tables(connection)
8
- connection
9
- end
10
- end
11
-
@@ -1,12 +0,0 @@
1
- ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
2
- def mark_active_table_as_loaded(table_name)
3
- @active_tables_loaded ||= []
4
- @active_tables_loaded << table_name
5
- end
6
-
7
- def active_table_loaded_for?(table_name)
8
- return false if @active_tables_loaded.blank?
9
- @active_tables_loaded.include?(table_name)
10
- end
11
- end
12
-
@@ -1,15 +0,0 @@
1
- ActiveRecord::ConnectionAdapters::SQLiteAdapter.class_eval do
2
- def tables(name = nil) #:nodoc:
3
- sql = <<-SQL
4
- SELECT name FROM sqlite_master
5
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
6
- UNION
7
- SELECT name FROM sqlite_temp_master
8
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
9
- SQL
10
-
11
- execute(sql, name).map do |row|
12
- row['name']
13
- end
14
- end
15
- end