ar-orderable 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/ar-orderable.gemspec +1 -1
- data/lib/ar-orderable.rb +13 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21bcc3926a7cbcadc327c1f008ca5e8aa95e954a
|
4
|
+
data.tar.gz: c0d61f2f0ab04dc166bdb5a01f13a22355624fd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea2627a7dd7bfe624ccda2af506c4863c9a72b86935c978d180651588cd87613991e4544ac076aab64b5650200cfb9cffce2b4b9fe1417658d04ad6bf09b78f9
|
7
|
+
data.tar.gz: 2843f200c252e198d48460e8adff5c4ef0f62dedca02f7273f45be74a81901c320b59f240dbccd3ab7efe1ac152c2fbdc058032a28b9842d1d0850aaeedf5d87
|
data/README.md
CHANGED
@@ -6,19 +6,21 @@ Rails 3 plugin for simple ordering.
|
|
6
6
|
|
7
7
|
Insert into your `Gemfile`:
|
8
8
|
|
9
|
-
gem
|
10
|
-
|
9
|
+
gem "ar-orderable"
|
10
|
+
|
11
11
|
then `bundle install`
|
12
12
|
|
13
13
|
### Setup
|
14
14
|
|
15
|
-
1. Add order field, like
|
16
|
-
2. In model add line
|
17
|
-
|
15
|
+
1. Add order field, like `order_nr` as integer
|
16
|
+
2. In model add line `acts_as_orderable` and if needed add options
|
17
|
+
- `column: "my_custom_order_field" # default it's order_nr`
|
18
|
+
- `scope: :locale # to order in some scope, you can add also as array [:locale, :some_type]`
|
19
|
+
3. If your table already has some rows of data then call the `YourModel.order_unordered` after adding new column.
|
18
20
|
|
19
21
|
Example migration:
|
20
22
|
|
21
|
-
add_column :categories, :order_nr, :integer
|
23
|
+
add_column :categories, :order_nr, :integer # change to your column name
|
22
24
|
Category.order_unordered # remove this for new table
|
23
25
|
add_index :categories, :order_nr
|
24
26
|
|
@@ -44,4 +46,4 @@ To skip model callbacks and just update order information you can specify `:skip
|
|
44
46
|
|
45
47
|
rspec spec # all examples should be green
|
46
48
|
|
47
|
-
Copyright (c) 2009 IT House, released under the MIT license
|
49
|
+
Copyright (c) 2009 IT House, released under the MIT license
|
data/ar-orderable.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "ar-orderable"
|
6
|
-
s.version = '1.0.
|
6
|
+
s.version = '1.0.6'
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["ITHouse (Latvia)", "Gatis Tomsons"]
|
9
9
|
s.email = "support@ithouse.lv"
|
data/lib/ar-orderable.rb
CHANGED
@@ -8,17 +8,18 @@ module ActiveRecord
|
|
8
8
|
attr_accessor :orderable_scope, :orderable_column, :skip_callbacks_for_orderable
|
9
9
|
|
10
10
|
# @:column [string] column name
|
11
|
+
# @:scope [string] column name to scope by
|
12
|
+
# @:scope Array[string] column names to scope by
|
11
13
|
# acts_as_orderable :column => "order_nr"
|
12
14
|
def acts_as_orderable options = {}
|
13
15
|
return unless self.connection.table_exists?(self.table_name)
|
14
16
|
self.orderable_column = (options[:column] || "order_nr").to_s
|
15
17
|
self.skip_callbacks_for_orderable = options[:skip_callbacks]
|
16
18
|
if self.columns_hash.keys.include? self.orderable_column
|
17
|
-
self.orderable_scope
|
19
|
+
self.orderable_scope = Array(options[:scope])
|
18
20
|
self.before_save :pre_save_ordering
|
19
21
|
self.before_destroy :pre_destroy_ordering
|
20
22
|
self.default_scope { order(self.orderable_column) }
|
21
|
-
#self.validates_uniqueness_of self.orderable_column, :scope => @orderable_scope
|
22
23
|
include ActiveRecord::Orderable::InstanceMethods
|
23
24
|
else
|
24
25
|
msg = "[IMPORTANT] ActiveRecord::Orderable plugin: class #{self} has missing column '#{self.orderable_column}'"
|
@@ -34,12 +35,12 @@ module ActiveRecord
|
|
34
35
|
|
35
36
|
# updates all unordered items puts them into the end of list
|
36
37
|
def order_unordered
|
37
|
-
self.reset_column_information
|
38
|
+
self.reset_column_information
|
38
39
|
self.group(self.orderable_scope).each do |obj|
|
39
40
|
unordered_conditions = "#{self.orderable_column} IS NULL OR #{self.table_name}.#{self.orderable_column} = 0"
|
40
41
|
ordered_conditions = "#{self.orderable_column} IS NOT NULL AND #{self.table_name}.#{self.orderable_column} != 0"
|
41
42
|
order_nr = obj.all_orderable.order(self.orderable_column).last[self.orderable_column] || 0
|
42
|
-
obj.all_orderable.where(unordered_conditions).
|
43
|
+
obj.all_orderable.where(unordered_conditions).find_each do |item|
|
43
44
|
order_nr += 1
|
44
45
|
raw_orderable_update(item.id, order_nr)
|
45
46
|
end
|
@@ -72,9 +73,14 @@ module ActiveRecord
|
|
72
73
|
move_to(self[self.class.orderable_column] + 1, options) if self[self.class.orderable_column]
|
73
74
|
end
|
74
75
|
|
76
|
+
# returns all elements in current scope
|
75
77
|
def all_orderable
|
76
|
-
if self.class.orderable_scope
|
77
|
-
self.class.
|
78
|
+
if self.class.orderable_scope.any?
|
79
|
+
scope = self.class.orderable_scope.inject({}) do |where, scope_name|
|
80
|
+
where[scope_name] = self[scope_name]
|
81
|
+
where
|
82
|
+
end
|
83
|
+
self.class.where(scope)
|
78
84
|
else
|
79
85
|
self.class.scoped
|
80
86
|
end
|
@@ -97,7 +103,7 @@ module ActiveRecord
|
|
97
103
|
end
|
98
104
|
|
99
105
|
return unless self.all_orderable.where("id != ? and #{column_name} = ?", self.id, self[column_name]).count > 0
|
100
|
-
self.all_orderable.where("#{self.class.table_name}.id != ?",self.id || 0).
|
106
|
+
self.all_orderable.where("#{self.class.table_name}.id != ?",self.id || 0).find_each do |item|
|
101
107
|
item[column_name] = 0 if item[column_name].nil?
|
102
108
|
if self.id
|
103
109
|
if item[column_name] > (self.send("#{column_name}_was") || 0 )
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ITHouse (Latvia)
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-12-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
71
|
version: '0'
|
72
72
|
requirements: []
|
73
73
|
rubyforge_project:
|
74
|
-
rubygems_version: 2.0.
|
74
|
+
rubygems_version: 2.0.3
|
75
75
|
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: You can order AR records and skip callbacks
|