sortables 0.0.1
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 +17 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/config/database.yml +3 -0
- data/db/.gitkeep +0 -0
- data/lib/sortables/sortable.rb +22 -0
- data/lib/sortables/version.rb +3 -0
- data/lib/sortables.rb +14 -0
- data/sortables.gemspec +24 -0
- data/spec/.gitkeep +0 -0
- data/spec/sortable_spec.rb +33 -0
- data/spec/spec_no_rails_helper.rb +20 -0
- metadata +128 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.3@sortables
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joe Sak
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Sortables
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sortables'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sortables
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/config/database.yml
ADDED
data/db/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Sortables
|
2
|
+
module Sortable
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.send(:default_scope, :order => :position)
|
6
|
+
base.send(:before_create, :set_minimum_position)
|
7
|
+
base.attr_accessible :position
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def minimum_position
|
12
|
+
(maximum(:position) || -1) + 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def set_minimum_position
|
18
|
+
return unless position.blank?
|
19
|
+
self.position = self.class.minimum_position
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/sortables.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "sortables/version"
|
2
|
+
require "sortables/sortable"
|
3
|
+
|
4
|
+
module Sortables
|
5
|
+
module ClassMethods
|
6
|
+
def sortable
|
7
|
+
send :include, Sortables::Sortable
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
if defined?(ActiveRecord::Base)
|
12
|
+
ActiveRecord::Base.extend Sortables::ClassMethods
|
13
|
+
end
|
14
|
+
end
|
data/sortables.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sortables/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sortables"
|
8
|
+
gem.version = Sortables::VERSION
|
9
|
+
gem.authors = ["Joe Sak"]
|
10
|
+
gem.email = ["joe@neotericdesign.com"]
|
11
|
+
gem.description = %q{Easily add sorting by an integer field on your ActiveRecord models}
|
12
|
+
gem.summary = %q{(hopefully) painless positional sorting}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'activerecord', '3.2.9'
|
21
|
+
gem.add_development_dependency 'sqlite3'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
gem.add_development_dependency 'database_cleaner'
|
24
|
+
end
|
data/spec/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_no_rails_helper'
|
2
|
+
require 'sortables'
|
3
|
+
|
4
|
+
class SomeSuchClass < ActiveRecord::Base
|
5
|
+
sortable
|
6
|
+
end
|
7
|
+
|
8
|
+
class MigrateDatabase < ActiveRecord::Migration
|
9
|
+
unless table_exists?('some_such_classes')
|
10
|
+
create_table :some_such_classes do |t|
|
11
|
+
t.integer :position
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Sortables do
|
17
|
+
before { @class = SomeSuchClass }
|
18
|
+
|
19
|
+
it "sorts automatically" do
|
20
|
+
obj = @class.create!
|
21
|
+
obj2 = @class.create!
|
22
|
+
|
23
|
+
obj.position.should == 0
|
24
|
+
obj2.position.should == 1
|
25
|
+
end
|
26
|
+
|
27
|
+
it "default scopes by position ASC" do
|
28
|
+
obj = @class.create!(:position => 45)
|
29
|
+
obj2 = @class.create!(:position => 25)
|
30
|
+
|
31
|
+
@class.all.should == [obj2, obj]
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'database_cleaner'
|
3
|
+
|
4
|
+
db = YAML.load(File.open('config/database.yml'))['test']
|
5
|
+
ActiveRecord::Base.establish_connection(db)
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.before(:suite) do
|
9
|
+
DatabaseCleaner.strategy = :transaction
|
10
|
+
DatabaseCleaner.clean_with(:truncation)
|
11
|
+
end
|
12
|
+
|
13
|
+
config.before(:each) do
|
14
|
+
DatabaseCleaner.start
|
15
|
+
end
|
16
|
+
|
17
|
+
config.after(:each) do
|
18
|
+
DatabaseCleaner.clean
|
19
|
+
end
|
20
|
+
end unless defined?(Rails)
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortables
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joe Sak
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.9
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.9
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: database_cleaner
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Easily add sorting by an integer field on your ActiveRecord models
|
79
|
+
email:
|
80
|
+
- joe@neotericdesign.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- .rvmrc
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- config/database.yml
|
93
|
+
- db/.gitkeep
|
94
|
+
- lib/sortables.rb
|
95
|
+
- lib/sortables/sortable.rb
|
96
|
+
- lib/sortables/version.rb
|
97
|
+
- sortables.gemspec
|
98
|
+
- spec/.gitkeep
|
99
|
+
- spec/sortable_spec.rb
|
100
|
+
- spec/spec_no_rails_helper.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses: []
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 1.8.23
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: (hopefully) painless positional sorting
|
125
|
+
test_files:
|
126
|
+
- spec/.gitkeep
|
127
|
+
- spec/sortable_spec.rb
|
128
|
+
- spec/spec_no_rails_helper.rb
|