record_neighbors 0.0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25501764dbb2ff2fff6805c81d0ea361aa453caa
4
+ data.tar.gz: 3dc463e600659173204b7e3b3f19481c8bd02d7e
5
+ SHA512:
6
+ metadata.gz: 09df179a6087569d19dec297316b62daa34247ea6693ccfbf398b001fe8be53bb433c04bf5a1cd3faf2c3f1b6b22bcb1479eac99a9e53354b80ae4f761ce2a45
7
+ data.tar.gz: e6b6343e8f9c7cb4c3027bf52d6d93d3d571b948120e2b72e9ff677da1e6465178ed261fdb05b06d3c993d1f0bb5637a96a3df8ce3bb04cb99115b4f00f8bed0
@@ -0,0 +1,20 @@
1
+ 2014-11-29 Ancalima Silme <ancalimasilme@gmail.com>
2
+
3
+ * Написал README.md
4
+ * Убрал у метода `all_without` атрибут.
5
+
6
+ 2014-11-29 Ancalima Silme <ancalimasilme@gmail.com>
7
+
8
+ * Переместил БД с тестовыми данными в RAM
9
+ * Добавил тесты
10
+
11
+ 2014-11-29 Ancalima Silme <ancalimasilme@gmail.com>
12
+
13
+ * Подключены модули ActiveRecord, SQLite3, RSpec для тестирования плагина
14
+ * Создала БД с тестовыми данными
15
+ * Написано несколько базовых тестов
16
+
17
+ 2014-11-26 Ancalima Silme <ancalimasilme@gmail.com>
18
+
19
+ * Для всех функций добавлена возможность указать атрибут, по которому будет отсортирована выборка и на основе которого будет
20
+ определен следующий/предыдущий, первый/последний элемент.
@@ -0,0 +1,72 @@
1
+ # RecordNeighbors
2
+
3
+ Это простой плагин для Ruby/Ruby on Rails, расширяющий возможность ActiveRecord
4
+
5
+ ### Установка
6
+
7
+ ##### Для Ruby c ActiveRecord
8
+
9
+ ```
10
+ gem install 'record_neighbors'
11
+ ```
12
+
13
+ ```
14
+ # my_app.rb
15
+ require 'active_record'
16
+ require 'record_neighbors'
17
+ ```
18
+
19
+ ##### Для Ruby on Rails
20
+
21
+ # Gemfile
22
+ gem 'record_neighbors'
23
+
24
+ ### Список методов
25
+
26
+ * first?
27
+ * last?
28
+ * next
29
+ * previous
30
+ * all_after
31
+ * all_before
32
+ * all_without
33
+
34
+ В качестве параметра методам можно передать название столбца. Выборка будет отсортирована по переданному атрибуту.
35
+
36
+ #### Подробнее о методах "first?" и last?
37
+
38
+ Методы **first?** и **last?** возращают TRUE если элемент является первым/последним в выборке.
39
+ Пример использования:
40
+
41
+ Post.find(1).first? #=> true
42
+ Post.find_by(position: 1).first?(:position) #=> true
43
+
44
+ Post.find(10).last? #=> true
45
+ Post.find_by(position: 1).first?(:position) #=> true
46
+
47
+ #### Подробнее о методах "next" и "previous"
48
+
49
+ Как следует из названия, методы **next** и **previous** возращают следующий/предыдущий объект в выборке.
50
+ Пример использования:
51
+
52
+ Post.find(1).next #=> #<Post id: 2, position: 3>
53
+ Post.find(5).previous #=> #<Post id: 4, position: 10>
54
+
55
+ post.find_by(position: 1).next(:position) #=> #<Post id: 1, position: 2>
56
+ post.find_by(position: 5).previous(:position) #=> #<Post id: 10, position: 4>
57
+
58
+ #### Подробнее о методах "all_before и "all_after"
59
+
60
+ Методы **all_before** и **all_after** возвращают коллекцию объектов до/после текущего. Текущий элемент не включается.
61
+
62
+ Post.find(5).all_before
63
+ Post.find(5).all_after
64
+
65
+ Post.find_by(position: 5).all_before(:position)
66
+ Post.find_by(position: 5).all_after(:position)
67
+
68
+ #### Подробнее о методах "all_without"
69
+
70
+ Методы **all_without** возвращают коллекцию объектов без текущего элемента.
71
+
72
+ Post.find(5).all_without.pluck(:id) #=> [1, 2, 3, 4, 6, 7, 8, 9, 10]
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,4 @@
1
+ require 'record_neighbors/base_extension'
2
+
3
+ module RecordNeighbors
4
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_support/concern'
2
+
3
+ module BaseExtension
4
+ extend ActiveSupport::Concern
5
+
6
+ def last?(attribute = "id")
7
+ self == self.class.order("#{attribute} ASC").last
8
+ end
9
+
10
+ def first?(attribute = "id")
11
+ self == self.class.order("#{attribute} ASC").first
12
+ end
13
+
14
+ def next(attribute = "id")
15
+ self.class.where("#{attribute} > #{self.send(attribute)}").order("#{attribute} ASC").limit(1).first
16
+ end
17
+
18
+ def previous(attribute = "id")
19
+ self.class.where("#{attribute} < #{self.send(attribute)}").order("#{attribute} DESC").limit(1).first
20
+ end
21
+
22
+ def all_after(attribute = "id")
23
+ self.class.where("#{attribute} > #{self.send(attribute)}").order("#{attribute} ASC")
24
+ end
25
+
26
+ def all_before(attribute = "id")
27
+ self.class.where("#{attribute} < #{self.send(attribute)}").order("#{attribute} ASC")
28
+ end
29
+
30
+ def all_without
31
+ self.class.where.not(id: self.id)
32
+ end
33
+
34
+ ActiveRecord::Base.send(:include, BaseExtension)
35
+ end
@@ -0,0 +1,3 @@
1
+ module RecordNeighbors
2
+ VERSION = "0.0.2.1"
3
+ end
@@ -0,0 +1,117 @@
1
+ require_relative 'spec_helper.rb'
2
+ require_relative 'support/test_database.rb'
3
+ require_relative '../lib/record_neighbors/base_extension.rb'
4
+
5
+
6
+ # post title 1 - 2
7
+ # post title 2 - 3
8
+ # post title 3 - 1
9
+ # post title 4 - 10
10
+ # post title 5 - 9
11
+ # post title 6 - 6
12
+ # post title 7 - 5
13
+ # post title 8 - 7
14
+ # post title 9 - 8
15
+ # post title 10 - 4
16
+
17
+
18
+ RSpec.describe 'next()' do
19
+ it "Должен вернуть следующий объект" do
20
+ expect(Post.find(5)).to eql(Post.find(4).next)
21
+ end
22
+
23
+ it "Должен вернуть nil если следующего элемента нет" do
24
+ expect(Post.find(10).next).to be_nil
25
+ end
26
+
27
+ it "Должен вернуть следующий объект из отсортированного списка по переданному атрибуту" do
28
+ expect(Post.find(7).next('position')).to eql(Post.find(6))
29
+ end
30
+ end
31
+
32
+ RSpec.describe 'previous()' do
33
+ it "Должен вернуть предыдущий объект" do
34
+ expect(Post.find(5)).to eql(Post.find(6).previous)
35
+ end
36
+
37
+ it "Должен вернуть nil если предыдущего элемента нет" do
38
+ expect(Post.find(1).previous).to be_nil
39
+ end
40
+
41
+ it "Должен вернуть предыдущий объект из отсортированного списка по переданному атрибуту" do
42
+ expect(Post.find(7).previous('position')).to eql(Post.find(10))
43
+ end
44
+ end
45
+
46
+ RSpec.describe 'last?()' do
47
+ it "Должен вернуть истину, если объект является последним" do
48
+ expect(Post.find(10).last?).to be_truthy
49
+ end
50
+
51
+ it "Должен вернуть ложь, если объект не является последним" do
52
+ expect(Post.find(5).last?).to be_falsey
53
+ end
54
+
55
+ it "Должен вернуть истину, если объект является последним в отсортированном, по атрибуту, списке" do
56
+ expect(Post.find(4).last?('position')).to be_truthy
57
+ end
58
+
59
+ it "Должен вернуть ложь, если объект не является последним в отсортированном, по атрибуту, списке" do
60
+ expect(Post.find(10).last?('position')).to be_falsey
61
+ end
62
+ end
63
+
64
+ RSpec.describe 'first?()' do
65
+ it "Должен вернуть истину, если объект является первым" do
66
+ expect(Post.find(1).first?).to be_truthy
67
+ end
68
+
69
+ it "Должен вернуть ложь, если объект не является первым" do
70
+ expect(Post.find(5).first?).to be_falsey
71
+ end
72
+
73
+ it "Должен вернуть истину, если объект является первым в отсортированном, по атрибуту, списке" do
74
+ expect(Post.find(3).first?('position')).to be_truthy
75
+ end
76
+
77
+ it "Должен вернуть ложь, если объект не является первым в отсортированном, по атрибуту, списке" do
78
+ expect(Post.find(1).first?('position')).to be_falsey
79
+ end
80
+ end
81
+
82
+ RSpec.describe 'all_before()' do
83
+ it "Все ID должны быть меньше 5" do
84
+ expect(Post.find(5).all_before.map { |p| p.id }).to all(be < 5)
85
+ end
86
+
87
+ it "Все Position должны быть меньше 5" do
88
+ expect(Post.find_by(position: 5).all_before('position').map { |p| p.position }).to all(be < 5)
89
+ end
90
+ end
91
+
92
+ RSpec.describe 'all_after()' do
93
+ it "Все ID должны быть больше 5" do
94
+ expect(Post.find(5).all_after.map { |p| p.id }).to all(be > 5)
95
+ end
96
+
97
+ it "Все Position должны быть больше 5" do
98
+ expect(Post.find_by(position: 5).all_after('position').map { |p| p.position }).to all(be > 5)
99
+ end
100
+ end
101
+
102
+ RSpec.describe 'all_without()' do
103
+ it "Количество элементв должно быть model.length - 1" do
104
+ expect(Post.find(5).all_without.size).to eql(Post.all.size - 1)
105
+ end
106
+
107
+ it "В результате не должно быть объекта с ID == 5" do
108
+ expect(Post.find(5).all_without).to_not include(Post.find(5))
109
+ end
110
+ end
111
+
112
+
113
+ RSpec.describe ActiveRecord::Base do
114
+ it "Должен включать все методы плагина" do
115
+ expect(ActiveRecord::Base.instance_methods).to include(:next, :previous, :first?, :last?, :all_before, :all_after, :all_without)
116
+ end
117
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |c|
4
+ c.mock_with :rspec
5
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ adapter: "sqlite3",
5
+ database: ":memory:"
6
+ )
7
+
8
+ class Post < ActiveRecord::Base
9
+ scope :on_position, -> { order(position: :asc) }
10
+ end
11
+
12
+ ActiveRecord::Schema.define do
13
+ create_table :posts do |t|
14
+ t.column :position, :indeger
15
+ end
16
+ end
17
+
18
+ [2, 3, 1, 10, 9, 6, 5, 7, 8, 4].each_with_index do |position, index|
19
+ Post.create!(position: position)
20
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: record_neighbors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Ancalima Silme
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-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: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Make possible get neighbors for ActiveRecord object.
56
+ email:
57
+ - ancalimasilme@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGE.log
63
+ - README.md
64
+ - Rakefile
65
+ - lib/record_neighbors.rb
66
+ - lib/record_neighbors/base_extension.rb
67
+ - lib/record_neighbors/version.rb
68
+ - spec/database_spec.rb
69
+ - spec/spec_helper.rb
70
+ - spec/support/test_database.rb
71
+ homepage:
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Neighbors of ActiveRecord object
94
+ test_files:
95
+ - spec/database_spec.rb
96
+ - spec/support/test_database.rb
97
+ - spec/spec_helper.rb