oort 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59358b08ac7c205469cd32c017b73ab36846c520e6d066e67c577bfd4daaacad
4
- data.tar.gz: 1c8ab9a97f89c45b7f1630198b2d810ceb6a877d7adbfd0641c92241e5aca425
3
+ metadata.gz: 9d13ddbadfac1d2c1ae4f77c6c81ebac6339ddfdb14b3e41a40da8ba93e62ae2
4
+ data.tar.gz: fae5d70f39bc682f322c16f6e969b64e01794f9fdd87ed50264fb44ce3beb54a
5
5
  SHA512:
6
- metadata.gz: 819c6dce941d799a7497e88fe05bc104391e5bc0ce13e3f4ed0a96e388a1ccb3940f1e46de139a02fc742dc2fc26fa2b9643d5ce30995ae242fb2bf5132c4e44
7
- data.tar.gz: 7763c120b0311b667159bf1adc48c464a8b43d3a770ac78af8bc9fcba7e616bbce11eef5564859c149b3ea09656e75e29bc2183f35d085fc6942d78a83b5fc58
6
+ metadata.gz: aa7621267ccc7d99735bd5548eb7dbdcce7c4b3e280540dd393d4cd45707b0b671f9213eb55c01023c42a710afed5f31163213eead118b0db35d22365cc9fac1
7
+ data.tar.gz: e0e6d7d5a3d2752ac7747439f28256aceb7183e773418836c05896b3f8ade18dbed010b41a54f05a08360f76c8039ac23c87e5900444867491e7405582552bd6
data/Gemfile.lock CHANGED
@@ -69,6 +69,7 @@ GEM
69
69
 
70
70
  PLATFORMS
71
71
  x86_64-darwin-20
72
+ x86_64-darwin-22
72
73
 
73
74
  DEPENDENCIES
74
75
  activerecord (~> 7.0, >= 7.0.1)
data/README.md CHANGED
@@ -17,7 +17,7 @@ Let's begin with a basic schema involving a User and a Post. You can substitute
17
17
 
18
18
  Firstly, you will also need the following migration to users (postgresql only for now):
19
19
 
20
- ```
20
+ ```RUBY
21
21
  def change
22
22
  add_column(:users, :posts_ordering, :integer, array: true, default: [], using: 'ARRAY[benefit_type]::INTEGER[]')
23
23
 
@@ -31,7 +31,7 @@ This will store the ids of posts in an array on the user, and will only accept i
31
31
 
32
32
  Include the `Oort::Ordered` module in the parent object:
33
33
 
34
- ```
34
+ ```RUBY
35
35
  class User < ActiveRecord::Base
36
36
  include Oort::Ordered
37
37
  handles_ordering_of :posts
@@ -52,7 +52,7 @@ This inclusion adds `update_posts_ordering` to the user model and `insert_at` to
52
52
 
53
53
  The following callbacks are also added to post:
54
54
 
55
- ```
55
+ ```RUBY
56
56
  after_create_commit :insert_at
57
57
  after_destroy :remove_from_reorderable
58
58
  ```
@@ -66,7 +66,7 @@ To remove a post, simply call `post.remove_from_reorderable`
66
66
  ### Scope
67
67
  The `ordered_with` scope is also added to the post model. This allows a `user` object to have the following query:
68
68
 
69
- ```
69
+ ```RUBY
70
70
  user.posts.ordered_with(user.posts_ordering)
71
71
 
72
72
  # or
@@ -74,6 +74,21 @@ user.posts.ordered_with(user.posts_ordering)
74
74
  Post.where(user_id: user.id).ordered_with(user.posts_ordering)
75
75
  ```
76
76
 
77
+ ### Customization
78
+
79
+ `handles_ordering_of` can allow for new records to be the top of the order or the bottom of the order. By default it will push any new records to the top of the order, but you can specify the bottom of the order like so:
80
+
81
+ ```ruby
82
+
83
+ class User < ActiveRecord::Base
84
+ include Oort::Ordered
85
+ handles_ordering_of :posts, default: :bottom
86
+
87
+ has_many :posts
88
+ end
89
+
90
+ ```
91
+
77
92
 
78
93
  ## Installation
79
94
 
@@ -2,22 +2,24 @@
2
2
 
3
3
  module Oort
4
4
  class Callbacks
5
- def self.call(association_class:, remove_from_method_name:, insert_method_name:, instance_name:)
5
+ def self.call(association_class:, remove_from_method_name:, insert_method_name:, instance_name:, default:)
6
6
  new(
7
7
  association_class: association_class,
8
8
  remove_from_method_name: remove_from_method_name,
9
9
  insert_method_name: insert_method_name,
10
- instance_name: instance_name
10
+ instance_name: instance_name,
11
+ default: default
11
12
  ).call
12
13
  end
13
14
 
14
- attr_reader :association_class, :remove_from_method_name, :insert_method_name, :instance_name
15
+ attr_reader :association_class, :remove_from_method_name, :insert_method_name, :instance_name, :default
15
16
 
16
- def initialize(association_class:, remove_from_method_name:, insert_method_name:, instance_name:)
17
+ def initialize(association_class:, remove_from_method_name:, insert_method_name:, instance_name:, default:)
17
18
  @association_class = association_class
18
19
  @remove_from_method_name = remove_from_method_name
19
20
  @insert_method_name = insert_method_name
20
21
  @instance_name = instance_name
22
+ @default = default
21
23
  end
22
24
 
23
25
  def call
@@ -29,7 +31,7 @@ module Oort
29
31
 
30
32
  def add_callbacks
31
33
  association_class.class_eval do
32
- after_create_commit :insert_at
34
+ after_create_commit :initial_insert_at
33
35
  after_destroy :remove_from_reorderable
34
36
  end
35
37
  end
@@ -45,9 +47,13 @@ module Oort
45
47
  # public_send(:user).public_send(:remove_from_posts_ordering, id)
46
48
  # end
47
49
  <<-RUBY, __FILE__, __LINE__ + 1
48
- def insert_at(position = 0)
50
+ def insert_at(position = 0, initial: nil)
49
51
  public_send(#{instance_name.inspect})
50
- .public_send(#{insert_method_name.inspect}, insert: id, at: position)
52
+ .public_send(#{insert_method_name.inspect}, insert: id, at: position, initial: initial)
53
+ end
54
+
55
+ def initial_insert_at
56
+ insert_at(initial: #{default.inspect})
51
57
  end
52
58
 
53
59
  def remove_from_reorderable
data/lib/oort/inserts.rb CHANGED
@@ -26,13 +26,22 @@ module Oort
26
26
  # end
27
27
  # end
28
28
  <<-RUBY, __FILE__, __LINE__ + 1
29
- def #{insert_method_name}(insert:, at: 0)
29
+ def #{insert_method_name}(insert:, at: 0, initial: nil)
30
30
  with_lock do
31
31
  current_values = public_send(#{stored_in.inspect})
32
- current_index = current_values.find_index(insert)
33
- insertable = current_index.blank? ? insert : current_values.delete_at(current_index)
34
- current_values.insert(at, insertable)
35
- update(#{stored_in.inspect} => current_values)
32
+
33
+ if initial == :top
34
+ current_values.unshift(insert)
35
+ save
36
+ elsif initial == :bottom
37
+ current_values << insert
38
+ save
39
+ else
40
+ current_index = current_values.find_index(insert)
41
+ insertable = current_index.blank? ? insert : current_values.delete_at(current_index)
42
+ current_values.insert(at, insertable)
43
+ update(#{stored_in.inspect} => current_values)
44
+ end
36
45
  end
37
46
  end
38
47
  RUBY
data/lib/oort/ordered.rb CHANGED
@@ -14,20 +14,23 @@ module Oort
14
14
  end
15
15
 
16
16
  module ClassMethods
17
- def handles_ordering_of(association)
17
+ def handles_ordering_of(association, default: :top)
18
18
  args = {
19
19
  stored_in: :"#{association}_ordering",
20
20
  insert_method_name: :"update_#{association}_ordering",
21
21
  remove_from_method_name: :"remove_from_#{association}_ordering",
22
22
  association_class: association.to_s.classify.constantize,
23
23
  instance_name: :"#{name.downcase}",
24
- class_name: name.classify.constantize
24
+ class_name: name.classify.constantize,
25
+ default:
25
26
  }
26
27
 
27
28
  Inserts.call(**args.slice(:stored_in, :insert_method_name, :class_name))
28
29
  Removes.call(**args.slice(:stored_in, :remove_from_method_name, :class_name))
29
30
  Scopes.call(**args.slice(:association_class))
30
- Callbacks.call(**args.slice(:association_class, :remove_from_method_name, :insert_method_name, :instance_name))
31
+ Callbacks.call(
32
+ **args.slice(:association_class, :remove_from_method_name, :insert_method_name, :instance_name, :default)
33
+ )
31
34
  end
32
35
  end
33
36
  end
data/lib/oort/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Oort
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
data/oort.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/oort/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "oort"
7
+ spec.version = Oort::VERSION
8
+ spec.authors = ["tobyond"]
9
+ spec.homepage = "https://github.com/tobyond/oort"
10
+
11
+ spec.summary = "Oort to sort, order"
12
+ spec.description = "Rails sorting and ordering without deadlocks"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.2.0"
15
+
16
+ spec.metadata["source_code_uri"] = "https://github.com/tobyond/oort"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(__dir__) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ # spec.add_dependency "rspec"
31
+
32
+ # For more information and examples about making a new gem, check out our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - tobyond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-26 00:00:00.000000000 Z
11
+ date: 2024-03-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rails sorting and ordering without deadlocks
14
14
  email:
@@ -40,6 +40,7 @@ files:
40
40
  - lib/oort/removes.rb
41
41
  - lib/oort/scopes.rb
42
42
  - lib/oort/version.rb
43
+ - oort.gemspec
43
44
  - sig/oort.rbs
44
45
  homepage: https://github.com/tobyond/oort
45
46
  licenses:
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  - !ruby/object:Gem::Version
62
63
  version: '0'
63
64
  requirements: []
64
- rubygems_version: 3.4.10
65
+ rubygems_version: 3.5.3
65
66
  signing_key:
66
67
  specification_version: 4
67
68
  summary: Oort to sort, order