rooble 0.0.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +36 -0
- data/LICENSE +21 -0
- data/README.md +56 -0
- data/lib/generators/rooble/config_generator.rb +12 -0
- data/lib/generators/templates/config_initializer.rb +4 -0
- data/lib/rooble.rb +38 -13
- data/lib/rooble/version.rb +3 -0
- data/rooble.gemspec +27 -0
- metadata +48 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5337c75a8c00119f11ac79c63831303bd404a2c
|
4
|
+
data.tar.gz: ffc83ab5fd996a9b11d484cb3833e4ff136d26c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 980c19cf41833ed5d3011953337ddad9d88b9f71944d71a8470ea81502a46944b04e143cb22984503c91cb1415b0f0fd06593d9b24be8929fc671bbfb6744829
|
7
|
+
data.tar.gz: b29f881f359c49f37fbf97b9f6830f50a398303b2652a474ba47bcc9d853fffc4b1e35f35188902b91b2107830145619560da5e3aedb2c4f3c4d0e12c0030f91
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalization:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Gustavo Rubio
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# rooble
|
2
|
+
|
3
|
+
Yet another unnecessary and selfishly-created pagination and search gem for rails.
|
4
|
+
|
5
|
+
## Why?
|
6
|
+
|
7
|
+
Because its a fun way for me to learn rails. There are other, more complex and full featured solutions like [Kaminari](https://github.com/amatsuda/kaminari) but this works fine too.
|
8
|
+
|
9
|
+
## How?
|
10
|
+
|
11
|
+
Add this to your Gemfile:
|
12
|
+
|
13
|
+
`gem 'rooble'`
|
14
|
+
|
15
|
+
Then do the usual dance:
|
16
|
+
|
17
|
+
`bundle install`
|
18
|
+
|
19
|
+
## Using it
|
20
|
+
|
21
|
+
First create a configuration file by running `rails g rooble:config` which will create `config/initializers/rooble.rb` and setup the default value for `max_records_per_page` to a sane number, you don't want to make it a high number.
|
22
|
+
|
23
|
+
There are three main methods:
|
24
|
+
|
25
|
+
`Model.pages` without any parameters will give you the number of available pages considering the `max_records_per_page` default. You can override it and pass the number of max records that should be counted per page so you get the total count of pages for such number doing something like `Model.pages(10)` if you want the count of pages with 10 records per page.
|
26
|
+
|
27
|
+
`Model.paginate(1)` will give you records for the first page. You can override the `max_records_per_page` config option on the second argument so that you paginate a specific page with a different amount of max records, for instance `Model.paginate(1, 10)` would grab the 10 records for the 1st page.
|
28
|
+
|
29
|
+
`Model.search("John", [:name])` will look for records which have **John** within the `name` attribute.
|
30
|
+
|
31
|
+
There is an extra options hash that you can pass to this method, the options are:
|
32
|
+
|
33
|
+
* `case_sensitive: false` whether you want to make the search case sensitive. Default is false.
|
34
|
+
* `type: all` type of match, beginning, end or the whole string. Default is all.
|
35
|
+
* `include` an array/hash of symbols if you want to include other relations on the model. Same as with default rails include.
|
36
|
+
* `join` an array/hash of symbols if you want to join other relations on the model. Same as with default rails join.
|
37
|
+
|
38
|
+
If you want to search for attributes in joint models you would do something like this:
|
39
|
+
|
40
|
+
`State.search("USA", "countries.name", join: [:country]).first`
|
41
|
+
|
42
|
+
Or if you want to search for the first city of the USA:
|
43
|
+
|
44
|
+
`City.search("USA", "countries.name", join: {state: :country}).first`
|
45
|
+
|
46
|
+
That would search the first state that has a country named USA. Do note the string notation for the relation.
|
47
|
+
|
48
|
+
Oh yes, you can chain the methods so you could do something like paginating search results:
|
49
|
+
|
50
|
+
`State.search("USA", "countries.name", join: [:country]).paginate(2)`
|
51
|
+
|
52
|
+
That's it for now.
|
53
|
+
|
54
|
+
## Other features
|
55
|
+
|
56
|
+
I will/may add a view helper to generate the pagination navigation and and initializer for the `MAX_RECORDS_PER_PAGE`. For now just set it yourself on your env vars.
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Rooble
|
2
|
+
module Generators
|
3
|
+
class ConfigGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../templates", __FILE__)
|
5
|
+
desc "Creates the initializer for Rooble"
|
6
|
+
|
7
|
+
def copy_initializer
|
8
|
+
template 'config_initializer.rb', 'config/initializers/rooble.rb'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/rooble.rb
CHANGED
@@ -1,6 +1,25 @@
|
|
1
|
+
require "rooble/version"
|
2
|
+
|
3
|
+
# Main module for rooble.
|
1
4
|
module Rooble
|
2
5
|
extend ActiveSupport::Concern
|
3
6
|
|
7
|
+
class Configuration
|
8
|
+
attr_accessor :max_records_per_page
|
9
|
+
end
|
10
|
+
|
11
|
+
class Error < StandardError
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
self.configuration ||= Configuration.new
|
20
|
+
yield(configuration) if block_given?
|
21
|
+
end
|
22
|
+
|
4
23
|
def self.build_query(search_term, fields, options={})
|
5
24
|
|
6
25
|
# Set whether we want case sensitive search
|
@@ -35,41 +54,47 @@ module Rooble
|
|
35
54
|
end
|
36
55
|
|
37
56
|
module ClassMethods
|
38
|
-
|
39
|
-
|
57
|
+
##
|
58
|
+
# Returns the amount of available pages to do pagination.
|
59
|
+
#
|
60
|
+
|
61
|
+
def pages(max_records_per_page=nil)
|
62
|
+
max_records_per_page ||= Rooble::configuration.max_records_per_page
|
40
63
|
total_record_count = self.count
|
41
64
|
return 0 unless total_record_count > 0
|
42
65
|
pages = (total_record_count.to_f / max_records_per_page.to_f).ceil
|
43
66
|
end
|
44
67
|
|
45
|
-
|
68
|
+
##
|
69
|
+
# Returns a set of paginated records given a page.
|
70
|
+
#
|
71
|
+
|
72
|
+
def paginate(page=1, max_records_per_page=nil)
|
46
73
|
page ||= 1
|
47
74
|
|
48
75
|
if page.to_i < 0
|
49
|
-
raise
|
76
|
+
raise Rooble::Error.new "Pagination index must be greater than zero"
|
50
77
|
end
|
51
78
|
|
52
|
-
max_records_per_page
|
79
|
+
max_records_per_page ||= Rooble::configuration.max_records_per_page
|
53
80
|
current_offset = ((page.to_i*max_records_per_page))-max_records_per_page
|
54
81
|
records = self.limit(max_records_per_page).offset(current_offset)
|
55
82
|
end
|
56
83
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
# type: beginning, end, all. whether to match beginning of the field, end or the whole thing (defauult is all)
|
61
|
-
# include: an array of symbols with relations to include
|
62
|
-
# join: an array of symbols with relations to join
|
84
|
+
##
|
85
|
+
# Searches through records for the given fields
|
86
|
+
#
|
63
87
|
|
88
|
+
def search(search_term, fields, options={})
|
64
89
|
if search_term.nil?
|
65
90
|
raise StandardError.new "You need to give a search term"
|
66
91
|
end
|
67
92
|
|
68
93
|
if fields.empty?
|
69
|
-
raise
|
94
|
+
raise Rooble::Error.new "You need to give at least one field to search"
|
70
95
|
end
|
71
96
|
|
72
|
-
raise
|
97
|
+
raise Rooble::Error.new("You can only include or join relations, not both!") if ([:include, :join] - options.keys ).empty?
|
73
98
|
|
74
99
|
# Build the query
|
75
100
|
query = Rooble::build_query(search_term, fields, options)
|
data/rooble.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rooble/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rooble'
|
8
|
+
spec.version = Rooble::VERSION
|
9
|
+
spec.authors = ["Gustavo Rubio"]
|
10
|
+
spec.email = 'gus@ahivamos.net'
|
11
|
+
|
12
|
+
spec.summary = "Allows to paginate and search through ActiveRecord associations in rails."
|
13
|
+
spec.description = "Yet another unnecessary and selfishly-created pagination and search gem for rails. This gem will add methods to your ActiveRecord associations and classes so you can search and paginate."
|
14
|
+
spec.homepage = 'http://github.com/gusrub/rooble'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
spec.date = Date.today.to_s
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
spec.files = `git ls-files`.split("\n")
|
19
|
+
spec.platform = Gem::Platform::RUBY
|
20
|
+
spec.post_install_message = "Here, have a cookie for installing this wonderful gem: 🍪"
|
21
|
+
|
22
|
+
spec.add_dependency 'activesupport', '~> 4.0', '>= 4.0.0'
|
23
|
+
spec.add_dependency 'activerecord', '~> 4.0', '>= 4.0.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.0', '>= 1.0.0'
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rooble
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gustavo Rubio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -30,6 +30,26 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 4.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activerecord
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '4.0'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 4.0.0
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '4.0'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 4.0.0
|
33
53
|
- !ruby/object:Gem::Dependency
|
34
54
|
name: bundler
|
35
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,18 +70,41 @@ dependencies:
|
|
50
70
|
- - ">="
|
51
71
|
- !ruby/object:Gem::Version
|
52
72
|
version: 1.0.0
|
53
|
-
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: rake
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '10.0'
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - "~>"
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '10.0'
|
87
|
+
description: Yet another unnecessary and selfishly-created pagination and search gem
|
88
|
+
for rails. This gem will add methods to your ActiveRecord associations and classes
|
89
|
+
so you can search and paginate.
|
54
90
|
email: gus@ahivamos.net
|
55
91
|
executables: []
|
56
92
|
extensions: []
|
57
93
|
extra_rdoc_files: []
|
58
94
|
files:
|
95
|
+
- ".gitignore"
|
96
|
+
- LICENSE
|
97
|
+
- README.md
|
98
|
+
- lib/generators/rooble/config_generator.rb
|
99
|
+
- lib/generators/templates/config_initializer.rb
|
59
100
|
- lib/rooble.rb
|
101
|
+
- lib/rooble/version.rb
|
102
|
+
- rooble.gemspec
|
60
103
|
homepage: http://github.com/gusrub/rooble
|
61
104
|
licenses:
|
62
105
|
- MIT
|
63
106
|
metadata: {}
|
64
|
-
post_install_message:
|
107
|
+
post_install_message: "Here, have a cookie for installing this wonderful gem: \U0001F36A"
|
65
108
|
rdoc_options: []
|
66
109
|
require_paths:
|
67
110
|
- lib
|
@@ -80,6 +123,5 @@ rubyforge_project:
|
|
80
123
|
rubygems_version: 2.4.5.1
|
81
124
|
signing_key:
|
82
125
|
specification_version: 4
|
83
|
-
summary:
|
84
|
-
rails
|
126
|
+
summary: Allows to paginate and search through ActiveRecord associations in rails.
|
85
127
|
test_files: []
|