activesorting 0.6.1 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 643af3294f6f2bf5df3160c798121a4d171faffd
4
- data.tar.gz: ea399cdc49501929b4ee0b16893ca830d69355aa
3
+ metadata.gz: b0f69021ec5e9e64e81c03fe39968216bb84c424
4
+ data.tar.gz: c2c94f2befce5784455af47bcf091eb1f11a0e5a
5
5
  SHA512:
6
- metadata.gz: af691cce0f3c649d9040cbfae395af5c3c020992679393182fd2b9bb16bdac572d46c28b2c7fcbc7307d0eacdc9b37469a671ba6852eef99662a2c15092b76d6
7
- data.tar.gz: 9c74a9416809a061bae119b0305b00f1afd0a234d6bf97820a70bbc314dc8b5f8f311301a316f600307d076b636692f2009f9704cb6ef237b16bdef2c57fc48a
6
+ metadata.gz: 96183ff9c864ef850a503612495dcac0089d21eebaa2061edc0629065bf3df7d77844ad7bec5995225aa1ac5d8db6ca35d5da26808e748a91effde0f10159aff
7
+ data.tar.gz: 923723716c2c1ba524dc8c4650e3a97fd0e6f147d091e7703623f752ba416f23978e9be0f25c00fe0d1ddb4e57bdeee15d94747b19008ddf1cedd7278dd579e0
@@ -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
 
@@ -1,3 +1,5 @@
1
+ require 'active_sorting/model'
2
+
1
3
  module ActiveSorting
2
4
  class Engine < Rails::Engine # :nodoc:
3
5
  ActiveSupport.on_load(:active_record) do
@@ -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, 'Sortable step should be of type Fixnum'
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, "Size mismatch between new (#{new_list.count}) and old (#{old_list.count}) array of items"
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
- # TODO: scopes
146
- max = self.class.unscoped.maximum(self.class.active_sorting_field)
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?
@@ -1,3 +1,3 @@
1
1
  module ActiveSorting
2
- VERSION = '0.6.1'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'rails/engine'
2
2
  require 'active_record'
3
3
  require 'active_sorting/version'
4
- require 'active_sorting/model'
5
4
  require 'active_sorting/engine'
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.6.1
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