hurray 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/lib/hurray.rb +31 -22
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b793175c03a722e4ccefe615c0ed55eef56016db
|
4
|
+
data.tar.gz: e8512e3b6696f47ed55eaf0bd66cf8c6a7ae290f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dfff74b4ddd096c24baf0aa379f23355463d2c7891dbd1f16ff25f04a2d580fbaf8de59f9980f210e83560cc234fb95f9c863acdfbfd638eb726ec1265036ad
|
7
|
+
data.tar.gz: bbaa37b46f7ce1acb105a2685aae896a571f6fd78647a545bb87069833b87ecd534e7b04c6ab9cc020442dcf1dd9312273e34260ed3eec42c976c56a499ef733
|
data/lib/hurray.rb
CHANGED
@@ -6,36 +6,45 @@ module Hurray
|
|
6
6
|
# Get records ordered according to the specified with array column order
|
7
7
|
#
|
8
8
|
# Examples:
|
9
|
-
# >> User.ordered_with(:
|
9
|
+
# >> User.ordered_with(id: [4,2,6])
|
10
10
|
# => User::ActiveRecord_Relation [#<User id: 4 ...>, #<User id: 2 ...>, #<User id: 6 ...>, #<User id: 1 ...>, ...]
|
11
11
|
#
|
12
|
-
# >> User.ordered_with(:id
|
13
|
-
# => User::ActiveRecord_Relation [#<User id: 4 ...>, #<User id: 2 ...>, #<User id: 6 ...>]
|
14
|
-
#
|
15
|
-
# >> User.ordered_with(:name, %w(Nick Malvin John), provided_only: true)
|
16
|
-
# => User::ActiveRecord_Relation [#<User name: Nick ...>, #<User name: Malvin ...>, #<User name: John ...>]
|
12
|
+
# >> User.joins(:friends).ordered_with(friends: { id: [4, 2, 6] })
|
17
13
|
#
|
18
14
|
# Arguments:
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
order_clause << sanitize_sql_array(['
|
15
|
+
# hash: (Hash)
|
16
|
+
|
17
|
+
def ordered_with(hash)
|
18
|
+
params = parse_ordered_with_params(hash)
|
19
|
+
|
20
|
+
order_clause = ''
|
21
|
+
params.each_with_index do |param, index|
|
22
|
+
order_clause << ' , ' if index != 0
|
23
|
+
order_clause << "CASE #{param[:table]}.#{param[:column]} "
|
24
|
+
array = param[:array]
|
25
|
+
array.each_with_index do |el, pos|
|
26
|
+
order_clause << sanitize_sql_array(['WHEN ? THEN ? ', el, pos])
|
27
|
+
end
|
28
|
+
order_clause << sanitize_sql_array(['ELSE ? END', array.length])
|
33
29
|
end
|
34
|
-
order_clause << sanitize_sql_array(['ELSE ? END ', array.length]) << direction
|
35
30
|
|
36
|
-
return where(column => array).order(order_clause) if provided_only
|
37
31
|
order(order_clause)
|
38
32
|
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def parse_ordered_with_params(table = table_name, hash)
|
37
|
+
results = []
|
38
|
+
hash.each do |k, v|
|
39
|
+
if v.is_a? Hash
|
40
|
+
parse_ordered_with_params(k.to_s, v)
|
41
|
+
else
|
42
|
+
results << { table: table, column: k.to_s, array: v }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
results
|
46
|
+
end
|
47
|
+
|
39
48
|
end
|
40
49
|
end
|
41
50
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hurray
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Kotov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
12
|
-
dependencies:
|
11
|
+
date: 2016-04-22 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'
|
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'
|
13
27
|
description: Simple ActiveRecord extension for working with database arrays fields
|
14
28
|
email: rkotov93@gmail.com
|
15
29
|
executables: []
|