active_record_temptable 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9dd53e6cd28f2b48bc192fd59f36a268706f0f6b674414488a9043b636839154
4
+ data.tar.gz: b53b7343396c2c902fa8f95ecd8187b26426ca753f8c8eefffbb3657c26b202a
5
+ SHA512:
6
+ metadata.gz: 006c7c9147c553eb50b6af984ec915ff9356f301ba760b7d964dc9118e807f0948b4eae91be9ec96a37467e4a141dae237eb1e4eb4b209800e7c9429cabe980f
7
+ data.tar.gz: ab1e589be360f3abff00ba189ebfa903ab563ccb8d7e2cc42145bcc2d04e270e371555a831eeac83ef527d20ada7fb380f04b459e1a175a5e26bae19b25fbe74
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ services:
6
+ - postgresql
7
+ - mysql
8
+ rvm:
9
+ - 2.5.1
10
+ before_install:
11
+ gem install bundler -v 1.16.5
12
+ before_script:
13
+ bundle exec rake create_dbs
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in activerecord_temptable.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_record_temptable (0.1.0)
5
+ activerecord (>= 4.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activerecord (5.2.1)
13
+ activemodel (= 5.2.1)
14
+ activesupport (= 5.2.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ concurrent-ruby (1.1.3)
23
+ diff-lcs (1.3)
24
+ i18n (1.1.1)
25
+ concurrent-ruby (~> 1.0)
26
+ minitest (5.11.3)
27
+ mysql2 (0.4.10)
28
+ pg (0.18.4)
29
+ rake (10.5.0)
30
+ rspec (3.8.0)
31
+ rspec-core (~> 3.8.0)
32
+ rspec-expectations (~> 3.8.0)
33
+ rspec-mocks (~> 3.8.0)
34
+ rspec-core (3.8.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-expectations (3.8.2)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.8.0)
39
+ rspec-mocks (3.8.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.8.0)
42
+ rspec-support (3.8.0)
43
+ sqlite3 (1.3.13)
44
+ thread_safe (0.3.6)
45
+ tzinfo (1.2.5)
46
+ thread_safe (~> 0.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ active_record_temptable!
53
+ bundler (~> 1.16)
54
+ mysql2 (~> 0.4.1)
55
+ pg (~> 0.18.4)
56
+ rake (~> 10.0)
57
+ rspec (~> 3.0)
58
+ sqlite3 (~> 1.3)
59
+
60
+ BUNDLED WITH
61
+ 1.16.6
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # ActiverecordTemptable
2
+
3
+ ActiveRecordTemptable provides with_temptable method to cache query results using temporary table.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord_temptable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord_temptable
20
+
21
+ ## Reasoning
22
+ Creating temp tables to save time once querying big result set multiple times(different groupings, count etc) with a complex query.
23
+
24
+ ## Usage
25
+
26
+ `ActiveRecordTemptable.with_temptable` can be used to cache complex query's result.
27
+
28
+ For example using scheme from RailsTutorial
29
+ ```ruby
30
+ ActiveRecordTemptable.with_temptable(current_user.feed, [:user_id]) do |microposts|
31
+ @most_microposts = microposts.group(:user_id).count
32
+ @not_own_microposts = microposts.where.not(user_id: current_user).to_a # note that to_a is needed to load records
33
+ end
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies and create databases. Then, run `rake spec` to run the tests.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/railwaymen/active_record_temptable.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'yaml'
4
+
5
+ CONFIG = YAML::load(File.open(File.join(__dir__, 'database.yml')))
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
10
+
11
+ task create_dbs: %w[pg:build mysql:build]
12
+
13
+ namespace :pg do
14
+ task :build do
15
+ %x( createdb -U #{CONFIG['postgres']['user']} -E UTF8 #{CONFIG['postgres']['database']} )
16
+ end
17
+ end
18
+
19
+ namespace :mysql do
20
+ task :build do
21
+ %x( mysql -e "create DATABASE #{CONFIG["mysql"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_record_temptable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record_temptable"
8
+ spec.version = ActiveRecordTemptable::VERSION
9
+ spec.authors = ["Marcin Henryk Bartkowiak"]
10
+ spec.email = ["mhbartkowiak@gmail.com"]
11
+ spec.licenses = ["MIT"]
12
+
13
+ spec.summary = 'Temp Table for activerecord'
14
+ spec.homepage = "https://git.railwaymen.org/marcin.bartkowiak/active_record_temptable"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'activerecord', '>= 4.2.0'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
31
+ spec.add_development_dependency 'pg', '~> 0.18.4'
32
+ spec.add_development_dependency 'mysql2', '~> 0.4.1'
33
+ end
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+ bundle exec rake create_dbs
data/database.yml ADDED
@@ -0,0 +1,10 @@
1
+ postgres:
2
+ adapter: postgresql
3
+ database: active_record_temptable_test
4
+ user: postgres
5
+ sqlite:
6
+ adapter: sqlite3
7
+ database: ":memory:"
8
+ mysql:
9
+ adapter: mysql2
10
+ database: active_record_temptable_test
@@ -0,0 +1,3 @@
1
+ module ActiveRecordTemptable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record_temptable/version'
4
+ require 'active_record'
5
+
6
+ module ActiveRecordTemptable
7
+ extend self
8
+
9
+ DEFAULT_TABLE_NAME = 'activerecord_temptable_records'.freeze
10
+
11
+
12
+ # Yields provided relation loaded in temporary table
13
+ #
14
+ # @param relation [ActiveRecord::Relation]
15
+ # @param indexes [Array] add_index will be called for every element in indexes
16
+ # @param table_name [String] name of table
17
+ # @return [void]
18
+ def with_temptable(relation, indexes = [], table_name = DEFAULT_TABLE_NAME)
19
+ ActiveRecord::Base.connection_pool.with_connection do |connection|
20
+ connection.transaction do
21
+ begin
22
+ create_table(connection, relation, table_name)
23
+ create_indexes(connection, table_name, indexes) if indexes.any?
24
+ klass = relation.klass
25
+ yield klass.unscoped.from("#{table_name} AS #{klass.table_name}") if block_given?
26
+ ensure
27
+ connection.drop_table(table_name, if_exists: true)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ # Creates temp table and loads provided relation into it
34
+ #
35
+ # @param connection [ActiveRecord::ConnectionAdapters]
36
+ # @param relation [ActiveRecord::Relation]
37
+ # @param table_name [String] name of table
38
+ # @return [void]
39
+ def create_table(connection, relation, table_name)
40
+ connection.execute(new_table_command(relation, table_name))
41
+ end
42
+
43
+ # Calls add_index for every element in array
44
+ #
45
+ # @param connection [ActiveRecord::ConnectionAdapters]
46
+ # @param table_name [String]
47
+ # @param indexes [Array]
48
+ # @return [void]
49
+ def create_indexes(connection, table_name, indexes)
50
+ indexes.each do |fields, options|
51
+ connection.add_index(table_name, fields, options || {})
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def new_table_command(relation, table_name)
58
+ "CREATE TEMPORARY TABLE #{table_name} AS #{relation.to_sql}"
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_temptable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Henryk Bartkowiak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.18.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.18.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: mysql2
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.4.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.4.1
111
+ description:
112
+ email:
113
+ - mhbartkowiak@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - README.md
124
+ - Rakefile
125
+ - active_record_temptable.gemspec
126
+ - bin/setup
127
+ - database.yml
128
+ - lib/active_record_temptable.rb
129
+ - lib/active_record_temptable/version.rb
130
+ homepage: https://git.railwaymen.org/marcin.bartkowiak/active_record_temptable
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.7.7
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Temp Table for activerecord
154
+ test_files: []