acts_as_ordered 0.0.1.alpha
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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/acts_as_ordered.gemspec +21 -0
- data/lib/acts_as_ordered/version.rb +3 -0
- data/lib/acts_as_ordered.rb +37 -0
- metadata +77 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# ActsAsOrdered
|
2
|
+
|
3
|
+
Add 'ordered' scope to your ActiveRecord models
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add to your Gemfile and run the `bundle` command to install it.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "acts_as_ordered"
|
11
|
+
```
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Call `acts_as_ordered` in an ActiveRecord class.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class List < ActiveRecord::Base
|
21
|
+
acts_as_ordered
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
It is possible to pass the name of the attribute that will be used as the default ordering column as well as the direction.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
acts_as_ordered(:name)
|
29
|
+
acts_as_ordered(:name, :desc)
|
30
|
+
```
|
31
|
+
|
32
|
+
Then use the injected scope
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
List.ordered
|
36
|
+
List.ordered(:name)
|
37
|
+
List.ordered(:name, :desc)
|
38
|
+
```
|
39
|
+
|
40
|
+
## Development
|
41
|
+
|
42
|
+
Missing tests
|
43
|
+
|
44
|
+
Questions or problems? Please post them on the [issue tracker](https://github.com/semiweb/acts_as_ordered/issues). You can contribute changes by forking the project and submitting a pull request.
|
45
|
+
|
46
|
+
This gem is created by Semiweb and is under the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_ordered/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_ordered"
|
7
|
+
s.version = ActsAsOrdered::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Semiweb"]
|
10
|
+
s.email = []
|
11
|
+
s.homepage = "https://github.com/semiweb/acts_as_ordered"
|
12
|
+
s.summary = %q{Add ordered dynamic scope to models}
|
13
|
+
s.description = %q{}
|
14
|
+
|
15
|
+
s.rubyforge_project = "acts_as_ordered"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ActsAsOrdered
|
2
|
+
def acts_as_ordered(column=nil, direction=nil)
|
3
|
+
|
4
|
+
@acts_as_ordered = {}
|
5
|
+
|
6
|
+
if column
|
7
|
+
if self.column_names.include?(column.to_s)
|
8
|
+
@acts_as_ordered[:column] = column
|
9
|
+
else
|
10
|
+
raise 'Invalid Acts as Ordered default column'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
if direction
|
15
|
+
if %w(asc desc).include?(direction.to_s)
|
16
|
+
@acts_as_ordered[:direction] = direction
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
scope :ordered, lambda{|column=nil, direction=nil|
|
21
|
+
column ||= @acts_as_ordered[:column]
|
22
|
+
direction ||= @acts_as_ordered[:direction]
|
23
|
+
|
24
|
+
if column
|
25
|
+
if direction
|
26
|
+
order("#{column} #{direction}")
|
27
|
+
else
|
28
|
+
order("#{column}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ActiveRecord::Base
|
36
|
+
extend ActsAsOrdered
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_ordered
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 296151482
|
5
|
+
prerelease: true
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
- alpha
|
11
|
+
version: 0.0.1.alpha
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Semiweb
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-11-28 00:00:00 -05:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: ""
|
24
|
+
email: []
|
25
|
+
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- acts_as_ordered.gemspec
|
38
|
+
- lib/acts_as_ordered.rb
|
39
|
+
- lib/acts_as_ordered/version.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: https://github.com/semiweb/acts_as_ordered
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 25
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 3
|
67
|
+
- 1
|
68
|
+
version: 1.3.1
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: acts_as_ordered
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Add ordered dynamic scope to models
|
76
|
+
test_files: []
|
77
|
+
|