simple_listing_rails 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dcde66c4f015e09e79441ce4dddd6e82047df681
4
+ data.tar.gz: 46bd5fc5b24a3b6de8ecbbe6d807dbde2cc7eb73
5
+ SHA512:
6
+ metadata.gz: 4569d75905bdf6ba38e3b0a2824a94d80f9dfea627ff093a25a8a6d7370f06b283e44057be58ed0530375cca779e398e04662299b2b4fa356880bbb7b84b9ea1
7
+ data.tar.gz: 39e34bda2d1098f3ddd3a84008cd45df283408ce5cd1ed4668050ad8ce0718f2ea07b7d996803e0ecc711bf34c12cf8cd934e9494d7bc4d44f07ddbd69daa23f
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_listing_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pavel Shutin
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,151 @@
1
+ # SimpleListingRails
2
+
3
+ This gem provides easy listing objects for your ActiveRecord relations.
4
+
5
+ ## Why
6
+
7
+ Usually application listings have are sortable, filterable and can be paginated.
8
+ I need an easy straightforward way to write flexible filters and sortings for my app, so I created this gem.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'simple_listing_rails'
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install simple_listing_rails
23
+
24
+ ## Usage
25
+
26
+ ### Minimal required code
27
+
28
+ First of all you need to define a listing class. E.g.
29
+
30
+ ```ruby
31
+ class UserListing < SimpleListing::Standard
32
+ end
33
+ ```
34
+
35
+ and then use it in your controller with "perform" function:
36
+
37
+ ```ruby
38
+ def index
39
+ @users = UserListing(User.all, params).perform
40
+ end
41
+ ```
42
+
43
+ Now you can add some filters or sortings declaration to your class.
44
+
45
+ ### Filtering
46
+
47
+ #### Simple "equal" filters
48
+
49
+ You can add strict match filter to your listing class with only one line:
50
+
51
+ ```ruby
52
+ class UserListing < SimpleListing::Standard
53
+ filterable_by :first_name, :last_name, :email
54
+ end
55
+ ```
56
+
57
+ `filterable_by` will perform "=" comparision with corresponding value in DB.
58
+
59
+ #### Custom filters
60
+
61
+ If you need your own custom filter with custom logic you can use `filter_by` function:
62
+
63
+ ```ruby
64
+ class UserListing < SimpleListing::Standard
65
+ filter_by :email, lambda { |scope, value, listing|
66
+ scope.where("email LIKE ?", "%#{value}")
67
+ }
68
+ end
69
+ ```
70
+
71
+ `filter_by` accepts filtering key and lambda-function with your own custom logic. As you see lambda accepts 3 arguments and MUST return a scope.
72
+
73
+ Keep in mind that "value" parameter can be a hash, so you can create complex filters e.g.:
74
+
75
+ ```ruby
76
+ class UserListing < SimpleListing::Standard
77
+ filter_by :age, lambda { |scope, value, listing|
78
+ scope.where("age > :min AND age < :max", value)
79
+ }
80
+ end
81
+ ```
82
+
83
+ #### Filters configuration
84
+
85
+ By default simple_listing pulls filters data from "params[:filters]".
86
+ If you need to change params key you can do it so by calling "config" function
87
+
88
+ ```ruby
89
+ class UserListing < SimpleListing::Standard
90
+ config filter_params_key: :my_filters
91
+ end
92
+ ```
93
+
94
+ In case you need completely different behavior you can override "filter_params" function in your listing class.
95
+
96
+ See more details in source code.
97
+
98
+ ### Sorting
99
+
100
+ #### Simple sorting by DB field
101
+
102
+ You can add sortings to your listing class with only one line:
103
+
104
+ ```ruby
105
+ class UserListing < SimpleListing::Standard
106
+ sortable_by :first_name, :last_name, :email
107
+ end
108
+ ```
109
+
110
+ #### Custom sorting
111
+
112
+ If you need your own custom sorting with custom logic you can use `sort_by` function:
113
+
114
+ ```ruby
115
+ class UserListing < SimpleListing::Standard
116
+ sort_by :email, lambda { |scope, direction, listing|
117
+ scope.order("CONCAT(first_name, last_name) #{direction}")
118
+ }
119
+ end
120
+ ```
121
+
122
+ `sort_by` accepts sorting key and lambda-function with your own custom logic. As you see lambda accepts 3 arguments and MUST return a scope.
123
+
124
+ #### Sorters configuration
125
+
126
+ By default simple_listing pulls sorting data from "params[:sort_by]" and "params[:sort_dir]".
127
+ If you need to change params keys you can do it so by calling "config" function
128
+
129
+ ```ruby
130
+ class UserListing < SimpleListing::Standard
131
+ config sort_by_param_key: :sind, sort_direction_param_key: :sord
132
+ end
133
+ ```
134
+
135
+ See more details in source code.
136
+
137
+ ### Customizing your listings
138
+
139
+ In rare case when you want to remove sorting or filtering ability from your listing
140
+ you can always inherit from "SimpleListing::Base" class and include only modules you need.
141
+
142
+ ## Contributing
143
+
144
+ 1. Fork it ( https://github.com/pluff/simple_listing_rails/fork )
145
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
146
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
147
+ 4. Push to the branch (`git push origin my-new-feature`)
148
+ 5. Create a new Pull Request
149
+
150
+
151
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,9 @@
1
+ require "simple_listing/configurable"
2
+ require "simple_listing/base"
3
+ require "simple_listing/filterable"
4
+ require "simple_listing/paginatable"
5
+ require "simple_listing/sortable"
6
+ require "simple_listing/standard"
7
+
8
+ module SimpleListing
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'simple_listing/configurable'
2
+
3
+ module SimpleListing
4
+ class Base
5
+ include Configurable
6
+
7
+ delegate :config, to: :class
8
+
9
+ attr_accessor :scope, :params
10
+ private :scope=, :params=
11
+
12
+ def initialize(scope, params)
13
+ @scope, @params = scope, params
14
+ end
15
+
16
+ def perform
17
+ yield self if block_given?
18
+ scope
19
+ end
20
+
21
+ private
22
+
23
+ def guard(message = nil)
24
+ raise(ArgumentError, message) unless yield == true
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module SimpleListing
2
+ module Configurable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def config(config_hash = nil)
7
+ self.configuration = configuration.merge(config_hash) if config_hash
8
+ configuration
9
+ end
10
+ end
11
+
12
+ included do
13
+ class_attribute :configuration
14
+ self.configuration = {}
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ module SimpleListing
2
+ module Filterable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ attr_reader :filterable_keys
7
+ attr_reader :filtering_handlers
8
+
9
+ def filterable_by(*keys)
10
+ @filterable_keys ||= []
11
+ @filterable_keys += keys.map(&:to_s)
12
+ @filterable_keys.uniq!
13
+ end
14
+
15
+ def filter_by(key, handler_proc)
16
+ filterable_by key
17
+ @filtering_handlers ||= {}.with_indifferent_access
18
+ @filtering_handlers[key] = handler_proc
19
+ end
20
+ end
21
+
22
+ included do
23
+ delegate :filtering_handlers, :filterable_keys, to: :class
24
+
25
+ config filter_params_key: :filters
26
+ end
27
+
28
+ def perform
29
+ super
30
+ apply_filters if should_be_filtered?
31
+ scope
32
+ end
33
+
34
+ def filter_params
35
+ params[config[:filter_params_key]]
36
+ end
37
+
38
+ def should_be_filtered?
39
+ filter_params && filter_params.respond_to?(:each)
40
+ end
41
+
42
+ private
43
+
44
+ def apply_filters
45
+ filter_params.each do |key, value|
46
+ guard("incorrect filter key '#{key}'") { filterable_by?(key) }
47
+ self.scope = if handler = filtering_handlers[key]
48
+ handler.call(scope, value, self)
49
+ else
50
+ scope.where(key => value)
51
+ end
52
+ end
53
+ end
54
+
55
+ def filterable_by?(key)
56
+ key.to_s.in? filterable_keys
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ module SimpleListing
2
+ module Paginatable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ config page_param_key: :page, per_page_param_key: :per_page
7
+ end
8
+
9
+ def perform
10
+ super
11
+ apply_pagination if should_be_paginated?
12
+ scope
13
+ end
14
+
15
+ def page
16
+ params[config[:page_param_key]]
17
+ end
18
+
19
+ def per_page
20
+ params[config[:per_page_param_key]]
21
+ end
22
+
23
+ def should_be_paginated?
24
+ page && per_page
25
+ end
26
+
27
+ private
28
+
29
+ def apply_pagination
30
+ self.scope = scope.page(page).per(per_page)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,72 @@
1
+ module SimpleListing
2
+ module Sortable
3
+ extend ActiveSupport::Concern
4
+
5
+ SORT_DIRECTIONS = %w{asc desc}.freeze
6
+
7
+ module ClassMethods
8
+ attr_reader :sortable_keys
9
+ attr_reader :sorting_handlers
10
+
11
+ def sortable_by(*keys)
12
+ @sortable_keys ||= []
13
+ @sortable_keys += keys.map(&:to_s)
14
+ @sortable_keys.uniq!
15
+ end
16
+
17
+ def sort_by(key, handler_proc)
18
+ sortable_by key
19
+ @sorting_handlers ||= {}.with_indifferent_access
20
+ @sorting_handlers[key] = handler_proc
21
+ end
22
+ end
23
+
24
+ included do
25
+ delegate :sorting_handlers, :sortable_keys, to: :class
26
+
27
+ config sort_by_param_key: :sort_by, sort_direction_param_key: :sort_dir
28
+ end
29
+
30
+ def perform
31
+ super
32
+ apply_sorting if should_be_sorted?
33
+ scope
34
+ end
35
+
36
+ def sort_value
37
+ @sort_value ||= params[config[:sort_by_param_key]].tap do |value|
38
+ guard("incorrect sorting value '#{value}'") { sortable_by?(value) }
39
+ end
40
+ end
41
+
42
+ def sort_direction
43
+ @sort_direction ||= begin
44
+ direction = params[config[:sort_direction_param_key]]
45
+ guard("incorrect sorting direction '#{direction}'") { direction.in? SORT_DIRECTIONS }
46
+ direction.to_sym
47
+ end
48
+ end
49
+
50
+ def inverted_sort_direction
51
+ sort_direction == :asc ? :desc : :asc
52
+ end
53
+
54
+ def should_be_sorted?
55
+ params[config[:sort_by_param_key]] && params[config[:sort_direction_param_key]]
56
+ end
57
+
58
+ private
59
+
60
+ def apply_sorting
61
+ self.scope = if handler = sorting_handlers[sort_value]
62
+ handler.call(scope, sort_direction, self)
63
+ else
64
+ scope.order(sort_value => sort_direction)
65
+ end
66
+ end
67
+
68
+ def sortable_by?(value)
69
+ sortable_keys.include? value
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,11 @@
1
+ require "simple_listing/base"
2
+ require "simple_listing/filterable"
3
+ require "simple_listing/paginatable"
4
+ require "simple_listing/sortable"
5
+
6
+ module SimpleListing
7
+ class Standard < Base
8
+ include Filterable
9
+ include Sortable
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ require "simple_listing_rails/version"
2
+ require 'simple_listing'
3
+
4
+ module SimpleListingRails
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleListingRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -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 'simple_listing_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_listing_rails"
8
+ spec.version = SimpleListingRails::VERSION
9
+ spec.authors = ["Pavel Shutsin"]
10
+ spec.email = ["publicshady@gmail.com"]
11
+ spec.summary = 'Simple classes for listing actions in your Rails app.'
12
+ spec.description = 'This gem provides easy and flexible way to write listing actions in your Rails app. It provides nice DSL for filtering, sorting and paginating in your listings.'
13
+ spec.homepage = "https://github.com/pluff/simple_listing_rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency 'activerecord', '~> 4.0'
22
+ spec.add_dependency 'activesupport', '~> 4.0'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_listing_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Shutsin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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
+ description: This gem provides easy and flexible way to write listing actions in your
70
+ Rails app. It provides nice DSL for filtering, sorting and paginating in your listings.
71
+ email:
72
+ - publicshady@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/simple_listing.rb
83
+ - lib/simple_listing/base.rb
84
+ - lib/simple_listing/configurable.rb
85
+ - lib/simple_listing/filterable.rb
86
+ - lib/simple_listing/paginatable.rb
87
+ - lib/simple_listing/sortable.rb
88
+ - lib/simple_listing/standard.rb
89
+ - lib/simple_listing_rails.rb
90
+ - lib/simple_listing_rails/version.rb
91
+ - simple_listing_rails.gemspec
92
+ homepage: https://github.com/pluff/simple_listing_rails
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Simple classes for listing actions in your Rails app.
116
+ test_files: []