me_first 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +102 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/me_first.rb +76 -0
- data/lib/me_first/version.rb +3 -0
- data/me_first.gemspec +23 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f344df8032472b8574e58dbc953ad86ac53bd68a
|
4
|
+
data.tar.gz: 78dafc5eaa034b107ed24bb4d8d934426773275b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cfff622205e1d6f77a2e0759aa44fad400e4f4e6f5cc855fd0cd35dee31224f3c0fb1c765bcfb61155be00be1b160d4137d0041e0974726173c0032e478714c0
|
7
|
+
data.tar.gz: 5a47b8179b4853bde27120a3bb54c1ad0307e0f2ecbcd11f60f61d6ce96c692d1c91cc9d3e6311c0d2f477b0deb810fdf121cff7cf937918b53bc943df24120a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
|
2
|
+
# MeFirst
|
3
|
+
|
4
|
+
A straighforward extension for ActiveRecord models which makes it easy to reorder objects by a given column's values.
|
5
|
+
|
6
|
+
![lemon-line](http://dl.dropboxusercontent.com/s/kuwpzbcz5659umh/lemon-line.gif?dl=0)
|
7
|
+
|
8
|
+
## Getting Started
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'me_first'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install me_first
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Installation
|
27
|
+
|
28
|
+
To use MeFirst, you need to:
|
29
|
+
|
30
|
+
1. Add an `integer` ordering column to your model.
|
31
|
+
2. Require MeFirst in your model.
|
32
|
+
3. Use MeFirst's `attr_orderable` method with the ordering column as an argument.
|
33
|
+
|
34
|
+
So:
|
35
|
+
|
36
|
+
```bash
|
37
|
+
$ rails g migration add_column_order_to_fake_models order:integer
|
38
|
+
$ rake db:migrate
|
39
|
+
```
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class FakeModel
|
43
|
+
include MeFirst
|
44
|
+
attr_orderable :order
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
That's it! You're now ready to reorder by `order` (or whichever column name you want) at will.
|
49
|
+
|
50
|
+
|
51
|
+
### Commands
|
52
|
+
|
53
|
+
MeFirst gives you a number of commands, named based on the given column. For the above example, you're given two scopes:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
FakeModel.by_order
|
57
|
+
FakeModel.by_reverse_order
|
58
|
+
```
|
59
|
+
|
60
|
+
And a number of useful instance methods:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
instance = FakeModel.first
|
64
|
+
instance.move_order_up!(2) # Moves the instance's order up by 2 places
|
65
|
+
instance.move_order_down!(1) # Moves the instance's order down by 1 place
|
66
|
+
instance.move_order_to_end!
|
67
|
+
instance.move_order_to_beginning!
|
68
|
+
instance.set_order!(5) # Sets the current instance's order to 5, and reorders other instances around it.
|
69
|
+
```
|
70
|
+
|
71
|
+
> To get a clearer sense of what exactly these methods do, check out [the specs](https://github.com/sashafklein/me_first/tree/master/spec/me_first_spec.rb).
|
72
|
+
|
73
|
+
Note that these methods are all dependent upon the column name:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class FakeModel
|
77
|
+
include MeFirst
|
78
|
+
attr_orderable :position
|
79
|
+
end
|
80
|
+
|
81
|
+
FakeModel.by_position
|
82
|
+
FakeModel.first.move_position_up!(5)
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
### Performance Note
|
87
|
+
|
88
|
+
This works great for relatively small numbers of objects. When dealing with a large table of ordered objects, MeFirst (and ordering by column in general), will not be particularly performant -- resetting a single instance's order requires changing the order of all the objects which followed its initial location.
|
89
|
+
|
90
|
+
## Development
|
91
|
+
|
92
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
93
|
+
|
94
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it ( https://github.com/sashafklein/me_first/fork )
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "me_first"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/me_first.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'me_first/version'
|
2
|
+
|
3
|
+
module MeFirst
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def attr_orderable(*attrs)
|
8
|
+
attrs.each do |att|
|
9
|
+
|
10
|
+
metaclass.instance_eval do
|
11
|
+
define_method "by_#{att}" do
|
12
|
+
self.order(att => :asc)
|
13
|
+
end
|
14
|
+
|
15
|
+
define_method "by_reverse_#{att}" do
|
16
|
+
self.order(att => :desc)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class_eval do
|
21
|
+
define_method "move_#{att}_to_end!" do
|
22
|
+
others = self.class.send("by_#{att}").where.not(id: self.id).to_a
|
23
|
+
|
24
|
+
save_to_db( others.concat([self]), att)
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method "set_#{att}!" do |new_order|
|
28
|
+
new_order = 0 if new_order < 0
|
29
|
+
|
30
|
+
others = self.class.send("by_#{att}").where.not(id: self.id).to_a
|
31
|
+
|
32
|
+
if new_order > others.length + 1
|
33
|
+
self.send("move_#{att}_to_end!")
|
34
|
+
else
|
35
|
+
others.insert( new_order, self )
|
36
|
+
|
37
|
+
save_to_db( others, att )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method "move_#{att}_to_beginning!" do
|
42
|
+
send("set_#{att}!", 0)
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method "move_#{att}_up!" do |num=1|
|
46
|
+
send("set_#{att}!", self.order + num)
|
47
|
+
end
|
48
|
+
|
49
|
+
define_method "move_#{att}_down!" do |num=1|
|
50
|
+
send("set_#{att}!", self.order - num)
|
51
|
+
end
|
52
|
+
|
53
|
+
define_method "save_to_db" do |items, att|
|
54
|
+
ActiveRecord::Base.transaction do
|
55
|
+
items.compact.each_with_index do |item, index|
|
56
|
+
item.send( "#{att}=", index )
|
57
|
+
item.save!
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private :save_to_db
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def metaclass
|
69
|
+
class << self; self; end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.included(base)
|
74
|
+
base.extend ClassMethods
|
75
|
+
end
|
76
|
+
end
|
data/me_first.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'me_first/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "me_first"
|
8
|
+
spec.version = MeFirst::VERSION
|
9
|
+
spec.authors = ["Sasha Klein"]
|
10
|
+
spec.email = ["sashafklein@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A tool to centralize and simplify ActiveRecord reordering.}
|
13
|
+
spec.homepage = "https://www.github.com/sashafklein/me_first"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "sqlite3"
|
22
|
+
spec.add_development_dependency "binding_of_caller"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: me_first
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sasha Klein
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: binding_of_caller
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- sashafklein@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/me_first.rb
|
85
|
+
- lib/me_first/version.rb
|
86
|
+
- me_first.gemspec
|
87
|
+
homepage: https://www.github.com/sashafklein/me_first
|
88
|
+
licenses: []
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: A tool to centralize and simplify ActiveRecord reordering.
|
110
|
+
test_files: []
|