activerecord_sortable 0.0.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 +7 -0
- data/lib/activerecord_sortable/association.rb +16 -0
- data/lib/activerecord_sortable/association_reflection.rb +16 -0
- data/lib/activerecord_sortable/association_relation.rb +17 -0
- data/lib/activerecord_sortable/has_many_association.rb +52 -0
- data/lib/activerecord_sortable/version.rb +4 -0
- data/lib/activerecord_sortable.rb +8 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d8fa68937adc8ddb575aa1b01c978566cb1c103e
|
4
|
+
data.tar.gz: be035324af3214219cadbf28022a8997d0574489
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c3095080eceb01f99dc7b4901956b93477b91a965ad4d2da4747d910e8b4ec3509380a1296ea0e11aae97d4a6b98b1e79618938749463d9c7ddb991a07a28443
|
7
|
+
data.tar.gz: 2f995737f10663ada46b3214992d6eaca04442c31ab616403455d4f3b93b40c1df0e9e08e8b92b4df06aa6ae58b05bb2b70eb805a322899601160164d2e0171e
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module Associations
|
4
|
+
module Builder
|
5
|
+
class Association
|
6
|
+
|
7
|
+
alias_method :original_valid_options, :valid_options
|
8
|
+
|
9
|
+
def valid_options
|
10
|
+
original_valid_options + [:sortable, :sortable_field]
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module Reflection
|
4
|
+
class AssociationReflection
|
5
|
+
|
6
|
+
def sortable_field
|
7
|
+
@options.fetch(:sortable_field, :position)
|
8
|
+
end
|
9
|
+
|
10
|
+
def sortable?
|
11
|
+
@options.fetch(:sortable, false)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ActiveRecord
|
3
|
+
module Associations
|
4
|
+
class HasManyAssociation
|
5
|
+
|
6
|
+
alias_method :original_reader, :reader
|
7
|
+
alias_method :original_writer, :writer
|
8
|
+
|
9
|
+
def writer(records)
|
10
|
+
original_writer(records)
|
11
|
+
sort_entities(records)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def sort_entities(records)
|
17
|
+
if reflection.sortable?
|
18
|
+
if respond_to? :through_reflection
|
19
|
+
sort_many_to_many_entities(records)
|
20
|
+
else
|
21
|
+
sort_one_to_many_entities(records)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def sort_many_to_many_entities(records)
|
27
|
+
record_positions = struct_record_positions(records)
|
28
|
+
owner.send(through_reflection.name).each do |intermediate_entity|
|
29
|
+
new_position = record_positions[intermediate_entity.send(source_reflection.foreign_key)]
|
30
|
+
intermediate_entity.send(:"#{reflection.sortable_field}=", new_position)
|
31
|
+
intermediate_entity.save
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def sort_one_to_many_entities(records)
|
36
|
+
records.each_with_index do |record, index|
|
37
|
+
record.send(:"#{reflection.sortable_field}=", index + 1)
|
38
|
+
record.save
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def struct_record_positions(records)
|
43
|
+
record_positions = {}
|
44
|
+
records.each_with_index do |record, index|
|
45
|
+
record_positions[record.id] = index + 1
|
46
|
+
end
|
47
|
+
record_positions
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_sortable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vladislav Melanitskiy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: It adds possibility for sorting relation
|
28
|
+
email:
|
29
|
+
- co@rademade.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/activerecord_sortable.rb
|
35
|
+
- lib/activerecord_sortable/association.rb
|
36
|
+
- lib/activerecord_sortable/association_reflection.rb
|
37
|
+
- lib/activerecord_sortable/association_relation.rb
|
38
|
+
- lib/activerecord_sortable/has_many_association.rb
|
39
|
+
- lib/activerecord_sortable/version.rb
|
40
|
+
homepage: https://github.com/Rademade/activerecord_sortable
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.2.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Extension for active record.
|
64
|
+
test_files: []
|