active_record_sorting 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2e7222d5e6379d7a1c1375cd90f25e3a36be2f1
4
+ data.tar.gz: a5afbea4b3def66de8bb6b1aa63e53c5711f6c59
5
+ SHA512:
6
+ metadata.gz: f20ad9907cf243c055ef785e38206dbddef16542416cad19dcd6f976ab181e4ad1195052f99ccaffa446aac431c7e34a3b594a9fa3d1e7e777e493ca452d5bf5
7
+ data.tar.gz: 22867b2abe95559e6d051b40e74ce1c7de04a6e5f3d696e75453be81fcdc9fa406c39e975ca851fea3e90f06a241eac9e4331b1914e25139ebb63d2aa785d4ce
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record_sorting.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 yratanov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # ActiveRecordSorting
2
+
3
+ Simple gem to sort your records keeping model clean
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_record_sorting'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_record_sorting
18
+
19
+ ## Why?
20
+
21
+ Usually when your application grows your models become a mess of different kind of logic (scopes, auth, everything).
22
+ And usually you need to provide an API for sorting.
23
+ Of course you add some magic Concerns and use it like this `User.sort(order)` and add custom orders' logic right in your model class.
24
+ But if you care about you code being clean and follow single responsibility principle then you need this gem to extract sorting logic out of the model.
25
+
26
+ ## Usage
27
+
28
+ 1. Create sorting class:
29
+ ```ruby
30
+ # app/sortings
31
+ require 'active_record_sorting/base'
32
+
33
+ class UserSorting < ActiveRecordSorting::Base
34
+ named_sorting_orders :full_name
35
+
36
+ def full_name(order)
37
+ scope.order(first_name: order, last_name: order)
38
+ end
39
+ end
40
+ ```
41
+ 2. In your controller:
42
+ ```ruby
43
+ def index
44
+ @users = UserSorting.sort(User, params[:sort]).page(params[:page])
45
+ end
46
+ ```
47
+ Possible values of `sort` param: `'id_asc', 'created_at_desc', 'relation.column_asc', 'named_order_asc'`
48
+
49
+ You can also define basic sorting class if you don't want to create it for each model:
50
+
51
+ ```ruby
52
+ # app/sorting/sorting
53
+
54
+ require 'active_record_sorting/base'
55
+
56
+ class Sorting < ActiveRecordSorting::Base
57
+ end
58
+ ```
59
+
60
+ Usage:
61
+
62
+ ```ruby
63
+ Sorting.sort(User, params[:sort])
64
+
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record_sorting/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record_sorting"
8
+ spec.version = ActiveRecordSorting::VERSION
9
+ spec.authors = ["yratanov"]
10
+ spec.email = ["organium@gmail.com"]
11
+ spec.description = 'Simple gem for Rails Active Record sorting'
12
+ spec.summary = 'Extract you sorting logic to separate object'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "activerecord"
25
+ spec.add_development_dependency "sqlite3"
26
+ end
@@ -0,0 +1,70 @@
1
+ require 'active_support/core_ext/class/attribute_accessors'
2
+
3
+ module ActiveRecordSorting
4
+ class Base
5
+ cattr_accessor(:sorting_orders) { [] }
6
+
7
+ attr_accessor :scope
8
+
9
+ # If simple order like "id_asc" is not enough, you can create complicated orders using this method.
10
+ # @param sortings Array
11
+ def self.named_sorting_orders(*sortings)
12
+ self.sorting_orders = sortings
13
+ end
14
+
15
+ def initialize(scope)
16
+ @scope = scope
17
+ end
18
+
19
+ def self.sort(scope, order)
20
+ new(scope).sort(order)
21
+ end
22
+
23
+ # Parses +sort_order+, applies sorting to scope and return it.
24
+ # @param sort_order String Examples: 'id_asc', 'created_at_desc', 'relation.column_asc', 'named_order_asc'
25
+ def sort(sort_order)
26
+ return scope unless sort_order.present?
27
+
28
+ column, direction = parse_order sort_order
29
+
30
+ if sorting_orders.include? column.to_sym
31
+ send column.to_sym, direction
32
+ elsif scope.column_names.include?(column)
33
+ column_sorting(column, direction)
34
+ else
35
+ relation_sorting(column, direction)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def sorting_orders
42
+ self.class.sorting_orders
43
+ end
44
+
45
+ def parse_order(sort_order)
46
+ sort_array = sort_order.split('_')
47
+ direction = sort_array.pop
48
+ unless %w(desc asc).include? direction.downcase
49
+ raise "Invalid direction '#{direction}'"
50
+ end
51
+ column = sort_array.join('_')
52
+ [column, direction]
53
+ end
54
+
55
+ def relation_sorting(column, direction)
56
+ relation, attribute = column.split '.'
57
+ association = scope.reflect_on_association(relation.to_sym)
58
+ if association.present?
59
+ scope.includes(association.name).
60
+ order("#{association.table_name}.#{attribute} #{direction}")
61
+ else
62
+ scope
63
+ end
64
+ end
65
+
66
+ def column_sorting(column, direction)
67
+ scope.order("#{scope.table_name}.#{column} #{direction}")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordSorting
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "active_record_sorting/version"
2
+
3
+ module ActiveRecordSorting
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'active_record_sorting/base'
3
+
4
+ describe ActiveRecordSorting::Base do
5
+ context '.named_sorting_orders' do
6
+ subject { described_class }
7
+
8
+ context 'default value' do
9
+ it { expect(subject.sorting_orders).to eq([]) }
10
+ end
11
+
12
+ context 'is set' do
13
+ before do
14
+ subject.named_sorting_orders :some_named_order, :some_named_order_2
15
+ end
16
+
17
+ it { expect(subject.sorting_orders).to eq [:some_named_order, :some_named_order_2] }
18
+ end
19
+ end
20
+
21
+ context '#sort' do
22
+ subject { described_class.new(scope) }
23
+ let(:scope) { User }
24
+
25
+ context 'no sorting order' do
26
+ it 'should return scope' do
27
+ expect(subject.sort(nil)).to eq scope
28
+ end
29
+ end
30
+
31
+ context 'named sorting order' do
32
+ before do
33
+ described_class.named_sorting_orders :named_order
34
+ end
35
+
36
+ let(:new_scope) { double }
37
+
38
+ it 'call custom named scope' do
39
+ expect(subject).to receive(:named_order).with('asc').and_return(new_scope)
40
+ expect(subject.sort('named_order_asc')).to eq new_scope
41
+ end
42
+ end
43
+
44
+ context 'simple column ordering' do
45
+ let(:new_scope) { double }
46
+
47
+ it 'call custom named scope' do
48
+ expect(scope).to receive(:order).with('users.name desc').and_return new_scope
49
+ expect(subject.sort('name_desc')).to eq new_scope
50
+ end
51
+ end
52
+
53
+ context 'relation ordering' do
54
+ let(:new_scope) { double }
55
+
56
+ it 'call custom named scope' do
57
+ expect(scope).to receive(:includes).with(:group).and_return new_scope
58
+ expect(new_scope).to receive(:order).with('groups.name asc').and_return new_scope
59
+ expect(subject.sort('group.name_asc')).to eq new_scope
60
+ end
61
+
62
+ context 'relation doesnt exist' do
63
+ it 'should return scope' do
64
+ expect(subject.sort('unknown_model.name_asc')).to eq scope
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,2 @@
1
+ class Group < ActiveRecord::Base
2
+ end
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, :force => true do |t|
5
+ t.string :name
6
+ t.integer :age
7
+ t.integer :group_id
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ create_table :groups, :force => true do |t|
13
+ t.string :name
14
+
15
+ t.timestamps
16
+ end
17
+
18
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ belongs_to :group
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record_sorting'
2
+ require 'active_record'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
5
+
6
+ require 'fixtures/schema'
7
+ require 'fixtures/group'
8
+ require 'fixtures/user'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_sorting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yratanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-11 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: activerecord
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
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple gem for Rails Active Record sorting
84
+ email:
85
+ - organium@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - active_record_sorting.gemspec
96
+ - lib/active_record_sorting.rb
97
+ - lib/active_record_sorting/base.rb
98
+ - lib/active_record_sorting/version.rb
99
+ - spec/active_record_sorting/base_spec.rb
100
+ - spec/fixtures/group.rb
101
+ - spec/fixtures/schema.rb
102
+ - spec/fixtures/user.rb
103
+ - spec/spec_helper.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Extract you sorting logic to separate object
128
+ test_files:
129
+ - spec/active_record_sorting/base_spec.rb
130
+ - spec/fixtures/group.rb
131
+ - spec/fixtures/schema.rb
132
+ - spec/fixtures/user.rb
133
+ - spec/spec_helper.rb