prioritize 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84063454a547cceae27c20081b6ad0f833cd352a2393a8a1670f2eabeca59ae2
4
- data.tar.gz: 5ac4d9025cd9eb0f0de80cd1e89bb011e73df2a2886cf97fde2a801ab6221f14
3
+ metadata.gz: 9b99951c3c1686db3b4a99fff3592ebfb582f15c415d3ba9b9aed700da892ffa
4
+ data.tar.gz: 851859d7029841b26edaa1be559b0b0c90e955face7ad0bfa6a0efad21ae5e10
5
5
  SHA512:
6
- metadata.gz: d7a789c449df198afe50c8d72de196fd2ada1b82451945901b725ebb9dbb4dd8558efa60dbc4e588815575fccc307a14d2f15ff48656d1a3aefde9f6dc3227f9
7
- data.tar.gz: b2dff65570402d3618231ac5dc9cd70d20d2bd759ec749600692b63f523d7b1d8d86aa51122fde3025d138993197a8a8c2db73b2c26bae51dc9f253461858ef8
6
+ metadata.gz: 2cff003c4c741977949eab296c38f08b062b0c7e287155dd8bd54687a508f6d668c7b66654800d8bdb5ff6e45ce9c8484103965f1ca6341a23945a8b584a976f
7
+ data.tar.gz: 21086d241e44a28ed27fffed0a1af65035f2c89e5060ec7e75ad3bc7c00adb703c086b5b133be2bc1350f43850faf268fc4751ffba1c5ef640439eab8c906834
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prioritize (1.0.0)
4
+ prioritize (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README-ru.md CHANGED
@@ -7,9 +7,9 @@ __Выбрать язык README.md__
7
7
  - en [English](README.md)
8
8
  - ru [Русский](README-ru.md)
9
9
 
10
- __Важно!__ Работает только с PostgreSQL бд. Обновление данных происходит за 1
11
- запрос, Пока что нет "ActiveRecord" алгоритма, на подобии: выбрать данные ->
12
- перебрать-преобразовать -> сохранить. Только SQL, только хардкор.
10
+ __Важно!__ Работает только с PostgreSQL и SQLite бд. В PostgreSQL обновление данных
11
+ происходит за 1 запрос, в SQLite используется «ActiveRecord» алгоритм
12
+ (выбрать данные -> перебрать-преобразовать -> сохранить)
13
13
 
14
14
  _Prioritize_ добавляет следующие возможности:
15
15
 
data/README.md CHANGED
@@ -8,9 +8,9 @@ __Select language README.md__
8
8
  - en [English](README.md)
9
9
  - ru [Русский](README-ru.md)
10
10
 
11
- __Important!__ Only works with PostgreSQL databases. Updating data occurs in 1
12
- request, So far there is no "ActiveRecord" algorithm, like: select data ->
13
- sort-transform data -> save. Only SQL, only hardcore.
11
+ __Important!__ Works only with PostgreSQL and SQLite databases. In PostgreSQL, data
12
+ is updated in 1 query, in SQLite, the "ActiveRecord" algorithm is used
13
+ (select data -> iterate-transform -> save)
14
14
 
15
15
  _Prioritize_ adds the following features:
16
16
 
@@ -1,3 +1,3 @@
1
1
  module Prioritize
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/prioritize.rb CHANGED
@@ -68,11 +68,15 @@ module Prioritize
68
68
  # подмодуле.
69
69
  module ClassMethods
70
70
  def priority_after(prev_id, moved_id)
71
- connection.exec_query(
72
- priority_sql,
73
- 'priority_after',
74
- [[nil, prev_id], [nil, moved_id]]
75
- )
71
+ if connection.adapter_name == 'SQLite'
72
+ priority_sqlite(prev_id, moved_id)
73
+ else
74
+ connection.exec_query(
75
+ priority_sql,
76
+ 'priority_after',
77
+ [[nil, prev_id], [nil, moved_id]]
78
+ )
79
+ end
76
80
  end
77
81
 
78
82
  def priority_sql
@@ -127,6 +131,26 @@ module Prioritize
127
131
  sql
128
132
  end
129
133
 
134
+ def priority_sqlite(prev_id, moved_id)
135
+ list = select('id', "#{priority_column}").order("#{priority_column}" => :asc).where.not(id: moved_id).to_a
136
+ moved = select('id', "#{priority_column}").find_by_id(moved_id)
137
+ if prev_id.nil?
138
+ list.unshift(moved)
139
+ else
140
+ list.each_with_index do |model, index|
141
+ if model.id == prev_id
142
+ list.insert(index + 1, moved)
143
+ break
144
+ end
145
+ end
146
+ end
147
+ transaction do
148
+ list.each_with_index do |model, index|
149
+ model.update("#{priority_column}" => index)
150
+ end
151
+ end
152
+ end
153
+
130
154
  # def priority_column
131
155
  # @priority_column.to_s
132
156
  # end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prioritize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zlatov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-12 00:00:00.000000000 Z
11
+ date: 2022-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print