sorted-activerecord 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
 - data/Rakefile +1 -1
 - data/lib/sorted/{activerecord → active_record}/builder.rb +7 -0
 - data/lib/sorted/{activerecord → active_record}/helper.rb +3 -3
 - data/lib/sorted/{activerecord → active_record}/railtie.rb +2 -2
 - data/lib/sorted/active_record/version.rb +5 -0
 - data/lib/sorted/active_record.rb +8 -0
 - data/lib/sorted/activerecord.rb +1 -2
 - data/sorted-activerecord.gemspec +2 -2
 - data/spec/sorted/active_record_spec.rb +126 -0
 - metadata +9 -8
 - data/lib/sorted/activerecord/version.rb +0 -5
 - data/spec/sorted/activerecord_spec.rb +0 -91
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: dad88b26febe4a092e3832ecf4a981590629768e
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 59a3842b4e0a927c118054c0bca71cdb1259bbc5
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 5384beb8cc7de376e714fc8d5fcb8cc9feb75d354fa820ee2373fccb6d80a879b2418691c5ddc5a36d47969f87f0507ae39c7977e11c99b97a80c3900abf1671
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 922d9586a74b8345f5011fe48c8f5233cd05a78b682faf835e87c6344a9115da962d69360aa0eed9fbfe7abd20a6a5b5444402cfec5e651afcb20b6072b44216
         
     | 
    
        data/Rakefile
    CHANGED
    
    
| 
         @@ -8,6 +8,7 @@ module Sorted 
     | 
|
| 
       8 
8 
     | 
    
         
             
                  attr_reader :set
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                  def initialize(sort: [], order: [], whitelist: [])
         
     | 
| 
      
 11 
     | 
    
         
            +
                    @return_hash = true
         
     | 
| 
       11 
12 
     | 
    
         
             
                    uri = parse_sort(sort)
         
     | 
| 
       12 
13 
     | 
    
         
             
                    if whitelist.length > 0
         
     | 
| 
       13 
14 
     | 
    
         
             
                      uri = ::Sorted::Set.new(uri.select { |o| whitelist.include?(o[0]) })
         
     | 
| 
         @@ -32,6 +33,7 @@ module Sorted 
     | 
|
| 
       32 
33 
     | 
    
         
             
                    return ::Sorted::Set.new if order.nil?
         
     | 
| 
       33 
34 
     | 
    
         
             
                    case order.class.name
         
     | 
| 
       34 
35 
     | 
    
         
             
                    when 'String'
         
     | 
| 
      
 36 
     | 
    
         
            +
                      @return_hash = false
         
     | 
| 
       35 
37 
     | 
    
         
             
                      ::Sorted::SQLQuery.parse(order)
         
     | 
| 
       36 
38 
     | 
    
         
             
                    when 'Array'
         
     | 
| 
       37 
39 
     | 
    
         
             
                      parse(order)
         
     | 
| 
         @@ -46,6 +48,7 @@ module Sorted 
     | 
|
| 
       46 
48 
     | 
    
         
             
                      when 'Hash'
         
     | 
| 
       47 
49 
     | 
    
         
             
                        memo = memo + parse(value.to_a)
         
     | 
| 
       48 
50 
     | 
    
         
             
                      when 'String'
         
     | 
| 
      
 51 
     | 
    
         
            +
                        @return_hash = false
         
     | 
| 
       49 
52 
     | 
    
         
             
                        memo = memo + ::Sorted::SQLQuery.parse(value)
         
     | 
| 
       50 
53 
     | 
    
         
             
                      when 'Symbol'
         
     | 
| 
       51 
54 
     | 
    
         
             
                        memo = memo << [value.to_s, 'asc']
         
     | 
| 
         @@ -63,6 +66,10 @@ module Sorted 
     | 
|
| 
       63 
66 
     | 
    
         
             
                  def to_hash
         
     | 
| 
       64 
67 
     | 
    
         
             
                    @set.to_a.inject({}) { |a, e| a.merge(Hash[e[0].to_sym, e[1].to_sym]) }
         
     | 
| 
       65 
68 
     | 
    
         
             
                  end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
                  def to_sql
         
     | 
| 
      
 71 
     | 
    
         
            +
                    @return_hash ? to_hash : ::Sorted::SQLQuery.encode(@set)
         
     | 
| 
      
 72 
     | 
    
         
            +
                  end
         
     | 
| 
       66 
73 
     | 
    
         
             
                end
         
     | 
| 
       67 
74 
     | 
    
         
             
              end
         
     | 
| 
       68 
75 
     | 
    
         
             
            end
         
     | 
| 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'active_record'
         
     | 
| 
       2 
2 
     | 
    
         
             
            require 'active_support/concern'
         
     | 
| 
       3 
     | 
    
         
            -
            require 'sorted/ 
     | 
| 
      
 3 
     | 
    
         
            +
            require 'sorted/active_record/builder'
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            module Sorted
         
     | 
| 
       6 
6 
     | 
    
         
             
              module ActiveRecord
         
     | 
| 
         @@ -12,14 +12,14 @@ module Sorted 
     | 
|
| 
       12 
12 
     | 
    
         
             
                      builder = ::Sorted::ActiveRecord::Builder.new(sort: sort,
         
     | 
| 
       13 
13 
     | 
    
         
             
                                                                    order: order,
         
     | 
| 
       14 
14 
     | 
    
         
             
                                                                    whitelist: whitelist)
         
     | 
| 
       15 
     | 
    
         
            -
                      order(builder. 
     | 
| 
      
 15 
     | 
    
         
            +
                      order(builder.to_sql)
         
     | 
| 
       16 
16 
     | 
    
         
             
                    end
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
18 
     | 
    
         
             
                    def self.resorted(sort: [], order: [], whitelist: [])
         
     | 
| 
       19 
19 
     | 
    
         
             
                      builder = ::Sorted::ActiveRecord::Builder.new(sort: sort,
         
     | 
| 
       20 
20 
     | 
    
         
             
                                                                    order: order,
         
     | 
| 
       21 
21 
     | 
    
         
             
                                                                    whitelist: whitelist)
         
     | 
| 
       22 
     | 
    
         
            -
                      reorder(builder. 
     | 
| 
      
 22 
     | 
    
         
            +
                      reorder(builder.to_sql)
         
     | 
| 
       23 
23 
     | 
    
         
             
                    end
         
     | 
| 
       24 
24 
     | 
    
         
             
                  end
         
     | 
| 
       25 
25 
     | 
    
         
             
                end
         
     | 
| 
         @@ -1,11 +1,11 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'rails'
         
     | 
| 
       2 
2 
     | 
    
         
             
            require 'rails/railtie'
         
     | 
| 
       3 
     | 
    
         
            -
            require 'sorted/ 
     | 
| 
      
 3 
     | 
    
         
            +
            require 'sorted/active_record/helper'
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            module Sorted
         
     | 
| 
       6 
6 
     | 
    
         
             
              module ActiveRecord
         
     | 
| 
       7 
7 
     | 
    
         
             
                class Railtie < ::Rails::Railtie
         
     | 
| 
       8 
     | 
    
         
            -
                   
     | 
| 
      
 8 
     | 
    
         
            +
                  initializer 'sorted' do |_app|
         
     | 
| 
       9 
9 
     | 
    
         
             
                    ActiveSupport.on_load(:active_record) do
         
     | 
| 
       10 
10 
     | 
    
         
             
                      include Sorted::ActiveRecord::Helper
         
     | 
| 
       11 
11 
     | 
    
         
             
                    end
         
     | 
    
        data/lib/sorted/activerecord.rb
    CHANGED
    
    | 
         @@ -1,2 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require 'sorted/ 
     | 
| 
       2 
     | 
    
         
            -
            require 'sorted/activerecord/railtie' if defined?(Rails::Railtie)
         
     | 
| 
      
 1 
     | 
    
         
            +
            require 'sorted/active_record'
         
     | 
    
        data/sorted-activerecord.gemspec
    CHANGED
    
    | 
         @@ -1,11 +1,11 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # coding: utf-8
         
     | 
| 
       2 
2 
     | 
    
         
             
            lib = File.expand_path('../lib', __FILE__)
         
     | 
| 
       3 
3 
     | 
    
         
             
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         
     | 
| 
       4 
     | 
    
         
            -
            require 'sorted/ 
     | 
| 
      
 4 
     | 
    
         
            +
            require 'sorted/active_record/version'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            Gem::Specification.new do |spec|
         
     | 
| 
       7 
7 
     | 
    
         
             
              spec.name          = 'sorted-activerecord'
         
     | 
| 
       8 
     | 
    
         
            -
              spec.version       = Sorted:: 
     | 
| 
      
 8 
     | 
    
         
            +
              spec.version       = Sorted::ActiveRecord::VERSION
         
     | 
| 
       9 
9 
     | 
    
         
             
              spec.authors       = ['Rufus Post']
         
     | 
| 
       10 
10 
     | 
    
         
             
              spec.email         = ['Rufus.Post@team.telstra.com']
         
     | 
| 
       11 
11 
     | 
    
         
             
              spec.summary       = 'Activerecord scoped for sorted.'
         
     | 
| 
         @@ -0,0 +1,126 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'active_record'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'sorted/active_record/helper'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            ActiveRecord::Base.send(:include, Sorted::ActiveRecord::Helper)
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            describe Sorted::ActiveRecord do
         
     | 
| 
      
 8 
     | 
    
         
            +
              class Post < ActiveRecord::Base
         
     | 
| 
      
 9 
     | 
    
         
            +
                establish_connection adapter: 'sqlite3', database: ':memory:'
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                connection.create_table table_name, force: true do |t|
         
     | 
| 
      
 12 
     | 
    
         
            +
                  t.string :name
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                belongs_to :user
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                scope :page, -> { limit(50) }
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              class User < ActiveRecord::Base
         
     | 
| 
      
 21 
     | 
    
         
            +
                establish_connection adapter: 'sqlite3', database: ':memory:'
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                connection.create_table table_name, force: true do |t|
         
     | 
| 
      
 24 
     | 
    
         
            +
                  t.string :name
         
     | 
| 
      
 25 
     | 
    
         
            +
                end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                has_many :posts
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                scope :page, -> { limit(50) }
         
     | 
| 
      
 30 
     | 
    
         
            +
              end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              let(:subject) { Post }
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
              it 'should integrate with ActiveRecord::Base sorted' do
         
     | 
| 
      
 35 
     | 
    
         
            +
                expect(subject.respond_to?(:sorted)).to eq(true)
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
              it 'should integrate with ActiveRecord::Base resorted' do
         
     | 
| 
      
 39 
     | 
    
         
            +
                expect(subject.respond_to?(:resorted)).to eq(true)
         
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
              it 'should integrate with ActiveRecord::Relation sorted' do
         
     | 
| 
      
 43 
     | 
    
         
            +
                expect(subject.references(:user).includes(:user).respond_to?(:sorted)).to eq(true)
         
     | 
| 
      
 44 
     | 
    
         
            +
              end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
              it 'should allow symbols for order' do
         
     | 
| 
      
 47 
     | 
    
         
            +
                expected = subject.order(:created_at).to_sql
         
     | 
| 
      
 48 
     | 
    
         
            +
                actual = subject.sorted(order: [:created_at]).to_sql
         
     | 
| 
      
 49 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
              it 'should allow strings for order' do
         
     | 
| 
      
 53 
     | 
    
         
            +
                expected = subject.order('created_at').to_sql
         
     | 
| 
      
 54 
     | 
    
         
            +
                actual = subject.sorted(order: ['created_at']).to_sql
         
     | 
| 
      
 55 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 56 
     | 
    
         
            +
              end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
              it 'should allow hash as order' do
         
     | 
| 
      
 59 
     | 
    
         
            +
                expected = subject.order({ created_at: :desc }).to_sql
         
     | 
| 
      
 60 
     | 
    
         
            +
                actual = subject.sorted(order: [{ created_at: :desc }]).to_sql
         
     | 
| 
      
 61 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 62 
     | 
    
         
            +
              end
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
              it 'should allow hash as sort' do
         
     | 
| 
      
 65 
     | 
    
         
            +
                expected = subject.order({ created_at: :desc }).to_sql
         
     | 
| 
      
 66 
     | 
    
         
            +
                actual = subject.sorted(sort: [{ created_at: :desc }]).to_sql
         
     | 
| 
      
 67 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 68 
     | 
    
         
            +
              end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
              it 'should allow symbols and hash as order' do
         
     | 
| 
      
 71 
     | 
    
         
            +
                expected = subject.order(:orders_count, { created_at: :desc }).to_sql
         
     | 
| 
      
 72 
     | 
    
         
            +
                actual = subject.sorted(sort: [:orders_count, { created_at: :desc }]).to_sql
         
     | 
| 
      
 73 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 74 
     | 
    
         
            +
              end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
              it 'should allow sql' do
         
     | 
| 
      
 77 
     | 
    
         
            +
                expected = subject.order('orders_count ASC, created_at DESC').to_sql
         
     | 
| 
      
 78 
     | 
    
         
            +
                actual = subject.sorted(order: 'orders_count ASC, created_at DESC').to_sql
         
     | 
| 
      
 79 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 80 
     | 
    
         
            +
              end
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
              it 'should allow uri' do
         
     | 
| 
      
 83 
     | 
    
         
            +
                expected = subject.order(orders_count: :asc, created_at: :desc).to_sql
         
     | 
| 
      
 84 
     | 
    
         
            +
                actual = subject.sorted(sort: 'orders_count_asc!created_at_desc').to_sql
         
     | 
| 
      
 85 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 86 
     | 
    
         
            +
              end
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
              it 'should allow an array of sql' do
         
     | 
| 
      
 89 
     | 
    
         
            +
                expected = subject.order('orders_count ASC', 'created_at DESC').to_sql
         
     | 
| 
      
 90 
     | 
    
         
            +
                actual = subject.sorted(order: ['orders_count ASC', 'created_at DESC']).to_sql
         
     | 
| 
      
 91 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 92 
     | 
    
         
            +
              end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
              it 'should play nice with other scopes' do
         
     | 
| 
      
 95 
     | 
    
         
            +
                expected = subject.where(name: 'bob').page.order('name ASC').to_sql
         
     | 
| 
      
 96 
     | 
    
         
            +
                actual = subject.where(name: 'bob').page.sorted(order: 'name ASC').to_sql
         
     | 
| 
      
 97 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 98 
     | 
    
         
            +
              end
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
              it 'should override the provided order' do
         
     | 
| 
      
 101 
     | 
    
         
            +
                expected = subject.page.where(name: 'bob').order(:id).order('name DESC').reorder('name ASC').to_sql
         
     | 
| 
      
 102 
     | 
    
         
            +
                actual = subject.page.where(name: 'bob').order(:id).sorted(order: 'name DESC').resorted(order: 'name ASC').to_sql
         
     | 
| 
      
 103 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 104 
     | 
    
         
            +
              end
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
              it 'should work with whitelist' do
         
     | 
| 
      
 107 
     | 
    
         
            +
                expected = subject.order(created_at: :desc).to_sql
         
     | 
| 
      
 108 
     | 
    
         
            +
                expect(subject.sorted(sort: 'orders_count_asc!created_at_desc',
         
     | 
| 
      
 109 
     | 
    
         
            +
                                      whitelist: %(created_at)).to_sql).to match(expected)
         
     | 
| 
      
 110 
     | 
    
         
            +
              end
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
              it 'should not parse custom order strings' do
         
     | 
| 
      
 113 
     | 
    
         
            +
                relation = subject.references(:user).includes(:user)
         
     | 
| 
      
 114 
     | 
    
         
            +
                expected = relation.order('users.name ASC').to_sql
         
     | 
| 
      
 115 
     | 
    
         
            +
                actual = relation.sorted(order: 'users.name ASC').to_sql
         
     | 
| 
      
 116 
     | 
    
         
            +
                expect(actual).to match(expected)
         
     | 
| 
      
 117 
     | 
    
         
            +
              end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
              it 'should not raise error with nil value' do
         
     | 
| 
      
 120 
     | 
    
         
            +
                expect(-> { subject.sorted(sort: nil) }).to_not raise_error
         
     | 
| 
      
 121 
     | 
    
         
            +
              end
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
              it 'should not raise error with empty string' do
         
     | 
| 
      
 124 
     | 
    
         
            +
                expect(-> { subject.sorted(sort: '') }).to_not raise_error
         
     | 
| 
      
 125 
     | 
    
         
            +
              end
         
     | 
| 
      
 126 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: sorted-activerecord
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Rufus Post
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2015- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2015-04-11 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activerecord
         
     | 
| 
         @@ -166,13 +166,14 @@ files: 
     | 
|
| 
       166 
166 
     | 
    
         
             
            - LICENSE.txt
         
     | 
| 
       167 
167 
     | 
    
         
             
            - README.md
         
     | 
| 
       168 
168 
     | 
    
         
             
            - Rakefile
         
     | 
| 
      
 169 
     | 
    
         
            +
            - lib/sorted/active_record.rb
         
     | 
| 
      
 170 
     | 
    
         
            +
            - lib/sorted/active_record/builder.rb
         
     | 
| 
      
 171 
     | 
    
         
            +
            - lib/sorted/active_record/helper.rb
         
     | 
| 
      
 172 
     | 
    
         
            +
            - lib/sorted/active_record/railtie.rb
         
     | 
| 
      
 173 
     | 
    
         
            +
            - lib/sorted/active_record/version.rb
         
     | 
| 
       169 
174 
     | 
    
         
             
            - lib/sorted/activerecord.rb
         
     | 
| 
       170 
     | 
    
         
            -
            - lib/sorted/activerecord/builder.rb
         
     | 
| 
       171 
     | 
    
         
            -
            - lib/sorted/activerecord/helper.rb
         
     | 
| 
       172 
     | 
    
         
            -
            - lib/sorted/activerecord/railtie.rb
         
     | 
| 
       173 
     | 
    
         
            -
            - lib/sorted/activerecord/version.rb
         
     | 
| 
       174 
175 
     | 
    
         
             
            - sorted-activerecord.gemspec
         
     | 
| 
       175 
     | 
    
         
            -
            - spec/sorted/ 
     | 
| 
      
 176 
     | 
    
         
            +
            - spec/sorted/active_record_spec.rb
         
     | 
| 
       176 
177 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
       177 
178 
     | 
    
         
             
            homepage: ''
         
     | 
| 
       178 
179 
     | 
    
         
             
            licenses:
         
     | 
| 
         @@ -199,5 +200,5 @@ signing_key: 
     | 
|
| 
       199 
200 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       200 
201 
     | 
    
         
             
            summary: Activerecord scoped for sorted.
         
     | 
| 
       201 
202 
     | 
    
         
             
            test_files:
         
     | 
| 
       202 
     | 
    
         
            -
            - spec/sorted/ 
     | 
| 
      
 203 
     | 
    
         
            +
            - spec/sorted/active_record_spec.rb
         
     | 
| 
       203 
204 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
         @@ -1,91 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require 'spec_helper'
         
     | 
| 
       2 
     | 
    
         
            -
            require 'active_record'
         
     | 
| 
       3 
     | 
    
         
            -
            require 'sorted/activerecord/helper'
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
            ActiveRecord::Base.send(:include, Sorted::ActiveRecord::Helper)
         
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
            describe Sorted::ActiveRecord do
         
     | 
| 
       8 
     | 
    
         
            -
              class SortedActiveRecordTest < ActiveRecord::Base
         
     | 
| 
       9 
     | 
    
         
            -
                establish_connection adapter: 'sqlite3', database: ':memory:'
         
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
                connection.create_table table_name, force: true do |t|
         
     | 
| 
       12 
     | 
    
         
            -
                  t.string :name
         
     | 
| 
       13 
     | 
    
         
            -
                end
         
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
                scope :page, -> { limit(50) }
         
     | 
| 
       16 
     | 
    
         
            -
              end
         
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
              let(:subject) { SortedActiveRecordTest }
         
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
              it 'should integrate with ActiveRecord::Base' do
         
     | 
| 
       21 
     | 
    
         
            -
                expect(subject.respond_to?(:sorted)).to eq(true)
         
     | 
| 
       22 
     | 
    
         
            -
                expect(subject.respond_to?(:resorted)).to eq(true)
         
     | 
| 
       23 
     | 
    
         
            -
              end
         
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
              it 'should allow symbols and strings order' do
         
     | 
| 
       26 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."created_at" ASC'
         
     | 
| 
       27 
     | 
    
         
            -
                expect(subject.sorted(order: [:created_at]).to_sql).to match(order_by)
         
     | 
| 
       28 
     | 
    
         
            -
                expect(subject.sorted(order: ['created_at']).to_sql).to match(order_by)
         
     | 
| 
       29 
     | 
    
         
            -
              end
         
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
              it 'should allow symbols and strings as sort' do
         
     | 
| 
       32 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."created_at" ASC'
         
     | 
| 
       33 
     | 
    
         
            -
                expect(subject.sorted(sort: [:created_at]).to_sql).to match(order_by)
         
     | 
| 
       34 
     | 
    
         
            -
                expect(subject.sorted(sort: ['created_at']).to_sql).to match(order_by)
         
     | 
| 
       35 
     | 
    
         
            -
              end
         
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
              it 'should allow hash as order' do
         
     | 
| 
       38 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       39 
     | 
    
         
            -
                expect(subject.sorted(order: [{ created_at: :desc }]).to_sql).to match(order_by)
         
     | 
| 
       40 
     | 
    
         
            -
              end
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
              it 'should allow hash as sort' do
         
     | 
| 
       43 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       44 
     | 
    
         
            -
                expect(subject.sorted(sort: [{ created_at: :desc }]).to_sql).to match(order_by)
         
     | 
| 
       45 
     | 
    
         
            -
              end
         
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
              it 'should allow symbols and hash as order' do
         
     | 
| 
       48 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."orders_count" ASC, "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       49 
     | 
    
         
            -
                expect(subject.sorted(order: [:orders_count, { created_at: :desc }]).to_sql).to match(order_by)
         
     | 
| 
       50 
     | 
    
         
            -
              end
         
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
       52 
     | 
    
         
            -
              it 'should allow sql' do
         
     | 
| 
       53 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."orders_count" ASC, "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       54 
     | 
    
         
            -
                expect(subject.sorted(order: 'orders_count ASC, created_at DESC').to_sql).to match(order_by)
         
     | 
| 
       55 
     | 
    
         
            -
              end
         
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
              it 'should allow uri' do
         
     | 
| 
       58 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."orders_count" ASC, "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       59 
     | 
    
         
            -
                expect(subject.sorted(sort: 'orders_count_asc!created_at_desc').to_sql).to match(order_by)
         
     | 
| 
       60 
     | 
    
         
            -
              end
         
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
              it 'should allow an array of sql' do
         
     | 
| 
       63 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."orders_count" ASC, "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       64 
     | 
    
         
            -
                expect(subject.sorted(order: ['orders_count ASC', 'created_at DESC']).to_sql).to match(order_by)
         
     | 
| 
       65 
     | 
    
         
            -
              end
         
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
       67 
     | 
    
         
            -
              it 'should play nice with other scopes' do
         
     | 
| 
       68 
     | 
    
         
            -
                sql = "SELECT  \"sorted_active_record_tests\".* FROM \"sorted_active_record_tests\" WHERE \"sorted_active_record_tests\".\"name\" = 'bob'  ORDER BY \"sorted_active_record_tests\".\"name\" ASC LIMIT 50"
         
     | 
| 
       69 
     | 
    
         
            -
                expect(subject.where(name: 'bob').page.sorted(order: 'name ASC').to_sql).to eq(sql)
         
     | 
| 
       70 
     | 
    
         
            -
                expect(subject.page.sorted(order: 'name ASC').where(name: 'bob').to_sql).to eq(sql)
         
     | 
| 
       71 
     | 
    
         
            -
              end
         
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
              it 'should override the provided order' do
         
     | 
| 
       74 
     | 
    
         
            -
                sql = "SELECT  \"sorted_active_record_tests\".* FROM \"sorted_active_record_tests\" WHERE \"sorted_active_record_tests\".\"name\" = 'bob'  ORDER BY \"sorted_active_record_tests\".\"name\" ASC LIMIT 50"
         
     | 
| 
       75 
     | 
    
         
            -
                expect(subject.page.where(name: 'bob').order(:id).sorted(order: 'name DESC').resorted(order: 'name ASC').to_sql).to eq(sql)
         
     | 
| 
       76 
     | 
    
         
            -
              end
         
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
              it 'should work with whitelist' do
         
     | 
| 
       79 
     | 
    
         
            -
                order_by = 'ORDER BY "sorted_active_record_tests"."created_at" DESC'
         
     | 
| 
       80 
     | 
    
         
            -
                expect(subject.sorted(sort: 'orders_count_asc!created_at_desc',
         
     | 
| 
       81 
     | 
    
         
            -
                                      whitelist: %(created_at)).to_sql).to match(order_by)
         
     | 
| 
       82 
     | 
    
         
            -
              end
         
     | 
| 
       83 
     | 
    
         
            -
             
     | 
| 
       84 
     | 
    
         
            -
              it 'should not raise error with nil value' do
         
     | 
| 
       85 
     | 
    
         
            -
                expect(-> { subject.sorted(sort: nil) }).to_not raise_error
         
     | 
| 
       86 
     | 
    
         
            -
              end
         
     | 
| 
       87 
     | 
    
         
            -
             
     | 
| 
       88 
     | 
    
         
            -
              it 'should not raise error with empty string' do
         
     | 
| 
       89 
     | 
    
         
            -
                expect(-> { subject.sorted(sort: '') }).to_not raise_error
         
     | 
| 
       90 
     | 
    
         
            -
              end
         
     | 
| 
       91 
     | 
    
         
            -
            end
         
     |