Sha256: b1dbbb012d315f116fdbd0525e740de385fa0d40862fb7f26778a1031dd145c7

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'spec_helper'

describe 'AjaxDatatablesRails::ORM::ActiveRecord#sort_records' do
  let(:view) { double('view', params: sample_params) }
  let(:datatable) { ComplexDatatable.new(view) }

  before(:each) do
    AjaxDatatablesRails.configure do |config|
      config.db_adapter = :sqlite
      config.orm = :active_record
    end

    User.create(username: 'johndoe', email: 'johndoe@example.com')
    User.create(username: 'msmith', email: 'mary.smith@example.com')
  end

  after(:each) do
    User.destroy_all
  end

  describe 'sort records' do
    let(:records) { User.all }

    it 'returns a records collection sorted by :order params' do
      # set to order Users by email in descending order
      datatable.params[:order]['0'] = { column: '1', dir: 'desc' }
      expect(datatable.send(:sort_records, records).map(&:email)).to match(
        ['mary.smith@example.com', 'johndoe@example.com']
      )
    end

    it 'can handle multiple sorting columns' do
      # set to order by Users username in ascending order, and
      # by Users email in descending order
      datatable.params[:order]['0'] = { column: '0', dir: 'asc' }
      datatable.params[:order]['1'] = { column: '1', dir: 'desc' }
      expect(datatable.send(:sort_records, records).to_sql).to include(
        "ORDER BY users.username ASC, users.email DESC"
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datatables-net-0.4.0 spec/ajax-datatables-rails/orm/active_record_sort_records_spec.rb