update_all_scope 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2
5
+ - 2.6
6
+ services:
7
+ - mysql
8
+ addons:
9
+ postgresql: "9.6"
10
+ env:
11
+ global:
12
+ - CC_TEST_REPORTER_ID=a114ded507ef07caf5bdc0ccaae726315144d079b728baed0bcdd9f17a260b65
13
+ matrix:
14
+ - DB=mysql
15
+ - DB=pg
16
+ gemfile:
17
+ - gemfiles/3.2.gemfile
18
+ - gemfiles/4.2.gemfile
19
+ - gemfiles/5.0.gemfile
20
+ - gemfiles/5.1.gemfile
21
+ - gemfiles/5.2.gemfile
22
+ - gemfiles/6.0.gemfile
23
+ matrix:
24
+ exclude:
25
+ - gemfile: gemfiles/3.2.gemfile
26
+ rvm: 2.6
27
+ - gemfile: gemfiles/6.0.gemfile
28
+ rvm: 2.2
29
+ before_install:
30
+ - gem i rubygems-update -v '<3' && update_rubygems
31
+ - gem install bundler -v 1.17.3
32
+ - gem --version
33
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
34
+ - chmod +x ./cc-test-reporter
35
+ - ./cc-test-reporter before-build
36
+ before_script:
37
+ - mysql -V
38
+ - mysql -u root -e 'CREATE DATABASE travis_ci_test;'
39
+ - psql -c "SELECT version();"
40
+ - psql -c 'create database travis_ci_test;' -U postgres
41
+ script:
42
+ - bundle exec rake test
43
+ after_script:
44
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
45
+
@@ -0,0 +1 @@
1
+ ## Change Log
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Rumble Huang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # UpdateAllScope
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/update_all_scope.svg?style=flat)](https://rubygems.org/gems/update_all_scope)
4
+ [![Build Status](https://api.travis-ci.com/khiav223577/update_all_scope.svg?branch=master)](https://travis-ci.com/khiav223577/update_all_scope)
5
+ [![RubyGems](http://img.shields.io/gem/dt/update_all_scope.svg?style=flat)](https://rubygems.org/gems/update_all_scope)
6
+ [![Code Climate](https://codeclimate.com/github/khiav223577/update_all_scope/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/update_all_scope)
7
+ [![Test Coverage](https://codeclimate.com/github/khiav223577/update_all_scope/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/update_all_scope/coverage)
8
+
9
+ `update_all_scope` is a Ruby Gem for you to write update queries.
10
+
11
+ Supports Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'update_all_scope'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install update_all_scope
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test DB=mysql` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ 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).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/update_all_scope. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the UpdateAllScope project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/update_all_scope/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'atomically'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install --gemfile=gemfiles/4.2.gemfile
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 3.2.0'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2', '0.3.21'
8
+ when 'postgres' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem 'simplecov'
11
+ gem 'i18n', '< 1.6'
12
+ end
13
+
14
+ gemspec path: '../'
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.2.0'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2', '0.3.21'
8
+ when 'postgres' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem 'simplecov'
11
+ gem 'i18n', '< 1.6'
12
+ end
13
+
14
+ gemspec path: '../'
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 5.0.0'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2', '0.3.21'
8
+ when 'postgres' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem 'simplecov'
11
+ gem 'i18n', '< 1.6'
12
+ end
13
+
14
+ gemspec path: '../'
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 5.1.0'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2', '0.3.21'
8
+ when 'postgres' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem 'simplecov'
11
+ gem 'i18n', '< 1.6'
12
+ end
13
+
14
+ gemspec path: '../'
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 5.2.0'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2', '0.5.1'
8
+ when 'postgres' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem 'simplecov'
11
+ gem 'i18n', '< 1.6'
12
+ end
13
+
14
+ gemspec path: '../'
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 6.0.0'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2', '0.5.1'
8
+ when 'postgres' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'update_all_scope/version'
4
+ require 'update_all_scope/update_all_scope'
5
+
6
+ module UpdateAllScope
7
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ module UpdateAllScope
5
+ class UpdateAllScope
6
+ AREL_SUPPORT_JOIN_TABLE = Gem::Version.new(Arel::VERSION) >= Gem::Version.new('10')
7
+
8
+ def initialize(model: nil, relation: nil)
9
+ @queries = []
10
+ @relation = relation || model.class.where(id: model.id)
11
+ end
12
+
13
+ def where(*args)
14
+ @relation = @relation.where(*args)
15
+ return self
16
+ end
17
+
18
+ def update(query, *binding_values)
19
+ args = binding_values.size > 0 ? [[query, *binding_values]] : [query]
20
+ @queries << klass.send(:sanitize_sql_for_assignment, *args)
21
+ return self
22
+ end
23
+
24
+ def do_query!
25
+ return 0 if @queries.empty?
26
+ return @relation.update_all(updates_as_string)
27
+ end
28
+
29
+ def updates_as_string
30
+ @queries.join(',')
31
+ end
32
+
33
+ def klass
34
+ @relation.klass
35
+ end
36
+
37
+ # See: https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/activerecord/lib/active_record/relation.rb#L315
38
+ def to_arel
39
+ if @relation.eager_loading?
40
+ scope = UpdateAllScope.new(model: model, relation: @relation.apply_join_dependency)
41
+ return scope.update(updates_as_string).to_update_manager
42
+ end
43
+
44
+ stmt = new_arel_update_manager
45
+
46
+ stmt.set Arel.sql(klass.send(:sanitize_sql_for_assignment, updates_as_string))
47
+ stmt.table(stmt_table)
48
+ stmt.key = arel_attribute(@relation.primary_key)
49
+
50
+ if should_use_join_to_update?
51
+ join_to_update(klass.connection, stmt, stmt.key)
52
+ else
53
+ stmt.take(@relation.arel.limit)
54
+ stmt.order(*@relation.arel.orders)
55
+ stmt.wheres = @relation.arel.constraints
56
+ end
57
+
58
+ return stmt
59
+ end
60
+
61
+ def to_sql
62
+ connection = klass.connection
63
+ sql, binds = to_sql_and_binds(connection, to_arel)
64
+ type_casted_binds(connection, binds).each_with_index{|var, idx| sql = sql.gsub("$#{idx + 1}", connection.quote(var)) }
65
+ return sql
66
+ end
67
+
68
+ private
69
+
70
+ def should_use_join_to_update?
71
+ return false if AREL_SUPPORT_JOIN_TABLE
72
+ return true if has_join_values?
73
+ return true if @relation.offset_value
74
+ return false
75
+ end
76
+
77
+ def stmt_table
78
+ return @relation.table if not AREL_SUPPORT_JOIN_TABLE
79
+ return @relation.arel.join_sources.empty? ? @relation.table : @relation.arel.source
80
+ end
81
+
82
+ def new_arel_update_manager
83
+ return Arel::UpdateManager.new(ActiveRecord::Base) if Gem::Version.new(Arel::VERSION) < Gem::Version.new('7')
84
+ return Arel::UpdateManager.new
85
+ end
86
+
87
+ def has_join_values?
88
+ return @relation.send(:has_join_values?) if @relation.respond_to?(:has_join_values?, true)
89
+ return true if @relation.joins_values.any?
90
+ return true if @relation.respond_to?(:left_outer_joins_values) and @relation.left_outer_joins_values.any?
91
+ return false
92
+ end
93
+
94
+ def arel_attribute(name)
95
+ return @relation.arel_attribute(name) if @relation.respond_to?(:arel_attribute)
96
+ name = klass.attribute_alias(name) if klass.respond_to?(:attribute_alias?) && klass.attribute_alias?(name) # attribute_alias? is not defined in Rails 3.
97
+ return @relation.arel_table[name]
98
+ end
99
+
100
+ def to_sql_and_binds(connection, arel_or_sql_string)
101
+ return connection.send(:to_sql_and_binds, arel_or_sql_string, []) if connection.respond_to?(:to_sql_and_binds, true)
102
+ return [arel_or_sql_string.dup.freeze, []] if !arel_or_sql_string.respond_to?(:ast)
103
+
104
+ sql = accept(connection, arel_or_sql_string.ast)
105
+ return [sql.freeze, bind_values] if sql.is_a?(String)
106
+ return [sql.compile(bind_values, connection), bind_values]
107
+ end
108
+
109
+ def accept(connection, ast)
110
+ return connection.visitor.accept(ast) if not connection.respond_to?(:collector) # For Rails 3
111
+ return connection.visitor.accept(ast, connection.collector)
112
+ end
113
+
114
+ def type_casted_binds(connection, binds)
115
+ return connection.type_casted_binds(binds) if connection.respond_to?(:type_casted_binds)
116
+ return binds.map{|column, value| connection.type_cast(value, column) } if binds.first.is_a?(Array)
117
+ return binds.map{|attr| connection.type_cast(attr.value_for_database) }
118
+ end
119
+
120
+ def join_to_update(connection, stmt, key)
121
+ return connection.join_to_update(stmt, @relation.arel) if connection.method(:join_to_update).arity == 2
122
+ return connection.join_to_update(stmt, @relation.arel, key)
123
+ end
124
+
125
+ def bind_values
126
+ return @relation.bound_attributes if @relation.respond_to?(:bound_attributes) # For Rails 5.1, 5.2
127
+ return @relation.bind_values # For Rails 4.2
128
+ end
129
+ end
130
+ end