has_order 0.1.2

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
+ SHA1:
3
+ metadata.gz: e557f1236be3b4b07640fbb1d983e1505ca8ee31
4
+ data.tar.gz: 0b3cd15c4df5733973286f6b7d38754708f3d2f3
5
+ SHA512:
6
+ metadata.gz: b83accc8697a18904838273af96ea7851d16f2b51dbd67b61d202295330d3eb07e427965b16307488a9f02612a3ba29ec59939b8b96083a2a3836d2a5eb85e03
7
+ data.tar.gz: 6bcb95921c60c534e4203d90c3d0b9d6bc1d2a3581d47883bceadcfc7a8fee37c85aa57d1105173a37bff527853b896e7f329dac17c9f610000194e7020c9208
data/.editorconfig ADDED
@@ -0,0 +1,14 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+
7
+ end_of_line = lf
8
+ charset = utf-8
9
+
10
+ trim_trailing_whitespace = true
11
+ insert_final_newline = true
12
+
13
+ [*.md]
14
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ /.bundle
3
+ /Gemfile.lock
4
+ /pkg
5
+ /tmp
6
+ /coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ ## 0.1.2
4
+
5
+ - Added README.md.
6
+ - Updated codestyle.
7
+ - Fixed rspec deprication warnings.
8
+ - Updated development dependencies.
9
+
10
+ ## 0.1.1
11
+
12
+ - Fixed scopes (always using lambdas).
13
+
14
+ ## 0.1.0
15
+
16
+ - Added `#move_to`.
17
+ - Added `#shift_interval` option.
18
+ - Added `#set_default_position?`.
19
+ - Added lambda scopes support.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ [![Gem Version](https://badge.fury.io/rb/has_order.svg)](http://badge.fury.io/rb/has_order)
2
+
3
+ # has_order
4
+
5
+ Provides tree behavior to active_record models.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'has_order'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ class Item < ActiveRecord::Base
21
+ # :scope - optional, proc, symbol or an array of symbols.
22
+ # :position_column - optional, default 'position'.
23
+ # :shift_interval - optional, default 1000.
24
+ has_order
25
+ end
26
+
27
+ foo, bar, baz, qux = Item.create([
28
+ { name: 'foo' },
29
+ { name: 'bar' },
30
+ { name: 'baz' },
31
+ { name: 'qux' }
32
+ ])
33
+
34
+ Item.at(foo.position) # => foo
35
+ baz.higher # => [ qux ]
36
+ baz.and_higher # => [ baz, qux ]
37
+ baz.lower # => [ foo, bar ]
38
+ baz.and_lower # => [ foo, bar, baz ]
39
+
40
+ baz.move_before(bar)
41
+ Item.ordered # => [ foo, baz, bar, qux ]
42
+
43
+ foo.move_after(qux)
44
+ Item.ordered # => [ baz, bar, qux, foo ]
45
+
46
+ baz.move_to(qux)
47
+ Item.ordered # => [ bar, baz, qux, foo ]
48
+ ```
49
+
50
+ ## License
51
+
52
+ Copyright (c) 2014 Kolesnikov Danil
53
+
54
+ MIT License
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining
57
+ a copy of this software and associated documentation files (the
58
+ "Software"), to deal in the Software without restriction, including
59
+ without limitation the rights to use, copy, modify, merge, publish,
60
+ distribute, sublicense, and/or sell copies of the Software, and to
61
+ permit persons to whom the Software is furnished to do so, subject to
62
+ the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be
65
+ included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
68
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
70
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
71
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
72
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
73
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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/has_order.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'has_order/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'has_order'
8
+ spec.version = HasOrder::VERSION
9
+
10
+ spec.authors = ['Kolesnikov Danil']
11
+ spec.email = ['kolesnikovde@gmail.com']
12
+ spec.description = 'Provides list behavior to active_record models.'
13
+ spec.summary = 'Provides list behavior to active_record models.'
14
+ spec.homepage = 'https://github.com/kolesnikovde/has_order'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1'
22
+ spec.add_development_dependency 'rake', '~> 10'
23
+ spec.add_development_dependency 'rspec', '~> 3'
24
+ spec.add_development_dependency 'sqlite3', '~> 1'
25
+ spec.add_development_dependency 'simplecov'
26
+
27
+ spec.add_runtime_dependency 'activerecord', '~> 4'
28
+ spec.add_runtime_dependency 'activesupport', '~> 4'
29
+ end
@@ -0,0 +1,3 @@
1
+ module HasOrder
2
+ VERSION = '0.1.2'
3
+ end
data/lib/has_order.rb ADDED
@@ -0,0 +1,151 @@
1
+ require 'active_record'
2
+ require 'has_order/version'
3
+
4
+ module HasOrder
5
+ # options - :scope - optional, proc, symbol or an array of symbols.
6
+ # :position_column - optional, default 'position'.
7
+ # :shift_interval - optional, default 1000.
8
+ def has_order options = {}
9
+ extend ClassMethods
10
+ include InstanceMethods
11
+
12
+ cattr_accessor :position_column do
13
+ options[:position_column] || :position
14
+ end
15
+
16
+ cattr_accessor :position_shift_interval do
17
+ options[:shift_interval] || 1000
18
+ end
19
+
20
+ before_save :set_default_position, if: :set_default_position?
21
+
22
+ define_list_scope(options[:scope])
23
+ end
24
+
25
+ module ClassMethods
26
+ def at(pos)
27
+ where(position_column => pos)
28
+ end
29
+
30
+ def ordered
31
+ order(arel_table[position_column].asc)
32
+ end
33
+
34
+ def shift!
35
+ col = position_column
36
+ update_all("#{col} = #{col} + #{position_shift_interval}")
37
+ end
38
+
39
+ def next_position
40
+ maximum(position_column).to_i + position_shift_interval
41
+ end
42
+ end
43
+
44
+ module InstanceMethods
45
+ def position
46
+ self[position_column]
47
+ end
48
+
49
+ def position=(pos)
50
+ self[position_column] = pos
51
+ end
52
+
53
+ def lower
54
+ where_position(:lt)
55
+ end
56
+
57
+ def and_lower
58
+ where_position(:lteq)
59
+ end
60
+
61
+ def higher
62
+ where_position(:gt)
63
+ end
64
+
65
+ def and_higher
66
+ where_position(:gteq)
67
+ end
68
+
69
+ def prev
70
+ lower.ordered.last
71
+ end
72
+
73
+ def next
74
+ higher.ordered.first
75
+ end
76
+
77
+ def move_to(pos)
78
+ ActiveRecord::Base.transaction do
79
+ if node = list_scope.at(pos).first
80
+ node.and_higher.shift!
81
+ end
82
+
83
+ self.position = pos
84
+ save!
85
+ end
86
+ end
87
+
88
+ def move_before(node)
89
+ node_pos = node.position
90
+ pos = node_pos > 0 ? node_pos - 1 : node_pos
91
+
92
+ ActiveRecord::Base.transaction do
93
+ if list_scope.at(pos).exists?
94
+ pos = node_pos
95
+ node.and_higher.shift!
96
+ end
97
+
98
+ self.position = pos
99
+ save!
100
+ end
101
+ end
102
+
103
+ def move_after(node)
104
+ node_pos = node.position
105
+ pos = node_pos + 1
106
+
107
+ ActiveRecord::Base.transaction do
108
+ if list_scope.at(pos).exists?
109
+ node.higher.shift!
110
+ end
111
+
112
+ self.position = pos
113
+ save!
114
+ end
115
+ end
116
+
117
+ protected
118
+
119
+ def list_scope
120
+ self.class.list_scope(self)
121
+ end
122
+
123
+ def where_position(cmp)
124
+ col = self.class.arel_table[position_column]
125
+ list_scope.where(col.send(cmp, position))
126
+ end
127
+
128
+ def set_default_position
129
+ self.position = list_scope.next_position
130
+ end
131
+
132
+ def set_default_position?
133
+ position.nil?
134
+ end
135
+ end
136
+
137
+ protected
138
+
139
+ def define_list_scope(list_scope)
140
+ scope :list_scope, case list_scope
141
+ when Proc
142
+ list_scope
143
+ when nil
144
+ ->(model) { self }
145
+ else
146
+ ->(model) { where(Hash[Array(list_scope).map{ |s| [ s, model[s] ] }]) }
147
+ end
148
+ end
149
+ end
150
+
151
+ ActiveRecord::Base.extend(HasOrder)
data/spec/db/schema.rb ADDED
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(version: 0) do
2
+ create_table :items, force: true do |t|
3
+ t.string :name
4
+ t.string :category
5
+
6
+ t.integer :position
7
+ end
8
+ end
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasOrder do
4
+ before(:each) do
5
+ @foo, @bar, @baz, @qux = Item.create([
6
+ { name: 'foo', category: 'A' },
7
+ { name: 'bar', category: 'A' },
8
+ { name: 'baz', category: 'B' },
9
+ { name: 'qux', category: 'B' }
10
+ ])
11
+ end
12
+
13
+ def reload_items
14
+ [ @foo, @bar, @baz, @qux ].each(&:reload)
15
+ end
16
+
17
+ describe 'position column' do
18
+ it 'defaults to "position"' do
19
+ expect(Item.position_column).to eq(:position)
20
+ end
21
+
22
+ it 'provides accessor' do
23
+ item = Item.new
24
+ item.position = 5
25
+ expect(item.position).to eq(5)
26
+ end
27
+ end
28
+
29
+ describe '.at' do
30
+ it 'scopes position' do
31
+ @foo.position = 5
32
+ @foo.save
33
+
34
+ expect(Item.at(5).first).to eq(@foo)
35
+ end
36
+ end
37
+
38
+ describe '.ordered' do
39
+ it 'ranks items according to position' do
40
+ @foo.update_attribute(:position, 2)
41
+ @bar.update_attribute(:position, 1)
42
+ @baz.update_attribute(:position, 4)
43
+ @qux.update_attribute(:position, 3)
44
+
45
+ expect(Item.ordered).to eq([ @bar, @foo, @qux, @baz ])
46
+ end
47
+ end
48
+
49
+ describe '.next_position' do
50
+ it 'returns next available position' do
51
+ @quux = Item.create!(name: 'quux')
52
+
53
+ expect(@quux.position).to be < Item.next_position
54
+ end
55
+ end
56
+
57
+ describe '#lower' do
58
+ it 'returns items with position less or equal to the item position' do
59
+ expect(@qux.lower).to match_array([ @foo, @bar, @baz ])
60
+ end
61
+ end
62
+
63
+ describe '#and_lower' do
64
+ it 'returns items with position less than or equal to the item position' do
65
+ expect(@baz.and_lower).to match_array([ @foo, @bar, @baz ])
66
+ end
67
+ end
68
+
69
+ describe '#higher' do
70
+ it 'returns items with position greater than the item position' do
71
+ expect(@foo.higher).to match_array([ @bar, @baz, @qux ])
72
+ end
73
+ end
74
+
75
+ describe '#and_higher' do
76
+ it 'returns items with position greater than or equal to the item' do
77
+ expect(@bar.and_higher).to match_array([ @bar, @baz, @qux ])
78
+ end
79
+ end
80
+
81
+ describe '#prev' do
82
+ it 'returns previous item' do
83
+ expect(@baz.prev).to eq(@bar)
84
+ end
85
+ end
86
+
87
+ describe '#next' do
88
+ it 'returns next item' do
89
+ expect(@bar.next).to eq(@baz)
90
+ end
91
+ end
92
+
93
+ context 'first item' do
94
+ subject { @foo }
95
+
96
+ it { expect(subject.lower).to be_empty }
97
+ it { expect(subject.and_lower).to eq([ subject ]) }
98
+ it { expect(subject.prev).to be_nil }
99
+ end
100
+
101
+ context 'last item' do
102
+ subject { @qux }
103
+
104
+ it { expect(subject.higher).to be_empty }
105
+ it { expect(subject.and_higher).to eq([ subject ]) }
106
+ it { expect(subject.next).to be_nil }
107
+ end
108
+
109
+ describe '#move_to' do
110
+ it 'shifts item and higher items when the position is occupied' do
111
+ @foo.update_attribute(:position, 1)
112
+ @bar.update_attribute(:position, 2)
113
+ @baz.update_attribute(:position, 3)
114
+ @qux.update_attribute(:position, 4)
115
+
116
+ @qux.move_to(@bar.position)
117
+
118
+ reload_items
119
+
120
+ expect(Item.ordered).to eq([ @foo, @qux, @bar, @baz ])
121
+ end
122
+ end
123
+
124
+ describe '#move_before' do
125
+ it 'decreases item position' do
126
+ prev_qux_position = @qux.position
127
+ @qux.move_before(@foo)
128
+
129
+ expect(@qux.position).to be < prev_qux_position
130
+ end
131
+
132
+ it 'shifts item and higher items when the position is occupied' do
133
+ @foo.update_attribute(:position, 1)
134
+ @bar.update_attribute(:position, 2)
135
+ @baz.update_attribute(:position, 3)
136
+ @qux.update_attribute(:position, 4)
137
+
138
+ @baz.move_before(@bar)
139
+
140
+ reload_items
141
+
142
+ expect(Item.ordered).to eq([ @foo, @baz, @bar, @qux ])
143
+ end
144
+ end
145
+
146
+ describe '#move_after' do
147
+ it 'increases item position' do
148
+ prev_foo_position = @foo.position
149
+ @foo.move_after(@qux)
150
+
151
+ expect(@foo.position).to be > prev_foo_position
152
+ end
153
+
154
+ it 'shifts higher items when the position is occupied' do
155
+ @foo.update_attribute(:position, 1)
156
+ @bar.update_attribute(:position, 2)
157
+ @baz.update_attribute(:position, 3)
158
+ @qux.update_attribute(:position, 4)
159
+
160
+ @foo.move_after(@bar)
161
+
162
+ reload_items
163
+
164
+ expect(Item.ordered).to eq([ @bar, @foo, @baz, @qux ])
165
+ end
166
+ end
167
+
168
+ describe 'scoping' do
169
+ shared_examples 'scoped' do
170
+ it { expect(subject.higher).to be_empty }
171
+ it { expect(subject.and_lower).to match_array([ @bar, @foo ]) }
172
+ end
173
+
174
+ describe 'via attributes' do
175
+ before(:all) do
176
+ Item.has_order scope: :category
177
+ end
178
+
179
+ subject { @bar }
180
+
181
+ it_behaves_like 'scoped'
182
+ end
183
+
184
+ describe 'via proc' do
185
+ before(:all) do
186
+ Item.has_order scope: ->(i){ Item.where(category: i.category) }
187
+ end
188
+
189
+ subject { @bar }
190
+
191
+ it_behaves_like 'scoped'
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,28 @@
1
+ if ENV['SIMPLECOV']
2
+ require 'simplecov'
3
+ SimpleCov.start 'rails'
4
+ end
5
+
6
+ require 'fileutils'
7
+ require 'logger'
8
+ require 'sqlite3'
9
+ require 'active_record'
10
+
11
+ FileUtils.mkdir_p('tmp/logs/')
12
+ ActiveRecord::Base.logger = Logger.new('tmp/logs/test.log')
13
+ ActiveRecord::Base.logger.level = Logger::DEBUG
14
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
15
+ ActiveRecord::Schema.verbose = false
16
+
17
+ require File.expand_path('../db/schema.rb', __FILE__)
18
+ require File.expand_path('../support/models.rb', __FILE__)
19
+
20
+ RSpec.configure do |config|
21
+ config.around :each do |example|
22
+ ActiveRecord::Base.transaction do
23
+ example.run
24
+
25
+ raise ActiveRecord::Rollback
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ require 'has_order'
2
+
3
+ class Item < ActiveRecord::Base
4
+ has_order
5
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_order
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Kolesnikov Danil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4'
111
+ description: Provides list behavior to active_record models.
112
+ email:
113
+ - kolesnikovde@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".editorconfig"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - CHANGELOG.md
122
+ - Gemfile
123
+ - README.md
124
+ - Rakefile
125
+ - has_order.gemspec
126
+ - lib/has_order.rb
127
+ - lib/has_order/version.rb
128
+ - spec/db/schema.rb
129
+ - spec/has_order_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/models.rb
132
+ homepage: https://github.com/kolesnikovde/has_order
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.2.2
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Provides list behavior to active_record models.
156
+ test_files:
157
+ - spec/db/schema.rb
158
+ - spec/has_order_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/models.rb