activesorting 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/activesorting.gemspec +1 -0
- data/lib/active_sorting/engine.rb +2 -0
- data/lib/active_sorting/model.rb +27 -7
- data/lib/active_sorting/version.rb +1 -1
- data/lib/active_sorting.rb +0 -1
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0f69021ec5e9e64e81c03fe39968216bb84c424
|
4
|
+
data.tar.gz: c2c94f2befce5784455af47bcf091eb1f11a0e5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96183ff9c864ef850a503612495dcac0089d21eebaa2061edc0629065bf3df7d77844ad7bec5995225aa1ac5d8db6ca35d5da26808e748a91effde0f10159aff
|
7
|
+
data.tar.gz: 923723716c2c1ba524dc8c4650e3a97fd0e6f147d091e7703623f752ba416f23978e9be0f25c00fe0d1ddb4e57bdeee15d94747b19008ddf1cedd7278dd579e0
|
data/activesorting.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency 'rake', '~> 10.0'
|
23
23
|
spec.add_development_dependency 'rspec'
|
24
24
|
spec.add_development_dependency 'sqlite3'
|
25
|
+
spec.add_development_dependency 'database_cleaner'
|
25
26
|
spec.add_development_dependency 'coveralls'
|
26
27
|
spec.add_development_dependency 'appraisal'
|
27
28
|
|
data/lib/active_sorting/model.rb
CHANGED
@@ -20,6 +20,7 @@ module ActiveSorting
|
|
20
20
|
active_sorting_options[:name] = name
|
21
21
|
active_sorting_check_options
|
22
22
|
validates active_sorting_options[:name], presence: true
|
23
|
+
default_scope { active_sorting_default_scope }
|
23
24
|
before_validation :active_sorting_callback_before_validation
|
24
25
|
end
|
25
26
|
|
@@ -37,11 +38,20 @@ module ActiveSorting
|
|
37
38
|
# TODO: columns_hash breaks when database has no tables
|
38
39
|
# field_type = columns_hash[active_sorting_field.to_s].type
|
39
40
|
# unless field_type == :integer
|
40
|
-
# raise ArgumentError, "Sortable field should be of type Integer, #{field_type} given"
|
41
|
+
# raise ArgumentError, "Sortable field should be of type Integer, #{field_type} where given"
|
41
42
|
# end
|
42
43
|
unless active_sorting_step.is_a?(Fixnum)
|
43
|
-
raise ArgumentError,
|
44
|
+
raise ArgumentError, "Sortable step should be of type Fixnum, #{active_sorting_step.class.name} where given"
|
44
45
|
end
|
46
|
+
unless active_sorting_scope.respond_to?(:each)
|
47
|
+
raise ArgumentError, "Sortable step should be of type Enumerable, #{active_sorting_scope.class.name} where given"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def active_sorting_default_scope
|
52
|
+
conditions = {}
|
53
|
+
conditions[active_sorting_field] = active_sorting_order
|
54
|
+
order(conditions)
|
45
55
|
end
|
46
56
|
|
47
57
|
# Calculate the least possible changes required to
|
@@ -51,7 +61,7 @@ module ActiveSorting
|
|
51
61
|
def active_sorting_changes_required(old_list, new_list)
|
52
62
|
changes = []
|
53
63
|
if old_list.count != new_list.count
|
54
|
-
raise ArgumentError, "
|
64
|
+
raise ArgumentError, "Sortable new and old lists should be of the same length"
|
55
65
|
end
|
56
66
|
proposal1 = active_sorting_calculate_changes(old_list.dup, new_list.dup)
|
57
67
|
if proposal1.count >= (new_list.count / 4)
|
@@ -115,6 +125,10 @@ module ActiveSorting
|
|
115
125
|
def active_sorting_order
|
116
126
|
active_sorting_options[:order]
|
117
127
|
end
|
128
|
+
|
129
|
+
def active_sorting_scope
|
130
|
+
active_sorting_options[:scope]
|
131
|
+
end
|
118
132
|
end
|
119
133
|
|
120
134
|
def active_sorting_value
|
@@ -125,7 +139,7 @@ module ActiveSorting
|
|
125
139
|
send("#{self.class.active_sorting_field}=", new_value)
|
126
140
|
end
|
127
141
|
|
128
|
-
# Centers an item between the given two positions
|
142
|
+
# Centers an item between the where given two positions
|
129
143
|
def active_sorting_center_item(n1, n2)
|
130
144
|
delta = (n1 - n2).abs
|
131
145
|
smaller = [n1, n2].min
|
@@ -141,9 +155,15 @@ module ActiveSorting
|
|
141
155
|
|
142
156
|
# Generate the next stepping
|
143
157
|
def active_sorting_next_step
|
158
|
+
conditions = {}
|
159
|
+
self.class.active_sorting_scope.each do |s|
|
160
|
+
conditions[s] = send(s)
|
161
|
+
end
|
144
162
|
# Get the maximum value for the sortable field name
|
145
|
-
|
146
|
-
|
163
|
+
max = self.class
|
164
|
+
.unscoped
|
165
|
+
.where(conditions)
|
166
|
+
.maximum(self.class.active_sorting_field)
|
147
167
|
# First value will always be 0
|
148
168
|
return 0 if max.nil?
|
149
169
|
# Increment by the step value configured
|
@@ -151,7 +171,7 @@ module ActiveSorting
|
|
151
171
|
end
|
152
172
|
|
153
173
|
## Callbacks
|
154
|
-
# Generates a new code based on given options
|
174
|
+
# Generates a new code based on where given options
|
155
175
|
def active_sorting_callback_before_validation
|
156
176
|
field_name = self.class.active_sorting_field
|
157
177
|
send("#{field_name}=", active_sorting_next_step) if send(field_name).nil?
|
data/lib/active_sorting.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activesorting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Omar Abdel-Wahab
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: coveralls
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|