<commit_before>**ranked-model** is a modern row sorting library built for Rails 3.  It uses ARel aggressively and is better optimized than most other libraries.

Installation
------------

To install ranked-model, just add it to your `Gemfile`:

    gem 'ranked-model'
    
    # Or pin ranked-model to git
    # gem 'ranked-model',
    #   :git => '<EMAIL>:harvesthq/ranked-model.git'

Then use `bundle install` to update your `Gemfile.lock`.

Simple Use
----------

Use of ranked-model is straight ahead.  Get some ducks:

    class Duck < ActiveRecord::Base
    end

Put your ducks in a row:

    class Duck < ActiveRecord::Base
    
      include RankedModel
      ranks :row_order
    
    end

This simple example assumes an integer column called `row_order`.  To order Ducks by this order:

    Duck.rank(:row_order).all

The ranking integers stored in the `row_order` column will be big and spaced apart.  When you
implement a sorting UI, just update the resource with the position instead:

    @duck.update_attribute :row_order_position, 0  # or 1, 2, 37. :first and :last are also valid

So using a normal json controller where `@duck.attributes = params[:duck]; @duck.save`, JS can
look pretty elegant:

    $.ajax({
      type: 'PUT',
      url: '/ducks',
      dataType: 'json',
      data: { duck: { row_order_position: 0 } },  // or whatever your new position is
    });

Complex Use
-----------

The `ranks` method takes serveral arguments:

    class Duck < ActiveRecord::Base
    
      include RankedModel
    
      ranks :row_order,           # Name this ranker, used with rank()
        :column => :sort_order    # Override the default column, which defaults to the name
      
      belongs_to :pond
      ranks :swimming_order,
        :with_same => :pond_id    # Ducks belong_to Ponds, make the ranker scoped to one pond
      
      scope :walking, where(:walking => true )
      ranks :walking_order,
        :scope => :walking        # Narrow this ranker to a scope
    
    end

When you make a query, add the rank:

    Duck.rank(:row_order)
    
    Pond.first.ducks.rank(:swimming_order)
    
    Duck.walking.rank(:walking)<commit_msg>Add some github flavored markdown to the code snippets.<commit_after>**ranked-model** is a modern row sorting library built for Rails 3.  It uses ARel aggressively and is better optimized than most other libraries.

Installation
------------

To install ranked-model, just add it to your `Gemfile`:

``` ruby
gem 'ranked-model'

# Or pin ranked-model to git
# gem 'ranked-model',
#   :git => '<EMAIL>:harvesthq/ranked-model.git'
```

Then use `bundle install` to update your `Gemfile.lock`.

Simple Use
----------

Use of ranked-model is straight ahead.  Get some ducks:

``` ruby
class Duck < ActiveRecord::Base
end
```

Put your ducks in a row:

``` ruby
class Duck < ActiveRecord::Base

  include RankedModel
  ranks :row_order

end
```

This simple example assumes an integer column called `row_order`.  To order Ducks by this order:

``` ruby
Duck.rank(:row_order).all
```

The ranking integers stored in the `row_order` column will be big and spaced apart.  When you
implement a sorting UI, just update the resource with the position instead:

``` ruby
@duck.update_attribute :row_order_position, 0  # or 1, 2, 37. :first and :last are also valid
```

So using a normal json controller where `@duck.attributes = params[:duck]; @duck.save`, JS can
look pretty elegant:

``` javascript
$.ajax({
  type: 'PUT',
  url: '/ducks',
  dataType: 'json',
  data: { duck: { row_order_position: 0 } },  // or whatever your new position is
});
```

Complex Use
-----------

The `ranks` method takes serveral arguments:

``` ruby
class Duck < ActiveRecord::Base

  include RankedModel

  ranks :row_order,           # Name this ranker, used with rank()
    :column => :sort_order    # Override the default column, which defaults to the name
  
  belongs_to :pond
  ranks :swimming_order,
    :with_same => :pond_id    # Ducks belong_to Ponds, make the ranker scoped to one pond
  
  scope :walking, where(:walking => true )
  ranks :walking_order,
    :scope => :walking        # Narrow this ranker to a scope

end
```

When you make a query, add the rank:

``` ruby
Duck.rank(:row_order)

Pond.first.ducks.rank(:swimming_order)

Duck.walking.rank(:walking)
```
{'fraction_non_alphanumeric': 0.10944007403979639, 'fraction_numerical': 0.003239241092086997, 'mean_word_length': 3.025139664804469, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1908432', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1324, 'n_words': 503}