hurray 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/hurray.rb +26 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d20a55f41c307aa0559f2fe780ac4bbf95dbca2c
|
4
|
+
data.tar.gz: 7ef24af6338e7a73bba1dbb04151be152a8d8751
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f035f164111f1f49f95d0865663840785c10205eae92ba9d6f737528d7e006a3eb63832d6d742c9330a8c7699f82e8a9d44d5777f7998e18d84476753c04269
|
7
|
+
data.tar.gz: 06ab89fa1a36e0cc7f4840100c97f99e7a7d20218c42f96a81b9165567a65b0a5ad68ed646f9816131260f393dd2f7f49e4e42ddb5cccfd672570cd476b4105c
|
data/lib/hurray.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# ActiveRecord module for working with database arrays
|
2
|
+
module Hurray
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
# Get records ordered according to the ids order
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
# >> User.ordered_with([4,2,6])
|
10
|
+
# => User::ActiveRecord_Relation
|
11
|
+
# => [#<User id: 4 ...>, #<User id: 2 ...>, #<User id: 6 ...>]
|
12
|
+
#
|
13
|
+
# Arguments:
|
14
|
+
# ids: (Array)
|
15
|
+
def ordered_with(ids)
|
16
|
+
order_clause = 'CASE id '
|
17
|
+
ids.each_with_index do |id, index|
|
18
|
+
order_clause << sanitize_sql_array(['WHEN ? THEN ? ', id, index])
|
19
|
+
end
|
20
|
+
order_clause << sanitize_sql_array(['ELSE ? END', ids.length])
|
21
|
+
where(id: ids).order(order_clause)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveRecord::Base.send(:include, Hurray)
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hurray
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ruslan Kotov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Simple ActiveRecord extension for working with arrays
|
14
|
+
email: rkotov93@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/hurray.rb
|
20
|
+
homepage: https://github.com/rkotov93/hurray
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.5.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Hurray!
|
44
|
+
test_files: []
|