sortable-model 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +23 -0
- data/Rakefile +20 -0
- data/VERSION +1 -0
- data/sortable_model.rb +62 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2010 Jonathan Distad, Ehren Murdick, EdgeCase LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "sortable-model"
|
5
|
+
gemspec.summary = "Create ordering named-scopes for ActiveRecord models."
|
6
|
+
gemspec.description = <<-EOS
|
7
|
+
Sortable-model has a DSL for creating simple ordering named scopes based on
|
8
|
+
simple attributes or attributes of that model's associated objects.
|
9
|
+
It also provides a method for calling the named scopes directly from params
|
10
|
+
without the risk of arbitrary code execution.
|
11
|
+
EOS
|
12
|
+
gemspec.email = "github@edgecase.com"
|
13
|
+
gemspec.homepage = "http://github.com/edgecase/sortable_model"
|
14
|
+
gemspec.authors = ["Jon Distad", "Ehren Murdick"]
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/sortable_model.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module SortableModel
|
2
|
+
def self.extended(mod)
|
3
|
+
mod.module_eval do
|
4
|
+
@_sort_scopes = []
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def sorted_by name, *args
|
9
|
+
if @_sort_scopes.include?(name.to_sym)
|
10
|
+
send("_sorted_by_#{name}", *args)
|
11
|
+
else
|
12
|
+
raise NoMethodError, "Sort condition is not defined: #{name}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def can_sort_by name_or_hash, scope_args=nil
|
18
|
+
scope_args ||= generate_scope_args(name_or_hash)
|
19
|
+
generate_named_scope name_or_hash, scope_args
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_named_scope name_or_hash, scope_args
|
23
|
+
name = get_scope_name(name_or_hash)
|
24
|
+
@_sort_scopes << name.to_sym
|
25
|
+
named_scope "_sorted_by_#{name}", scope_args
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_scope_name name_or_hash
|
29
|
+
Hash === name_or_hash ? name_or_hash.keys.first : name_or_hash
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_scope_args name_or_hash
|
33
|
+
case name_or_hash
|
34
|
+
when Hash
|
35
|
+
name, order_col, table = scope_args_from_hash(name_or_hash)
|
36
|
+
lambda { |asc|
|
37
|
+
order = asc ? "ASC" : "DESC"
|
38
|
+
{ :order => "#{table}.#{order_col} #{order}", :include => name }
|
39
|
+
}
|
40
|
+
else
|
41
|
+
order_col = name_or_hash
|
42
|
+
table = quoted_table_name
|
43
|
+
lambda { |asc|
|
44
|
+
order = asc ? "ASC" : "DESC"
|
45
|
+
{ :order => "#{table}.#{order_col} #{order}" }
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def scope_args_from_hash hash
|
51
|
+
name = hash.keys.first
|
52
|
+
order_col = hash[name]
|
53
|
+
table = reflect_on_association(name.to_sym).quoted_table_name
|
54
|
+
[name, order_col, table]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
ActiveRecord::Base.instance_eval do
|
59
|
+
def sortable_model
|
60
|
+
extend SortableModel
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortable-model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jon Distad
|
8
|
+
- Ehren Murdick
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-02-03 00:00:00 -05:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: |
|
18
|
+
Sortable-model has a DSL for creating simple ordering named scopes based on
|
19
|
+
simple attributes or attributes of that model's associated objects.
|
20
|
+
It also provides a method for calling the named scopes directly from params
|
21
|
+
without the risk of arbitrary code execution.
|
22
|
+
|
23
|
+
email: github@edgecase.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
files:
|
31
|
+
- LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- VERSION
|
34
|
+
- sortable_model.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/edgecase/sortable_model
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Create ordering named-scopes for ActiveRecord models.
|
63
|
+
test_files: []
|
64
|
+
|