m2m_fast_insert 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/.sass-cache
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1,5 @@
1
+ rvm --create ruby-1.9.3-p125@m2m_fast_insert
2
+ # Ensure that Bundler is installed, install it if it is not.
3
+ if ! command -v bundle > /dev/null; then
4
+ gem install bundler
5
+ fi
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ # uncomment this line if your project needs to run something other than `rake`:
6
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ m2m_fast_insert (0.0.1)
5
+ activerecord (>= 3.0.0, < 4.0.0)
6
+ activesupport (>= 3.0.0, < 4.0.0)
7
+ rails (~> 3.0.13)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ abstract (1.0.0)
13
+ actionmailer (3.0.13)
14
+ actionpack (= 3.0.13)
15
+ mail (~> 2.2.19)
16
+ actionpack (3.0.13)
17
+ activemodel (= 3.0.13)
18
+ activesupport (= 3.0.13)
19
+ builder (~> 2.1.2)
20
+ erubis (~> 2.6.6)
21
+ i18n (~> 0.5.0)
22
+ rack (~> 1.2.5)
23
+ rack-mount (~> 0.6.14)
24
+ rack-test (~> 0.5.7)
25
+ tzinfo (~> 0.3.23)
26
+ activemodel (3.0.13)
27
+ activesupport (= 3.0.13)
28
+ builder (~> 2.1.2)
29
+ i18n (~> 0.5.0)
30
+ activerecord (3.0.13)
31
+ activemodel (= 3.0.13)
32
+ activesupport (= 3.0.13)
33
+ arel (~> 2.0.10)
34
+ tzinfo (~> 0.3.23)
35
+ activeresource (3.0.13)
36
+ activemodel (= 3.0.13)
37
+ activesupport (= 3.0.13)
38
+ activesupport (3.0.13)
39
+ arel (2.0.10)
40
+ builder (2.1.2)
41
+ diff-lcs (1.1.3)
42
+ erubis (2.6.6)
43
+ abstract (>= 1.0.0)
44
+ i18n (0.5.0)
45
+ json (1.7.3)
46
+ mail (2.2.19)
47
+ activesupport (>= 2.3.6)
48
+ i18n (>= 0.4.0)
49
+ mime-types (~> 1.16)
50
+ treetop (~> 1.4.8)
51
+ mime-types (1.18)
52
+ polyglot (0.3.3)
53
+ rack (1.2.5)
54
+ rack-mount (0.6.14)
55
+ rack (>= 1.0.0)
56
+ rack-test (0.5.7)
57
+ rack (>= 1.0)
58
+ rails (3.0.13)
59
+ actionmailer (= 3.0.13)
60
+ actionpack (= 3.0.13)
61
+ activerecord (= 3.0.13)
62
+ activeresource (= 3.0.13)
63
+ activesupport (= 3.0.13)
64
+ bundler (~> 1.0)
65
+ railties (= 3.0.13)
66
+ railties (3.0.13)
67
+ actionpack (= 3.0.13)
68
+ activesupport (= 3.0.13)
69
+ rake (>= 0.8.7)
70
+ rdoc (~> 3.4)
71
+ thor (~> 0.14.4)
72
+ rake (0.9.2.2)
73
+ rdoc (3.12)
74
+ json (~> 1.4)
75
+ rspec (2.10.0)
76
+ rspec-core (~> 2.10.0)
77
+ rspec-expectations (~> 2.10.0)
78
+ rspec-mocks (~> 2.10.0)
79
+ rspec-core (2.10.1)
80
+ rspec-expectations (2.10.0)
81
+ diff-lcs (~> 1.1.3)
82
+ rspec-mocks (2.10.1)
83
+ rspec-rails (2.10.1)
84
+ actionpack (>= 3.0)
85
+ activesupport (>= 3.0)
86
+ railties (>= 3.0)
87
+ rspec (~> 2.10.0)
88
+ sqlite3 (1.3.6)
89
+ thor (0.14.6)
90
+ treetop (1.4.10)
91
+ polyglot
92
+ polyglot (>= 0.3.1)
93
+ tzinfo (0.3.33)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ m2m_fast_insert!
100
+ rake
101
+ rspec-rails
102
+ sqlite3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,5 @@
1
+ # M2mFastInsert
2
+
3
+ [![TravisCI](https://secure.travis-ci.org/jphenow/m2m_fast_insert.png "TravisCI")](http://travis-ci.org/jphenow/m2m_fast_insert "Travis-CI M2MFastInsert")
4
+
5
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rake'
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'M2MFastInsert'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ require 'rspec/core/rake_task'
24
+
25
+ RSpec::Core::RakeTask.new('spec')
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ module M2MFastInsert
2
+ class Base
3
+ attr_reader :ids, :options, :id, :join_table, :join_column_name, :table_name
4
+
5
+ def initialize(id, join_column_name, table_name, join_table, *args)
6
+ @ids = args[0]
7
+ raise TypeError, "IDs must be fixnums" if ids.any? { |i| !i.is_a? Fixnum }
8
+ @options = args[1].present? ? args[1] : {}
9
+ @ids.uniq! if options[:unique]
10
+ @id = id
11
+ @join_table = join_table
12
+ @join_column_name = join_column_name
13
+ @table_name = table_name
14
+ end
15
+
16
+ def inserts
17
+ @inserts ||= begin inserts = []
18
+ ids.each do |given_id|
19
+ inserts << "(#{id}, #{given_id})"
20
+ end
21
+ inserts.join ", "
22
+ end
23
+ end
24
+
25
+ def sql
26
+ "INSERT INTO #{join_table} (`#{table_name}_id`, `#{join_column_name}_id`) VALUES #{inserts}"
27
+ end
28
+
29
+ def fast_insert
30
+ ActiveRecord::Base.connection.execute sql
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,38 @@
1
+ module M2MFastInsert
2
+ module HasAndBelongsToManyOverride
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ class_eval do
6
+ # Create Method chain if habtm is defined - This is because it goes down to AR::Base
7
+ # and errors because at the time of inclusion, there is none defined
8
+ alias_method_chain :has_and_belongs_to_many, :fast_inserts if self.method_defined? :has_and_belongs_to_many
9
+ end
10
+ end
11
+
12
+ # Rig the original habtm to call our method definition
13
+ #
14
+ # name - Plural name of the model we're associating with
15
+ # options - see ActiveRecord docs
16
+ def has_and_belongs_to_many_with_fast_inserts(name, options = {})
17
+ has_and_belongs_to_many_without_fast_inserts(name, options)
18
+ define_fast_methods_for_model(name, options)
19
+ end
20
+
21
+ private
22
+ # Get necessary table and column information so we can define
23
+ # fast insertion methods
24
+ #
25
+ # name - Plural name of the model we're associating with
26
+ # options - see ActiveRecord docs
27
+ def define_fast_methods_for_model(name, options)
28
+ join_table = options[:join_table]
29
+ join_column_name = name.to_s.downcase.singularize
30
+ define_method "fast_#{join_column_name}_ids_insert" do |*args|
31
+ table_name = self.class.table_name.singularize
32
+ insert = M2MFastInsert::Base.new id, join_column_name, table_name, join_table, *args
33
+ insert.fast_insert
34
+ end
35
+ end
36
+ end
37
+ end
38
+ ActiveRecord::AutosaveAssociation::ClassMethods.send :include, M2MFastInsert::HasAndBelongsToManyOverride
@@ -0,0 +1,3 @@
1
+ module M2MFastInsert
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+ require 'active_support/all'
3
+ require 'm2m_fast_insert/base'
4
+ require 'm2m_fast_insert/version'
5
+ require 'm2m_fast_insert/has_and_belongs_to_many_override'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :m2m_fast_insert do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ # Maintain your gem's version:
5
+ require "m2m_fast_insert/version"
6
+
7
+ # Describe your gem and declare its dependencies:
8
+ Gem::Specification.new do |s|
9
+ s.name = "m2m_fast_insert"
10
+ s.version = M2MFastInsert::VERSION
11
+ s.authors = ["Jon Phenow"]
12
+ s.email = ["jon.phenow@tstmedia.com"]
13
+ s.homepage = ""
14
+ s.summary = %q{Fast Inserts for Rails Many to Many relations}
15
+ s.description = %q{}
16
+
17
+ s.rubyforge_project = "m2m_fast_insert"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_dependency "rails", "~> 3.0.13"
25
+ s.add_runtime_dependency('activerecord', [">= 3.0.0", "< 4.0.0"])
26
+ s.add_runtime_dependency('activesupport', [">= 3.0.0", "< 4.0.0"])
27
+
28
+ s.add_development_dependency "rake"
29
+ s.add_development_dependency "rspec-rails"
30
+ s.add_development_dependency "sqlite3"
31
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ module M2MFastInsert
4
+ describe Base do
5
+ let(:id) { 1 }
6
+ let(:join_column_name) { 'post' }
7
+ let(:table_name) { 'user' }
8
+ let(:join_table) { 'posts_users' }
9
+ describe "basic init" do
10
+ let(:ids) { [1,2,3] }
11
+ subject do
12
+ Base.new(id, join_column_name, table_name, join_table, ids)
13
+ end
14
+
15
+ it { should respond_to :fast_insert }
16
+
17
+ it "allows a basic init" do
18
+ subject.id.should == id
19
+ subject.join_column_name.should == join_column_name
20
+ subject.table_name.should == table_name
21
+ subject.join_table.should == join_table
22
+ subject.ids.should == ids
23
+ subject.options.should == {}
24
+ end
25
+
26
+ it "builds inserts" do
27
+ subject.inserts.should == "(1, 1), (1, 2), (1, 3)"
28
+ end
29
+
30
+ it "builds INSERT statement" do
31
+ subject.sql.should == "INSERT INTO posts_users (`user_id`, `post_id`) VALUES #{subject.inserts}"
32
+ end
33
+
34
+ it "executes SQL" do
35
+ ActiveRecord::Base.connection.should_receive(:execute).with(subject.sql)
36
+ subject.fast_insert
37
+ end
38
+ end
39
+
40
+ describe "basic init" do
41
+ let(:ids) { ['1',2,3] }
42
+ it "denies non-fixnum ids" do
43
+ expect {
44
+ Base.new(id, join_column_name, table_name, join_table, ids)
45
+ }.to raise_error(TypeError, 'IDs must be fixnums')
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe User do
4
+ it { should respond_to(:fast_post_ids_insert) }
5
+ it { should_not respond_to(:fast_friend_ids_insert) }
6
+ end
7
+
8
+ describe Post do
9
+ it { should respond_to(:fast_user_ids_insert) }
10
+ end
11
+
12
+ describe Friend do
13
+ it { should_not respond_to(:fast_user_ids_insert) }
14
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe M2MFastInsert do
4
+ it "has a VERSION" do
5
+ M2MFastInsert::VERSION.should_not be_nil
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ RSpec.configure do |config|
2
+ config.requires = %w[m2m_fast_insert sqlite3]
3
+
4
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
5
+
6
+ # Requires supporting ruby files with custom matchers and macros, etc,
7
+ # in spec/support/ and its subdirectories.
8
+ Dir.glob(File.expand_path('../support/**/*.rb', __FILE__)).each { |file| require file }
9
+
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+ config.color = true
14
+ end
@@ -0,0 +1,43 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :users, :force => true do |t|
3
+ t.string :name
4
+ end
5
+
6
+ create_table :posts_users, :id => false, :force => true do |t|
7
+ t.references :user, :null => false
8
+ t.references :post, :null => false
9
+ end
10
+
11
+ create_table :posts, :force => true do |t|
12
+ t.string :content
13
+ end
14
+
15
+ create_table :connections, :force => true do |t|
16
+ t.references :user
17
+ t.references :friend
18
+ end
19
+
20
+ create_table :friends, :force => true do |t|
21
+ t.string :type
22
+ end
23
+ end
24
+
25
+ class User < ActiveRecord::Base
26
+ has_and_belongs_to_many :posts
27
+ has_many :connections
28
+ has_many :friends, :through => :connections
29
+ end
30
+
31
+ class Post < ActiveRecord::Base
32
+ has_and_belongs_to_many :users
33
+ end
34
+
35
+ class Connection < ActiveRecord::Base
36
+ has_many :friends
37
+ has_many :users
38
+ end
39
+
40
+ class Friend < ActiveRecord::Base
41
+ has_many :connections
42
+ has_many :users, :through => :connections
43
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: m2m_fast_insert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jon Phenow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.13
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.13
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.0
38
+ - - <
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.0
49
+ - - <
50
+ - !ruby/object:Gem::Version
51
+ version: 4.0.0
52
+ - !ruby/object:Gem::Dependency
53
+ name: activesupport
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 3.0.0
60
+ - - <
61
+ - !ruby/object:Gem::Version
62
+ version: 4.0.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: 3.0.0
71
+ - - <
72
+ - !ruby/object:Gem::Version
73
+ version: 4.0.0
74
+ - !ruby/object:Gem::Dependency
75
+ name: rake
76
+ requirement: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ - !ruby/object:Gem::Dependency
91
+ name: rspec-rails
92
+ requirement: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ type: :development
99
+ prerelease: false
100
+ version_requirements: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ - !ruby/object:Gem::Dependency
107
+ name: sqlite3
108
+ requirement: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ type: :development
115
+ prerelease: false
116
+ version_requirements: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ description: ''
123
+ email:
124
+ - jon.phenow@tstmedia.com
125
+ executables: []
126
+ extensions: []
127
+ extra_rdoc_files: []
128
+ files:
129
+ - .gitignore
130
+ - .rspec
131
+ - .rvmrc
132
+ - .travis.yml
133
+ - Gemfile
134
+ - Gemfile.lock
135
+ - MIT-LICENSE
136
+ - README.markdown
137
+ - Rakefile
138
+ - lib/m2m_fast_insert.rb
139
+ - lib/m2m_fast_insert/base.rb
140
+ - lib/m2m_fast_insert/has_and_belongs_to_many_override.rb
141
+ - lib/m2m_fast_insert/version.rb
142
+ - lib/tasks/m2m_fast_insert_tasks.rake
143
+ - m2m_fast_insert.gemspec
144
+ - spec/m2m_fast_insert/base_spec.rb
145
+ - spec/m2m_fast_insert/has_and_belongs_to_many_override_spec.rb
146
+ - spec/m2m_fast_insert_spec.rb
147
+ - spec/spec_helper.rb
148
+ - spec/support/test_model.rb
149
+ homepage: ''
150
+ licenses: []
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ segments:
162
+ - 0
163
+ hash: -2433803218446936242
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ segments:
171
+ - 0
172
+ hash: -2433803218446936242
173
+ requirements: []
174
+ rubyforge_project: m2m_fast_insert
175
+ rubygems_version: 1.8.24
176
+ signing_key:
177
+ specification_version: 3
178
+ summary: Fast Inserts for Rails Many to Many relations
179
+ test_files:
180
+ - spec/m2m_fast_insert/base_spec.rb
181
+ - spec/m2m_fast_insert/has_and_belongs_to_many_override_spec.rb
182
+ - spec/m2m_fast_insert_spec.rb
183
+ - spec/spec_helper.rb
184
+ - spec/support/test_model.rb