hurray 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hurray.rb +31 -22
  3. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66fc996dcb6ff5de44ceda87d0b9de9df69bbc40
4
- data.tar.gz: 2918a2a990f457a1ac33d36192a0814915a888b3
3
+ metadata.gz: b793175c03a722e4ccefe615c0ed55eef56016db
4
+ data.tar.gz: e8512e3b6696f47ed55eaf0bd66cf8c6a7ae290f
5
5
  SHA512:
6
- metadata.gz: a81f7f152f5c3521cb3e163c1e4b886a8191d4a9b57e4f1af81f3edf3b164d8afa1804c03e3ac1c1bb8b54e0a5eee92f70c69d6792c596111ee6afeeefe37e20
7
- data.tar.gz: e37f3aabb3a41d24f49cb674194cd7171070807f2f5d7a87a9631becac96cca5ad7d4370c574a67892620ee5dff8d891f303306dd0970d26f01207ed01681edc
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(:id, [4,2,6])
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, [4,2,6], provided_only: true)
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
- # column: (Key or String) # DB column
20
- # array: (Array) # Custom order
21
- # options: (Hash)
22
- # povided: (Boolean) # Get records satisfying array values
23
- # direction: (Key or String) # :asc or :desc
24
-
25
- def ordered_with(column, array, options = {})
26
- direction = options[:direction].to_s.upcase
27
- direction = %w(ASC DESC).include?(direction) ? direction : 'ASC'
28
- provided_only = options[:provided_only].nil? ? false : options[:provided_only]
29
-
30
- order_clause = "CASE #{column} "
31
- array.each_with_index do |el, index|
32
- order_clause << sanitize_sql_array(['WHEN ? THEN ? ', el, index])
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.0
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-15 00:00:00.000000000 Z
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: []