reprioritizable 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: 0a30c30b1642ac35bc5baafb556d21ec1fcc6b6826ca806e342d6d36d9147095
4
+ data.tar.gz: b5f68ee6bcfbca3f73a3b2432293c4b05583d9509cd33b230f34169502d714ff
5
+ SHA512:
6
+ metadata.gz: 15d1468fc51e3b41b07501f6ad628d0175154f5c5148621ef734cdf1d3215e3e75b275dc7e4056954a654c5c9883776b13bed8e841b068820c11337a853c19ec
7
+ data.tar.gz: d684c52dd8579a127040412e8bf93ec898ace9c6609813ede52b32cf638bfc215d96c40d7b8d98a347e9865c73046c93af78c41e93d6ad199d80041198ecc9c8
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 murakami
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.md ADDED
@@ -0,0 +1,28 @@
1
+ # Reprioritizable
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'reprioritizable'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install reprioritizable
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module Reprioritizable
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'reprioritizable' do
4
+ ActiveSupport.on_load :action_view do
5
+ require 'reprioritizable/view_helpers/action_view'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Reprioritizable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,11 @@
1
+ module Reprioritizable
2
+ module ViewHelpers
3
+ module ActionView
4
+ def exclamatize(string, number = 1)
5
+ string + '!' * number
6
+ end
7
+
8
+ ::ActionView::Base.send :include, self
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ require "reprioritizable/version"
2
+ require "reprioritizable/railtie"
3
+
4
+ module Reprioritizable
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def reprioritize!(column:, scope:, instance:, new_priority:)
9
+ instance.restrict_max_priority!(column, new_priority, scope)
10
+ instance.restrict_minimum_priority!(column, new_priority, scope)
11
+
12
+ before_priority = instance.send(column)
13
+ if before_priority > new_priority
14
+ reprioritize(
15
+ scope.where("#{column} >= ?", new_priority).where("#{column} < ?", before_priority)
16
+ ) do |record|
17
+ record.increase_priority(column)
18
+ end
19
+ elsif before_priority < new_priority
20
+ reprioritize(
21
+ scope.where("#{column} <= ?", new_priority).where("#{column} > ?", before_priority)
22
+ ) do |record|
23
+ record.decrease_priority(column)
24
+ end
25
+ end
26
+
27
+ instance.update!("#{column}": new_priority)
28
+ end
29
+
30
+ def reprioritize(scope, &_block)
31
+ attributes = scope.map do |prioritable|
32
+ yield(prioritable)
33
+ prioritable.updated_at = Time.zone.now
34
+ prioritable.attributes
35
+ end
36
+ scope.klass.upsert_all(attributes) if attributes.any?
37
+ end
38
+ end
39
+
40
+ def increase_priority(column)
41
+ eval("self.#{column} += 1")
42
+ end
43
+
44
+ def decrease_priority(column)
45
+ eval("self.#{column} -= 1")
46
+ end
47
+
48
+ def restrict_max_priority!(column, new_priority, scope)
49
+ max_size = scope.maximum(column)
50
+ errors.add(:base, "Please register with a value of #{max_size} or less.") if max_size < new_priority
51
+
52
+ raise ActiveRecord::RecordInvalid, self if errors.any?
53
+ end
54
+
55
+ def restrict_minimum_priority!(column, new_priority, scope)
56
+ minimum_size = scope.minimum(column)
57
+ errors.add(:base, "Please register with a value of #{minimum_size} or more.") if minimum_size > new_priority
58
+
59
+ raise ActiveRecord::RecordInvalid, self if errors.any?
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :reprioritizable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reprioritizable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - murakami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.4.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.4.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: byebug
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ description: Description of Reprioritizable.
62
+ email:
63
+ - ek.h1.hy@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - MIT-LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - lib/reprioritizable.rb
72
+ - lib/reprioritizable/railtie.rb
73
+ - lib/reprioritizable/version.rb
74
+ - lib/reprioritizable/view_helpers/action_view.rb
75
+ - lib/tasks/reprioritizable_tasks.rake
76
+ homepage: http://example.com
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.1.4
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Summary of Reprioritizable.
99
+ test_files: []