Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

me_first

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

me_first

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

MeFirst

A straighforward extension for ActiveRecord models which makes it easy to reorder objects by a given column's values.

lemon-line

Getting Started

Add this line to your application's Gemfile:

gem 'me_first'

And then execute:

$ bundle

Or install it yourself as:

$ gem install me_first

Usage

Installation

To use MeFirst, you need to:

  1. Add an integer ordering column to your model.
  2. Require MeFirst in your model.
  3. Use MeFirst's attr_orderable method with the ordering column as an argument.

So:

$ rails g migration add_column_order_to_fake_models order:integer
$ rake db:migrate
class FakeModel
    include MeFirst
    attr_orderable :order
end

That's it! You're now ready to reorder by order (or whichever column name you want) at will.

Commands

MeFirst gives you a number of commands, named based on the given column. For the above example, you're given two scopes:

FakeModel.by_order
FakeModel.by_reverse_order

And a number of useful instance methods:

instance = FakeModel.first
instance.move_order_up!(2) # Moves the instance's order up by 2 places
instance.move_order_down!(1) # Moves the instance's order down by 1 place
instance.move_order_to_end!
instance.move_order_to_beginning!
instance.set_order!(5) # Sets the current instance's order to 5, and reorders other instances around it.

To get a clearer sense of what exactly these methods do, check out the specs.

Note that these methods are all dependent upon the column name:

class FakeModel
    include MeFirst
    attr_orderable :position
end

FakeModel.by_position
FakeModel.first.move_position_up!(5)

Performance Note

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.

Development

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.

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.

Contributing

  1. Fork it ( https://github.com/sashafklein/me_first/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

FAQs

Package last updated on 04 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc