activerecord-update_counters_with_values 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 900297351f21b026aeb59f2e85f32e907baabd27328dcde5339908dfa24380c8
4
+ data.tar.gz: 35c110cf4cbfcffd2420013c10e2f560239b9f288a82d3989c678e749d63bf3b
5
+ SHA512:
6
+ metadata.gz: 512b2de5bc097298043b466039c4e933a97b3415dd0800b9aae347a9ddaf072fb075dab35a4d6f79947fbbdff475e48106fcdd26fadf9dfb4e1a0f856d84df0b
7
+ data.tar.gz: c2e8d03609c240e96b2c8753c2c78e2c2027e2ddcdcf71d61a1f857f2a36106653f099797c8dab4208607c92ceec0ed7dfed0c6ef180f67369867d7d14489f81
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.irbrc ADDED
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'pry'
3
+ Pry.start
4
+ exit
5
+ rescue LoadError
6
+ puts "Pry not found, using 'irb' instead. Try\n gem install pry"
7
+ end
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'rails', '>= 4.2'
6
+
7
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,148 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-update_counters_with_values (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (5.2.0)
10
+ actionpack (= 5.2.0)
11
+ nio4r (~> 2.0)
12
+ websocket-driver (>= 0.6.1)
13
+ actionmailer (5.2.0)
14
+ actionpack (= 5.2.0)
15
+ actionview (= 5.2.0)
16
+ activejob (= 5.2.0)
17
+ mail (~> 2.5, >= 2.5.4)
18
+ rails-dom-testing (~> 2.0)
19
+ actionpack (5.2.0)
20
+ actionview (= 5.2.0)
21
+ activesupport (= 5.2.0)
22
+ rack (~> 2.0)
23
+ rack-test (>= 0.6.3)
24
+ rails-dom-testing (~> 2.0)
25
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
26
+ actionview (5.2.0)
27
+ activesupport (= 5.2.0)
28
+ builder (~> 3.1)
29
+ erubi (~> 1.4)
30
+ rails-dom-testing (~> 2.0)
31
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
32
+ activejob (5.2.0)
33
+ activesupport (= 5.2.0)
34
+ globalid (>= 0.3.6)
35
+ activemodel (5.2.0)
36
+ activesupport (= 5.2.0)
37
+ activerecord (5.2.0)
38
+ activemodel (= 5.2.0)
39
+ activesupport (= 5.2.0)
40
+ arel (>= 9.0)
41
+ activestorage (5.2.0)
42
+ actionpack (= 5.2.0)
43
+ activerecord (= 5.2.0)
44
+ marcel (~> 0.3.1)
45
+ activesupport (5.2.0)
46
+ concurrent-ruby (~> 1.0, >= 1.0.2)
47
+ i18n (>= 0.7, < 2)
48
+ minitest (~> 5.1)
49
+ tzinfo (~> 1.1)
50
+ arel (9.0.0)
51
+ builder (3.2.3)
52
+ coderay (1.1.2)
53
+ concurrent-ruby (1.0.5)
54
+ crass (1.0.4)
55
+ diff-lcs (1.3)
56
+ erubi (1.7.1)
57
+ globalid (0.4.1)
58
+ activesupport (>= 4.2.0)
59
+ i18n (1.0.1)
60
+ concurrent-ruby (~> 1.0)
61
+ loofah (2.2.2)
62
+ crass (~> 1.0.2)
63
+ nokogiri (>= 1.5.9)
64
+ mail (2.7.0)
65
+ mini_mime (>= 0.1.1)
66
+ marcel (0.3.2)
67
+ mimemagic (~> 0.3.2)
68
+ method_source (0.9.0)
69
+ mimemagic (0.3.2)
70
+ mini_mime (1.0.0)
71
+ mini_portile2 (2.3.0)
72
+ minitest (5.11.3)
73
+ nio4r (2.3.1)
74
+ nokogiri (1.8.4)
75
+ mini_portile2 (~> 2.3.0)
76
+ pry (0.11.3)
77
+ coderay (~> 1.1.0)
78
+ method_source (~> 0.9.0)
79
+ rack (2.0.5)
80
+ rack-test (1.0.0)
81
+ rack (>= 1.0, < 3)
82
+ rails (5.2.0)
83
+ actioncable (= 5.2.0)
84
+ actionmailer (= 5.2.0)
85
+ actionpack (= 5.2.0)
86
+ actionview (= 5.2.0)
87
+ activejob (= 5.2.0)
88
+ activemodel (= 5.2.0)
89
+ activerecord (= 5.2.0)
90
+ activestorage (= 5.2.0)
91
+ activesupport (= 5.2.0)
92
+ bundler (>= 1.3.0)
93
+ railties (= 5.2.0)
94
+ sprockets-rails (>= 2.0.0)
95
+ rails-dom-testing (2.0.3)
96
+ activesupport (>= 4.2.0)
97
+ nokogiri (>= 1.6)
98
+ rails-html-sanitizer (1.0.4)
99
+ loofah (~> 2.2, >= 2.2.2)
100
+ railties (5.2.0)
101
+ actionpack (= 5.2.0)
102
+ activesupport (= 5.2.0)
103
+ method_source
104
+ rake (>= 0.8.7)
105
+ thor (>= 0.18.1, < 2.0)
106
+ rake (10.5.0)
107
+ rspec (3.7.0)
108
+ rspec-core (~> 3.7.0)
109
+ rspec-expectations (~> 3.7.0)
110
+ rspec-mocks (~> 3.7.0)
111
+ rspec-core (3.7.1)
112
+ rspec-support (~> 3.7.0)
113
+ rspec-expectations (3.7.0)
114
+ diff-lcs (>= 1.2.0, < 2.0)
115
+ rspec-support (~> 3.7.0)
116
+ rspec-mocks (3.7.0)
117
+ diff-lcs (>= 1.2.0, < 2.0)
118
+ rspec-support (~> 3.7.0)
119
+ rspec-support (3.7.1)
120
+ sprockets (3.7.2)
121
+ concurrent-ruby (~> 1.0)
122
+ rack (> 1, < 3)
123
+ sprockets-rails (3.2.1)
124
+ actionpack (>= 4.0)
125
+ activesupport (>= 4.0)
126
+ sprockets (>= 3.0.0)
127
+ thor (0.20.0)
128
+ thread_safe (0.3.6)
129
+ tzinfo (1.2.5)
130
+ thread_safe (~> 0.1)
131
+ websocket-driver (0.7.0)
132
+ websocket-extensions (>= 0.1.0)
133
+ websocket-extensions (0.1.3)
134
+
135
+ PLATFORMS
136
+ ruby
137
+
138
+ DEPENDENCIES
139
+ activerecord (>= 4.2)
140
+ activerecord-update_counters_with_values!
141
+ bundler (~> 1.16)
142
+ pry
143
+ rails (>= 4.2)
144
+ rake (~> 10.0)
145
+ rspec (~> 3.0)
146
+
147
+ BUNDLED WITH
148
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Igor Alexandrov
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Activerecord::Increment::And::Return
2
+
3
+ Update ActiveRecord counter and update instance value
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-update_counters_with_values', require: 'active_record/update_counters_with_values'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-update_counters_with_values
20
+
21
+ ## Usage
22
+
23
+ ### Class methods
24
+
25
+ ```ruby
26
+ MyModel.update_counters_and_return_values(1, comments_count: 1)
27
+ MyModel.update_counters_and_return_values(1, comments_count: 1, another_count: 2)
28
+ MyModel.update_counters_and_return_values([1,2], comments_count: 1, another_count: 2)
29
+
30
+ MyModel.increment_counter_and_return_value(1, :comments_count)
31
+ MyModel.decrement_counter_and_return_value(1, :comments_count)
32
+ ```
33
+
34
+ ### Instance methods
35
+
36
+ ```ruby
37
+ document = Document.first
38
+ document.update_counters_with_values(comments_count: 1)
39
+ document.update_counters_with_values(comments_count: 1, another_count: 2)
40
+
41
+ document.increment_counter_with_value(:comments_count)
42
+ document.decrement_counter_with_value(:comments_count)
43
+ ```
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ 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).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/igor-alexandrov/activerecord-increment-and-return.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "active_record/update_counters_with_values/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "activerecord-update_counters_with_values"
9
+ spec.version = ActiveRecord::UpdateCountersWithValues::VERSION
10
+ spec.authors = ["Igor Alexandrov"]
11
+ spec.email = ["igor.alexandrov@gmail.com"]
12
+
13
+ spec.summary = %q{Update ActiveRecord counter and update instance value}
14
+ spec.homepage = "https://github.com/jetrockets/activerecord-increment-counter-and-value"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency 'activerecord', '>= 4.2'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+
27
+ spec.add_development_dependency 'pry'
28
+
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/increment/and/return"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require 'active_record/update_counters_with_values/version'
2
+ require 'active_record/update_counters_with_values/class_methods'
3
+
4
+ module ActiveRecord
5
+ module UpdateCountersWithValues
6
+ def self.included(base)
7
+ base.extend ActiveRecord::UpdateCountersWithValues::ClassMethods
8
+ end
9
+
10
+ def update_counters_with_values(counters)
11
+ results = self.class.update_counters_and_return_values(id, counters)[0]
12
+
13
+ counters.each do |key, value|
14
+ write_attribute(key, results[key.to_s])
15
+ clear_attribute_changes(key)
16
+ end
17
+
18
+ self
19
+ end
20
+
21
+ def increment_counter_with_value(counter)
22
+ update_counters_with_values(counter => 1)
23
+ end
24
+
25
+ def decrement_counter_with_value(counter)
26
+ update_counters_with_values(counter => -1)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_record/update_counters_with_values/query_builder'
2
+
3
+ module ActiveRecord
4
+ module UpdateCountersWithValues
5
+ module ClassMethods
6
+ def update_counters_and_return_values(id, counters)
7
+ query = ActiveRecord::UpdateCountersWithValues::QueryBuilder.new(self).call(id, counters)
8
+ connection.execute(query, "#{class_name.to_s} Update Counters").to_a
9
+ end
10
+
11
+ def increment_counter_and_return_value(id, counter)
12
+ update_counters_and_return_values(id, counter => 1)
13
+ end
14
+
15
+ def decrement_counter_and_return_value(id, counter)
16
+ update_counters_and_return_values(id, counter => -1)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ module ActiveRecord
2
+ module UpdateCountersWithValues
3
+ class QueryBuilder
4
+ def initialize(klass)
5
+ @klass = klass
6
+ end
7
+ def call(id, counters)
8
+ updates = build_updates(counters)
9
+ conditions = build_conditions(id)
10
+ returns = build_returns(counters)
11
+
12
+ (<<~SQL)
13
+ UPDATE #{@klass.connection.quote_table_name(@klass.table_name)}
14
+ SET
15
+ #{updates}
16
+ WHERE
17
+ #{conditions}
18
+ RETURNING
19
+ #{returns}
20
+ SQL
21
+ end
22
+
23
+ private
24
+
25
+ def build_updates(counters)
26
+ counters.map do |counter_name, value|
27
+ operator = value < 0 ? '-' : '+'
28
+ quoted_column = @klass.connection.quote_column_name(counter_name)
29
+ "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
30
+ end.join(', ')
31
+ end
32
+
33
+ def build_conditions(id)
34
+ conditions = ["#{@klass.connection.quote_table_name(@klass.table_name)}.#{@klass.connection.quote_column_name(@klass.primary_key)}"]
35
+ if Array.wrap(id).size > 1
36
+ conditions << "IN (#{Array.wrap(id).map{ |id| @klass.connection.quote(id) }.join(', ')})"
37
+ else
38
+ conditions << "= #{@klass.connection.quote(id)}"
39
+ end
40
+ conditions.join(' ')
41
+ end
42
+
43
+ def build_returns(counters)
44
+ returns = [@klass.connection.quote_column_name(@klass.primary_key)]
45
+ returns << counters.map { |counter_name, value| @klass.connection.quote_column_name(counter_name) }
46
+ returns.flatten.join(', ')
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module UpdateCountersWithValues
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-update_counters_with_values
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Alexandrov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-13 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'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - igor.alexandrov@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".irbrc"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - activerecord-update_counters_with_values.gemspec
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/active_record/update_counters_with_values.rb
103
+ - lib/active_record/update_counters_with_values/class_methods.rb
104
+ - lib/active_record/update_counters_with_values/query_builder.rb
105
+ - lib/active_record/update_counters_with_values/version.rb
106
+ homepage: https://github.com/jetrockets/activerecord-increment-counter-and-value
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.7.6
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Update ActiveRecord counter and update instance value
130
+ test_files: []