activerecord_sortable 0.0.4 → 0.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 +4 -4
- data/lib/activerecord_sortable.rb +2 -1
- data/lib/activerecord_sortable/collection_proxy.rb +19 -0
- data/lib/activerecord_sortable/through_reflection.rb +16 -0
- data/lib/activerecord_sortable/version.rb +1 -1
- data/spec/models/association_reflection_spec.rb +3 -3
- metadata +11 -4
- data/lib/activerecord_sortable/association_relation.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bf73c95b55406d62e73c59a4e3d61b1fe9f7116
|
4
|
+
data.tar.gz: 55acfcb751c18e34f8dd09d016444a105cbcd9ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef04696ffa2b16931bae9b146f4bf20c74f0f20fdefb1b4d27dd6ac26ef2c60a3e979f0bfc53286a0615dd6396fa89d7f0aea9c5c1c18cbfb410d9a1e02cdad7
|
7
|
+
data.tar.gz: b0e3147d74a143f2d3bf10c5828b440b7a2aae5ad663131d373f456f665f66bd2d0e1931462b7896350544f0253ec5d036c40fccf302fc9e6b2c981ed72413b9
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'activerecord_sortable/association'
|
2
2
|
require 'activerecord_sortable/association_reflection'
|
3
|
-
require 'activerecord_sortable/
|
3
|
+
require 'activerecord_sortable/through_reflection'
|
4
|
+
require 'activerecord_sortable/collection_proxy'
|
4
5
|
require 'activerecord_sortable/has_many_association'
|
5
6
|
|
6
7
|
module ActiveRecordSortable
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module Associations
|
4
|
+
class CollectionProxy
|
5
|
+
|
6
|
+
alias_method :original_load_target, :load_target
|
7
|
+
|
8
|
+
def load_target
|
9
|
+
if proxy_association.reflection.sortable? and order_values.empty?
|
10
|
+
sortable_table_name = (proxy_association.try(:through_reflection) || proxy_association.reflection).table_name
|
11
|
+
order("#{sortable_table_name}.#{proxy_association.reflection.sortable_field} ASC").to_a
|
12
|
+
else
|
13
|
+
original_load_target
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module Reflection
|
4
|
+
class ThroughReflection
|
5
|
+
|
6
|
+
def sortable_field
|
7
|
+
@delegate_reflection.options.fetch(:sortable_field, :position).to_sym
|
8
|
+
end
|
9
|
+
|
10
|
+
def sortable?
|
11
|
+
@delegate_reflection.options.fetch(:sortable, false)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -3,15 +3,15 @@ require 'spec_helper'
|
|
3
3
|
describe 'Association reflection' do
|
4
4
|
|
5
5
|
let(:post_reflection) do
|
6
|
-
User.reflections[
|
6
|
+
User.reflections['posts']
|
7
7
|
end
|
8
8
|
|
9
9
|
let(:user_post_reflection) do
|
10
|
-
User.reflections[
|
10
|
+
User.reflections['user_posts']
|
11
11
|
end
|
12
12
|
|
13
13
|
let(:sorted_post_reflection) do
|
14
|
-
User.reflections[
|
14
|
+
User.reflections['sorted_posts']
|
15
15
|
end
|
16
16
|
|
17
17
|
describe '.sortable?' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_sortable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladislav Melanitskiy
|
@@ -16,14 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.
|
19
|
+
version: '4.2'
|
20
|
+
- - '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.2.0
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - ~>
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.
|
29
|
+
version: '4.2'
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.2.0
|
27
33
|
description: It adds possibility for sorting relation
|
28
34
|
email:
|
29
35
|
- co@rademade.com
|
@@ -34,8 +40,9 @@ files:
|
|
34
40
|
- lib/activerecord_sortable.rb
|
35
41
|
- lib/activerecord_sortable/association.rb
|
36
42
|
- lib/activerecord_sortable/association_reflection.rb
|
37
|
-
- lib/activerecord_sortable/
|
43
|
+
- lib/activerecord_sortable/collection_proxy.rb
|
38
44
|
- lib/activerecord_sortable/has_many_association.rb
|
45
|
+
- lib/activerecord_sortable/through_reflection.rb
|
39
46
|
- lib/activerecord_sortable/version.rb
|
40
47
|
- spec/dummy/README.rdoc
|
41
48
|
- spec/dummy/Rakefile
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module ActiveRecord
|
3
|
-
class AssociationRelation
|
4
|
-
|
5
|
-
alias_method :original_to_a, :to_a
|
6
|
-
|
7
|
-
def to_a
|
8
|
-
if proxy_association.reflection.sortable? and order_values.empty?
|
9
|
-
sortable_table_name = (proxy_association.try(:through_reflection) || proxy_association.reflection).table_name
|
10
|
-
order("#{sortable_table_name}.#{proxy_association.reflection.sortable_field} ASC").to_a
|
11
|
-
else
|
12
|
-
original_to_a
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|