random_order 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c88e7bc11ab8afd4a6ada8c58dba72416dcfce0ab4dfaa17cea9949e75c0369
4
- data.tar.gz: 527e4a9fc42c9c4822a544d70236582bab52b84e5fa21d1c79cd0a5d541cc0c4
3
+ metadata.gz: 85869c402378af9d2b328e80a04f2324bf044ffbabbaed80909e52f37ba3957f
4
+ data.tar.gz: 5d6ee714b7f0311a88f7ed34134b655dec57ece3543e8a9e1c26ce5468ef4917
5
5
  SHA512:
6
- metadata.gz: 750f1300766024397ae2074be27174cb136b99c7968e3e1b565e1181c2fcc751c5ab1aaeb3d9a7242a8a7bbfe00cf886979dfac3d824190c601ce99df61434db
7
- data.tar.gz: 437860646c3266b4248481da5ee873354ae5cd572a8fc9fe8798cb691f697235e482ead70bca41986c4b079af6f7d9ba8c735718ed9d85d7c2fec3bde341088d
6
+ metadata.gz: 6b02bb4634682f6872a708be802b7f44b1bafa9ce72a876c6a29f57bb5154653608d609519f1045e67d4f8082c7f23619ed66f826ac947f735f1d8f7c66ba536
7
+ data.tar.gz: 85796b4d178f8ea035773829df8b8a7a7f8e15bd5f632f7ab01b204f479b44d91b630810e5a34d04262dd761d7eca6ee7c317400b5439bef2acf691ce4e875e2
data/README.md CHANGED
@@ -1,8 +1,29 @@
1
1
  # RandomOrder
2
- Short description and motivation.
2
+ This gem could DRY your code and return random relation objects or instance object.
3
3
 
4
4
  ## Usage
5
- How to use my plugin.
5
+ Works with `MySQL`, `SQLite`, `PostgreSQL`
6
+
7
+ This gem is adding three methods to ActiveRecord Relations - `random` , `find_random` and `find_many_random(...)`.
8
+
9
+ You can do the following:
10
+ ```ruby
11
+ u1 = User.create(name: 'Lilith')
12
+ u2 = User.create(name: 'Metamorph')
13
+ u3 = User.create(name: 'Diablo')
14
+
15
+ assert_equal User.find_random.is_a?(User), true
16
+ assert_equal User.random.is_a?(ActiveRecord::Relation), true
17
+ assert_equal User.find_many_random(10).is_a?(ActiveRecord::Relation), true
18
+ ```
19
+ #### Examples:
20
+ ```ruby
21
+ User.friends.random # return ActiveRecord::Relation with user`s random friends
22
+ User.find_random.friends.random # return ActiveRecord::Relation with random user`s random friends
23
+ Post.published.random.limit(3) # return ActiveRecord::Relation with 3 published posts
24
+ Post.published.find_many_random(3) # --//--
25
+ ```
26
+
6
27
 
7
28
  ## Installation
8
29
  Add this line to your application's Gemfile:
@@ -25,4 +46,4 @@ $ gem install random_order
25
46
  Contribution directions go here.
26
47
 
27
48
  ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
49
+ The gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,28 +1,11 @@
1
+ require_relative "random_order/extension"
2
+
1
3
  module RandomOrder
2
4
  class Railtie < ::Rails::Railtie
3
- module Extension
4
- def find_many_random(n)
5
- random.limit(n || 1)
6
- end
7
-
8
- def find_random
9
- random.limit(1)
10
- end
11
-
12
- def random
13
- case ActiveRecord::Base.configurations[Rails.env]['adapter']
14
- when 'mysql2'
15
- self.order(Arel.sql('RAND()'))
16
- when 'sqlite3'
17
- self.order(Arel.sql('RANDOM()'))
18
- when 'postgresql'
19
- self.order(Arel.sql('RANDOM()'))
20
- end
21
- end
22
- end
5
+ include Extension
23
6
 
24
7
  initializer 'RandomOrder.initialize' do
25
- ActiveSupport.on_load(:active_record) do
8
+ if RandomOrder.const_defined?("ActiveRecord")
26
9
  ActiveRecord::Base.send :extend, Extension
27
10
  end
28
11
  end
@@ -0,0 +1,22 @@
1
+ module RandomOrder
2
+ module Extension
3
+ def find_many_random(n)
4
+ random.limit(n)
5
+ end
6
+
7
+ def find_random
8
+ random.first
9
+ end
10
+
11
+ def random
12
+ case RandomOrder.adapter
13
+ when 'mysql2'
14
+ self.order(Arel.sql('RAND()'))
15
+ when 'postgresql'
16
+ self.order(Arel.sql('RANDOM()'))
17
+ when 'sqlite3'
18
+ self.order(Arel.sql('RANDOM()'))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module RandomOrder
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/random_order.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  require "random_order/railtie"
2
2
 
3
3
  module RandomOrder
4
- # Your code goes here...
4
+ def self.adapter
5
+ @@adapter ||= detect_adapter
6
+ end
7
+
8
+ def self.detect_adapter
9
+ if self.const_defined?("ActiveRecord")
10
+ ActiveRecord::Base.configurations[Rails.env]['adapter']
11
+ end
12
+ end
5
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_order
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
  - Vitalii Kasianchuk
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
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
+ - !ruby/object:Gem::Dependency
56
+ name: postgres
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: Find random record in your models, just to DRY your code.
42
70
  email:
43
71
  - kasvit93@gmail.com
@@ -50,6 +78,7 @@ files:
50
78
  - Rakefile
51
79
  - lib/random_order.rb
52
80
  - lib/random_order/railtie.rb
81
+ - lib/random_order/random_order/extension.rb
53
82
  - lib/random_order/version.rb
54
83
  - lib/tasks/random_order_tasks.rake
55
84
  homepage: https://github.com/Kasvit/random_order