has_counter_on 0.1.1

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: 3215f052de0fbce0492b83eb0f152cff88f12bff942da5960d3549a363c75c7a
4
+ data.tar.gz: 6ef38b32105b621cd87326cf3182ec6fdb8c568f34aa71ce4f6862ae57560a6b
5
+ SHA512:
6
+ metadata.gz: 495a2171178bffbfccf26e7d785befbc7928c9507abad6b9c35c4fba42a5031b483c24cf4eb685419af2c3344d39ceabf9771b3ebac33e04e800462689df23d8
7
+ data.tar.gz: f1945ca749f914867bc5797eb241cce4690d1fe989a06a75cda81ea0143b2eee342ee7ebff47acbc67e3a3ceb6071fe3f18f0d5f3d47ff844910d8842e8a7389
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in has_counter_on.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Injung Chung
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,40 @@
1
+ # HasCounterOn
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/has_counter_on`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'has_counter_on'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install has_counter_on
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/has_counter_on.
36
+
37
+
38
+ ## License
39
+
40
+ 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
+ task :default => :spec
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ load 'lib/tasks/has_counter_on_task.rake'
@@ -0,0 +1,14 @@
1
+ AR_VERSION = ActiveRecord.gem_version.version.to_f
2
+
3
+ class CreateCounters < ActiveRecord::Migration[AR_VERSION]
4
+ def change
5
+ create_table :counters do |t|
6
+ t.string :countable_type, limit: 64
7
+ t.integer :countable_id, unsigned: true
8
+ t.string :countable_name
9
+ t.integer :value
10
+ end
11
+
12
+ add_index :counters, [:countable_id, :countable_type, :countable_name], name: :index_counters_on_countable_id_type_name
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'lib/has_counter_on/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'has_counter_on'
5
+ spec.version = HasCounterOn::VERSION
6
+ spec.authors = ['Injung Chung']
7
+ spec.email = ['mu29@yeoubi.net']
8
+
9
+ spec.summary = 'ActiveRecord counter_cache has been reborn, with ability to specify conditions.'
10
+ spec.homepage = 'https://github.com/mu29/has_counter_on'
11
+ spec.license = 'MIT'
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+
14
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activerecord', '~> 3.2'
20
+ end
@@ -0,0 +1,42 @@
1
+ module HasCounterOn
2
+ module Countable
3
+ def has_counter_on(association_name, counter_name = nil, conditions = {})
4
+ association = reflect_on_association(association_name)
5
+ target = association.klass
6
+
7
+ unless target.ancestors.include? HasCounterOn::CounterMethods
8
+ target.include HasCounterOn::CounterMethods
9
+
10
+ target.after_create :has_counter_on_after_create
11
+ target.before_update :has_counter_on_before_update
12
+ target.before_destroy :has_counter_on_before_destroy
13
+
14
+ target.cattr_accessor :has_counter_on_options
15
+ target.has_counter_on_options = [];
16
+ end
17
+
18
+ counter_name ||= "#{association.plural_name}_count".to_sym
19
+
20
+ unless respond_to? counter_name
21
+ has_many :counters, as: :countable, dependent: :destroy, class_name: '::HasCounterOn::Counter'
22
+
23
+ define_method counter_name do
24
+ counters.find_by(countable_name: counter_name)&.value or 0
25
+ end
26
+ end
27
+
28
+ this = association.inverse_of
29
+ target.has_counter_on_options << [
30
+ -> (id:) {
31
+ HasCounterOn::Counter.find_or_create_by(
32
+ countable_type: this.klass,
33
+ countable_id: id,
34
+ countable_name: counter_name,
35
+ )
36
+ },
37
+ this.foreign_key,
38
+ conditions,
39
+ ]
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module HasCounterOn
2
+ class Counter < ActiveRecord::Base
3
+ belongs_to :countable, polymorphic: true
4
+ end
5
+ end
@@ -0,0 +1,86 @@
1
+ module HasCounterOn
2
+ module CounterMethods
3
+ private
4
+
5
+ def has_counter_on_after_create
6
+ self.has_counter_on_options.each do |get_counter, foreign_key, conditions|
7
+ next unless satisfy_present?(conditions: conditions)
8
+
9
+ association_id = send(foreign_key)
10
+ counter = get_counter.call(id: association_id)
11
+
12
+ counter.increment!(:value)
13
+ end
14
+ end
15
+
16
+ def has_counter_on_before_update
17
+ self.has_counter_on_options.each do |get_counter, foreign_key, conditions|
18
+ if satisfy_present?(conditions: conditions)
19
+ before, after = send("#{foreign_key}_change")
20
+
21
+ if before != after
22
+ get_counter.call(id: before).decrement!(:value)
23
+ get_counter.call(id: after).increment!(:value)
24
+
25
+ next
26
+ end
27
+ end
28
+
29
+ before = satisfy_before?(conditions: conditions)
30
+ after = satisfy_after?(conditions: conditions)
31
+
32
+ next if before == after
33
+
34
+ association_id = send(foreign_key)
35
+ counter = get_counter.call(id: association_id)
36
+
37
+ if before
38
+ counter.decrement!(:value)
39
+ elsif after
40
+ counter.increment!(:value)
41
+ end
42
+ end
43
+ end
44
+
45
+ def has_counter_on_before_destroy
46
+ self.has_counter_on_options.each do |klass, foreign_key, counter_name, conditions|
47
+ next unless satisfy_present?(conditions: conditions)
48
+
49
+ association_id = send(foreign_key)
50
+ counter = get_counter.call(id: association_id)
51
+
52
+ counter.decrement!(:value)
53
+ end
54
+ end
55
+
56
+ def satisfy_before?(conditions:)
57
+ conditions.each.all? do |attr, value|
58
+ if value.is_a? Proc
59
+ send("#{attr}_changed?") && value.call(attribute_was(attr))
60
+ else
61
+ send("#{attr}_changed?", from: value)
62
+ end
63
+ end
64
+ end
65
+
66
+ def satisfy_present?(conditions:)
67
+ conditions.each.all? do |attr, value|
68
+ if value.is_a? Proc
69
+ value.call(send(attr))
70
+ else
71
+ send(attr) == value
72
+ end
73
+ end
74
+ end
75
+
76
+ def satisfy_after?(conditions:)
77
+ conditions.each.all? do |attr, value|
78
+ if value.is_a? Proc
79
+ send("#{attr}_changed?") && value.call(send(attr))
80
+ else
81
+ send("#{attr}_changed?", to: value)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module HasCounterOn
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_record'
2
+ require 'active_record/version'
3
+ require 'active_support/core_ext/module'
4
+
5
+ module HasCounterOn
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :Countable
9
+ autoload :CounterMethods
10
+ autoload :Counter
11
+ autoload :VERSION
12
+ end
13
+
14
+ ActiveSupport.on_load(:active_record) do
15
+ extend HasCounterOn::Countable
16
+ end
@@ -0,0 +1,8 @@
1
+ namespace :has_counter_on do
2
+ desc 'Install has_counter_on dependencies'
3
+ task :install do
4
+ ActiveRecord::Base.establish_connection
5
+ ActiveRecord::Migrator.migrate('db/migrate/')
6
+ Rake::Task['db:schema'].invoke
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_counter_on
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Injung Chung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-29 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: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description:
28
+ email:
29
+ - mu29@yeoubi.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - db/migrate/create_counters_migration.rb
40
+ - has_counter_on.gemspec
41
+ - lib/has_counter_on.rb
42
+ - lib/has_counter_on/countable.rb
43
+ - lib/has_counter_on/counter.rb
44
+ - lib/has_counter_on/counter_methods.rb
45
+ - lib/has_counter_on/version.rb
46
+ - lib/tasks/has_counter_on_task.rake
47
+ homepage: https://github.com/mu29/has_counter_on
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.3.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.7.6
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: ActiveRecord counter_cache has been reborn, with ability to specify conditions.
71
+ test_files: []