prioritize 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: 396834d7951d62b0cc6d2b49704e85ca61b360cd673d19173db5ef7766f26f8f
4
+ data.tar.gz: 16391da63996a8867e26db9336e6617f1669df344b3f91d111a63f95174f8c6d
5
+ SHA512:
6
+ metadata.gz: 857fec8198e0c39d35b529a9d5b42ab3109ca7c0f902aded72b8a5b6579e0a31802d9e1e18274c874ec02272596f866247c04fb6b8341b690280dfb938b37d57
7
+ data.tar.gz: 8bc036e52af40fec4c6e75aada313bdbc5ca26a997bf358bf9da558aa98a45245880ea7d6176fe77efa217b2c53bca223545a4c516f1f3e554da5626a1856c71
data/.byebug_history ADDED
@@ -0,0 +1,24 @@
1
+ c
2
+ self.send :include, Prioritize
3
+ self
4
+ c
5
+ Post.methods.include? :prioritize_col
6
+ Post
7
+ ar_class
8
+ c
9
+ ar_class
10
+ c
11
+ ar_class
12
+ c
13
+ ar_class
14
+ c
15
+ class_methods.class
16
+ class_methods
17
+ class_methods.include? :prioritize_col
18
+ c
19
+ Post.methods.include? :prioritize_col
20
+ ActiveRecord::Base.methods.include? :prioritize_col
21
+ c
22
+ ActiveRecord::Base.methods.include? :prioritize_col
23
+ ActiveRecord::Base.methods.sort
24
+ c
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/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in prioritize.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prioritize (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (5.2.6)
10
+ activesupport (= 5.2.6)
11
+ activerecord (5.2.6)
12
+ activemodel (= 5.2.6)
13
+ activesupport (= 5.2.6)
14
+ arel (>= 9.0)
15
+ activesupport (5.2.6)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ arel (9.0.0)
21
+ awesome_print (1.9.2)
22
+ byebug (11.1.3)
23
+ concurrent-ruby (1.1.9)
24
+ diff-lcs (1.4.4)
25
+ i18n (1.8.10)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.14.4)
28
+ rake (12.3.3)
29
+ rspec (3.10.0)
30
+ rspec-core (~> 3.10.0)
31
+ rspec-expectations (~> 3.10.0)
32
+ rspec-mocks (~> 3.10.0)
33
+ rspec-core (3.10.1)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-expectations (3.10.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.10.0)
38
+ rspec-mocks (3.10.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.10.0)
41
+ rspec-support (3.10.2)
42
+ thread_safe (0.3.6)
43
+ tzinfo (1.2.9)
44
+ thread_safe (~> 0.1)
45
+
46
+ PLATFORMS
47
+ x86_64-linux
48
+
49
+ DEPENDENCIES
50
+ activerecord (~> 5.0, >= 5.0.0.1)
51
+ awesome_print
52
+ byebug
53
+ prioritize!
54
+ rake (~> 12.0)
55
+ rspec (~> 3.0)
56
+
57
+ BUNDLED WITH
58
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 iadfeshchm
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,54 @@
1
+ # Prioritize
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be
4
+ able to package up your Ruby library into a gem. Put your Ruby code in the file
5
+ `lib/prioritize`. To experiment with that code, run `bin/console` for an
6
+ interactive prompt.
7
+
8
+ TODO: Delete this and the text above, and describe your gem
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'prioritize'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install prioritize
25
+
26
+ ## Usage
27
+
28
+ TODO: Write usage instructions here
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
33
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
34
+ prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To
37
+ release a new version, update the version number in `version.rb`, and then run
38
+ `bundle exec rake release`, which will create a git tag for the version, push
39
+ git commits and tags, and push the `.gem` file to [rubygems.org]
40
+ (https://rubygems.org).
41
+
42
+ ```sh
43
+ bundle
44
+ rake spec
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/prioritize.
50
+
51
+
52
+ ## License
53
+
54
+ 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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prioritize"
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,3 @@
1
+ module Prioritize
2
+ VERSION = "0.1.0"
3
+ end
data/lib/prioritize.rb ADDED
@@ -0,0 +1,89 @@
1
+ require "prioritize/version"
2
+
3
+ module Prioritize
4
+ class Error < StandardError; end
5
+
6
+ def self.included base
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def prioritize_column
12
+ self.include PriorityAfter
13
+ end
14
+ end
15
+
16
+ module PriorityAfter
17
+ def self.included base
18
+ base.extend ClassMethods
19
+ end
20
+ def priority_after before_id
21
+ self.class.priority_after before_id, self.id
22
+ end
23
+ module ClassMethods
24
+ def priority_after before_id, moved_id
25
+ connection.exec_query(
26
+ <<-SQL,
27
+ UPDATE #{table_name} o
28
+ SET #{priority_column} = ordered.rn
29
+ FROM (
30
+ SELECT *, ROW_NUMBER() OVER() AS rn
31
+ FROM (
32
+ (
33
+ SELECT o.*
34
+ FROM #{table_name} o
35
+ LEFT JOIN #{table_name} ai ON ai.id = $1
36
+ WHERE
37
+ (o.#{priority_column} < ai.#{priority_column} OR ai.#{priority_column} IS NULL) AND
38
+ o.id <> $2
39
+ ORDER BY o.#{priority_column} ASC
40
+ )
41
+ UNION ALL
42
+ (
43
+ SELECT o.*
44
+ FROM #{table_name} o
45
+ WHERE o.id = $2
46
+ )
47
+ UNION ALL
48
+ (
49
+ SELECT o.*
50
+ FROM #{table_name} o
51
+ LEFT JOIN #{table_name} ai ON ai.id = $1
52
+ WHERE
53
+ o.#{priority_column} >= ai.#{priority_column} AND
54
+ o.id <> $2
55
+ ORDER BY o.#{priority_column} ASC
56
+ )
57
+ ) numbered
58
+ ) ordered
59
+ WHERE o.id = ordered.id
60
+ SQL
61
+ 'priority_after',
62
+ [[nil,before_id], [nil,moved_id]]
63
+ )
64
+ end
65
+ attr_accessor :priority_column
66
+ def priority_column
67
+ @priority_column.to_s
68
+ end
69
+ def self.extended base
70
+ base.class_eval do
71
+ @priority_column = :priority
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ ActiveRecord::Base.send(:include, Prioritize)
79
+
80
+
81
+ # byebug
82
+
83
+ # Кто после кого
84
+ # PostSection.priority_after 1, 2
85
+ # post_section.priority_after 2
86
+ #
87
+ #
88
+ # self.implicit_order_column = "updated_at"
89
+ #
@@ -0,0 +1,33 @@
1
+ require_relative 'lib/prioritize/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "prioritize"
5
+ spec.version = Prioritize::VERSION
6
+ spec.authors = ["Zlatov"]
7
+ spec.email = ["zlatov@ya.ru"]
8
+
9
+ spec.summary = "Prioritize a ActiveRecord list"
10
+ spec.description = "Adds a method that allows you to update the model column used for sorting."
11
+ spec.homepage = "https://github.com/Zlatov/prioritize"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/Zlatov/prioritize.git"
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "awesome_print"
31
+ spec.add_development_dependency "byebug"
32
+ spec.add_development_dependency "activerecord", '~> 5.0', '>= 5.0.0.1'
33
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prioritize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zlatov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 5.0.0.1
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '5.0'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 5.0.0.1
61
+ description: Adds a method that allows you to update the model column used for sorting.
62
+ email:
63
+ - zlatov@ya.ru
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".byebug_history"
69
+ - ".gitignore"
70
+ - ".rspec"
71
+ - ".ruby-version"
72
+ - ".travis.yml"
73
+ - Gemfile
74
+ - Gemfile.lock
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - bin/console
79
+ - bin/setup
80
+ - lib/prioritize.rb
81
+ - lib/prioritize/version.rb
82
+ - prioritize.gemspec
83
+ homepage: https://github.com/Zlatov/prioritize
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ allowed_push_host: https://rubygems.org
88
+ homepage_uri: https://github.com/Zlatov/prioritize
89
+ source_code_uri: https://github.com/Zlatov/prioritize.git
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 2.3.0
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.2.22
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Prioritize a ActiveRecord list
109
+ test_files: []