ar-orderable 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +1 -1
- data/VERSION +1 -1
- data/ar-orderable.gemspec +1 -5
- data/lib/ar-orderable.rb +3 -0
- metadata +16 -20
- data/LICENSE.txt +0 -20
- data/init.rb +0 -2
- data/lib/ar_orderable.rb +0 -126
data/MIT-LICENSE
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/ar-orderable.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-orderable}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Gatis Tomsons", "IT House"]
|
@@ -13,7 +13,6 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = %q{You can order AR records and skip callbacks}
|
14
14
|
s.email = %q{gatis@ithouse.cc}
|
15
15
|
s.extra_rdoc_files = [
|
16
|
-
"LICENSE.txt",
|
17
16
|
"README.md"
|
18
17
|
]
|
19
18
|
s.files = [
|
@@ -21,15 +20,12 @@ Gem::Specification.new do |s|
|
|
21
20
|
".rspec",
|
22
21
|
"Gemfile",
|
23
22
|
"Gemfile.lock",
|
24
|
-
"LICENSE.txt",
|
25
23
|
"MIT-LICENSE",
|
26
24
|
"README.md",
|
27
25
|
"Rakefile",
|
28
26
|
"VERSION",
|
29
27
|
"ar-orderable.gemspec",
|
30
|
-
"init.rb",
|
31
28
|
"lib/ar-orderable.rb",
|
32
|
-
"lib/ar_orderable.rb",
|
33
29
|
"spec/ar-orderable_spec.rb",
|
34
30
|
"spec/ar_orderable_spec.rb",
|
35
31
|
"spec/spec.opts",
|
data/lib/ar-orderable.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
$:<<File.dirname(__FILE__) unless $:.include?(File.dirname(__FILE__))
|
1
2
|
module ActiveRecord # :nodoc:
|
2
3
|
module Orderable # :nodoc:
|
3
4
|
def self.included(base) # :nodoc:
|
@@ -124,3 +125,5 @@ module ActiveRecord # :nodoc:
|
|
124
125
|
end
|
125
126
|
end
|
126
127
|
end
|
128
|
+
|
129
|
+
ActiveRecord::Base.class_eval { include ActiveRecord::Orderable }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-orderable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -15,7 +15,7 @@ default_executable:
|
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
18
|
-
requirement: &
|
18
|
+
requirement: &82064910 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: 3.0.0
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *82064910
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
|
-
requirement: &
|
29
|
+
requirement: &82064670 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ~>
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: 1.3.4
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *82064670
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: rspec
|
40
|
-
requirement: &
|
40
|
+
requirement: &82064430 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: 2.6.0
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *82064430
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: ruby-debug19
|
51
|
-
requirement: &
|
51
|
+
requirement: &82064190 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ~>
|
@@ -56,10 +56,10 @@ dependencies:
|
|
56
56
|
version: 0.11.6
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *82064190
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
61
|
name: bundler
|
62
|
-
requirement: &
|
62
|
+
requirement: &82063950 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - ~>
|
@@ -67,10 +67,10 @@ dependencies:
|
|
67
67
|
version: 1.0.0
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
|
-
version_requirements: *
|
70
|
+
version_requirements: *82063950
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: jeweler
|
73
|
-
requirement: &
|
73
|
+
requirement: &82063710 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ~>
|
@@ -78,10 +78,10 @@ dependencies:
|
|
78
78
|
version: 1.6.4
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
|
-
version_requirements: *
|
81
|
+
version_requirements: *82063710
|
82
82
|
- !ruby/object:Gem::Dependency
|
83
83
|
name: rcov
|
84
|
-
requirement: &
|
84
|
+
requirement: &82063470 !ruby/object:Gem::Requirement
|
85
85
|
none: false
|
86
86
|
requirements:
|
87
87
|
- - ! '>='
|
@@ -89,28 +89,24 @@ dependencies:
|
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
|
-
version_requirements: *
|
92
|
+
version_requirements: *82063470
|
93
93
|
description: You can order AR records and skip callbacks
|
94
94
|
email: gatis@ithouse.cc
|
95
95
|
executables: []
|
96
96
|
extensions: []
|
97
97
|
extra_rdoc_files:
|
98
|
-
- LICENSE.txt
|
99
98
|
- README.md
|
100
99
|
files:
|
101
100
|
- .document
|
102
101
|
- .rspec
|
103
102
|
- Gemfile
|
104
103
|
- Gemfile.lock
|
105
|
-
- LICENSE.txt
|
106
104
|
- MIT-LICENSE
|
107
105
|
- README.md
|
108
106
|
- Rakefile
|
109
107
|
- VERSION
|
110
108
|
- ar-orderable.gemspec
|
111
|
-
- init.rb
|
112
109
|
- lib/ar-orderable.rb
|
113
|
-
- lib/ar_orderable.rb
|
114
110
|
- spec/ar-orderable_spec.rb
|
115
111
|
- spec/ar_orderable_spec.rb
|
116
112
|
- spec/spec.opts
|
@@ -131,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
127
|
version: '0'
|
132
128
|
segments:
|
133
129
|
- 0
|
134
|
-
hash:
|
130
|
+
hash: -183352115
|
135
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
132
|
none: false
|
137
133
|
requirements:
|
data/LICENSE.txt
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2011 IT House, Gatis Tomsons
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/init.rb
DELETED
data/lib/ar_orderable.rb
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
module ActiveRecord # :nodoc:
|
2
|
-
module Orderable # :nodoc:
|
3
|
-
def self.included(base) # :nodoc:
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
attr_accessor :orderable_scope, :orderable_column, :skip_callbacks_for_orderable
|
9
|
-
|
10
|
-
# @:column [string] column name
|
11
|
-
# acts_as_orderable :column => "order_nr"
|
12
|
-
def acts_as_orderable options = {}
|
13
|
-
self.orderable_column = (options[:column] || "order_nr").to_s
|
14
|
-
self.skip_callbacks_for_orderable = options[:skip_callbacks]
|
15
|
-
if self.columns_hash.keys.include? self.orderable_column
|
16
|
-
self.orderable_scope = options[:scope]
|
17
|
-
self.before_save :pre_save_ordering
|
18
|
-
self.before_destroy :pre_destroy_ordering
|
19
|
-
self.default_scope :order => self.orderable_column
|
20
|
-
#self.validates_uniqueness_of self.orderable_column, :scope => @orderable_scope
|
21
|
-
include ActiveRecord::Orderable::InstanceMethods
|
22
|
-
else
|
23
|
-
msg = "[IMPORTANT] ActiveRecord::Orderable plugin: class #{self} has missing column '#{self.orderable_column}'"
|
24
|
-
puts msg if Rails.env == "development"
|
25
|
-
RAILS_DEFAULT_LOGGER.error msg
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# updates all unordered items puts them into the end of list
|
30
|
-
def order_unordered
|
31
|
-
self.reset_column_information # because before this usual 'add_column' is executed and the new column isn't fetched yet
|
32
|
-
self.group(self.orderable_scope).each do |obj|
|
33
|
-
unordered_conditions = "#{self.orderable_column} IS NULL OR #{self.table_name}.#{self.orderable_column} = 0"
|
34
|
-
ordered_conditions = "#{self.orderable_column} IS NOT NULL AND #{self.table_name}.#{self.orderable_column} != 0"
|
35
|
-
order_nr = obj.all_orderable.order(self.orderable_column).last[self.orderable_column] || 0
|
36
|
-
obj.all_orderable.where(unordered_conditions).each do |item|
|
37
|
-
order_nr += 1
|
38
|
-
raw_orderable_update(item.id, order_nr)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def raw_orderable_update id, nr
|
44
|
-
self.connection.execute("update #{self.table_name} set #{self.orderable_column} = #{nr.to_i} where #{self.table_name}.id = #{id.to_i};")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
module InstanceMethods
|
49
|
-
|
50
|
-
# Moves Item to given position
|
51
|
-
def move_to nr, options = {}
|
52
|
-
if options[:skip_callbacks].nil? ? self.class.skip_callbacks_for_orderable : options[:skip_callbacks]
|
53
|
-
self[self.class.orderable_column] = nr
|
54
|
-
self.send(:pre_save_ordering)
|
55
|
-
self.class.raw_orderable_update(self.id, nr)
|
56
|
-
else
|
57
|
-
self.update_attribute(self.class.orderable_column, nr)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def move_up options = {}
|
62
|
-
move_to(self[self.class.orderable_column] - 1, options) if self[self.class.orderable_column]
|
63
|
-
end
|
64
|
-
|
65
|
-
def move_down options = {}
|
66
|
-
move_to(self[self.class.orderable_column] + 1, options) if self[self.class.orderable_column]
|
67
|
-
end
|
68
|
-
|
69
|
-
def all_orderable
|
70
|
-
if self.class.orderable_scope
|
71
|
-
self.class.where(:"#{self.class.orderable_scope}" => self[self.class.orderable_scope])
|
72
|
-
else
|
73
|
-
self.class.scoped
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
def pre_save_ordering
|
80
|
-
self[self.class.orderable_column] = 0 if self[self.class.orderable_column].nil?
|
81
|
-
if self.id
|
82
|
-
if self[self.class.orderable_column] == 0
|
83
|
-
self[self.class.orderable_column] = 1
|
84
|
-
end
|
85
|
-
if self[self.class.orderable_column] > self.all_orderable.count
|
86
|
-
self[self.class.orderable_column] = self[self.class.orderable_column] -1
|
87
|
-
end
|
88
|
-
else
|
89
|
-
self[self.class.orderable_column] = self.all_orderable.count + 1 if self[self.class.orderable_column] == 0
|
90
|
-
end
|
91
|
-
self.all_orderable.where("#{self.class.table_name}.id != ?",self.id || 0).each do |item|
|
92
|
-
item[self.class.orderable_column] = 0 if item[self.class.orderable_column].nil?
|
93
|
-
if self.id
|
94
|
-
if item[self.class.orderable_column] > (self.send("#{self.class.orderable_column}_was") || 0 )
|
95
|
-
if item[self.class.orderable_column] <= self[self.class.orderable_column]
|
96
|
-
item[self.class.orderable_column] -= 1
|
97
|
-
end
|
98
|
-
else
|
99
|
-
if item[self.class.orderable_column] >= self[self.class.orderable_column]
|
100
|
-
item[self.class.orderable_column] += 1
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
if item[self.class.orderable_column] != item.send("#{self.class.orderable_column}_was")
|
105
|
-
self.class.raw_orderable_update(item.id, item[self.class.orderable_column])
|
106
|
-
end
|
107
|
-
else
|
108
|
-
if item[self.class.orderable_column] >= self[self.class.orderable_column]
|
109
|
-
item[self.class.orderable_column] += 1
|
110
|
-
self.class.raw_orderable_update(item.id, item[self.class.orderable_column])
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def pre_destroy_ordering
|
117
|
-
self.all_orderable.each do |item|
|
118
|
-
if item[self.class.orderable_column].to_i > self[self.class.orderable_column].to_i
|
119
|
-
item[self.class.orderable_column] -= 1
|
120
|
-
self.class.raw_orderable_update(item.id, item[self.class.orderable_column])
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|