rooble 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rooble.rb +90 -0
  3. metadata +85 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a4d2cb17f6bb39efab61bf9505f07d7b244d454
4
+ data.tar.gz: e60dfd9da1704718c8d4d01ee0a57764ccc13456
5
+ SHA512:
6
+ metadata.gz: ca3dae8f9ad5039ca86ded8c6731013b30162f0492d659633cbcf716f6dbefaffda55ceecdd2b88e3ba280525eb079ac623ccfca7867fd65f3063bec1da96ada
7
+ data.tar.gz: f5ab1bc4338214d6750bc922bce0bb0d1b59e4b3d3fd29a71408774fb721f204605f37a27ec427d449fad0958227010667cd229eda29ca2ac6b0bbcc70e3ea08
data/lib/rooble.rb ADDED
@@ -0,0 +1,90 @@
1
+ module Rooble
2
+ extend ActiveSupport::Concern
3
+
4
+ def self.build_query(search_term, fields, options={})
5
+
6
+ # Set whether we want case sensitive search
7
+ operator = "ILIKE"
8
+ if options.has_key? :case_sensitive
9
+ operator = "LIKE" if options[:case_sensitive]
10
+ end
11
+
12
+ search_beginning = "%"
13
+ search_end = "%"
14
+ if options.has_key? :type
15
+ case options[:type]
16
+ when "beginning"
17
+ search_end = ""
18
+ when "end"
19
+ search_beginning = ""
20
+ end
21
+ end
22
+
23
+ # Loop through fields and build query
24
+ query = ""
25
+ or_cond = ""
26
+ fields = [].push(fields) unless fields.is_a? Array
27
+ fields.each_with_index do |v,i|
28
+ if i > 0
29
+ or_cond = "OR"
30
+ end
31
+ query += " #{or_cond} #{v} #{operator} '#{search_beginning}#{search_term}#{search_end}' "
32
+ end
33
+
34
+ query
35
+ end
36
+
37
+ module ClassMethods
38
+ def pages
39
+ max_records_per_page = ENV['MAX_RECORDS_PER_PAGE'].to_i
40
+ total_record_count = self.count
41
+ return 0 unless total_record_count > 0
42
+ pages = (total_record_count.to_f / max_records_per_page.to_f).ceil
43
+ end
44
+
45
+ def paginate(page=1)
46
+ page ||= 1
47
+
48
+ if page.to_i < 0
49
+ raise StandardError.new "Pagination index must be greater than zero"
50
+ end
51
+
52
+ max_records_per_page = ENV['MAX_RECORDS_PER_PAGE'].to_i
53
+ current_offset = ((page.to_i*max_records_per_page))-max_records_per_page
54
+ records = self.limit(max_records_per_page).offset(current_offset)
55
+ end
56
+
57
+ def search(search_term, fields, options={})
58
+ # Options:
59
+ # case_sensitive: true or false, default is false
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
63
+
64
+ if search_term.nil?
65
+ raise StandardError.new "You need to give a search term"
66
+ end
67
+
68
+ if fields.empty?
69
+ raise StandardError.new "You need to give at least one field to search"
70
+ end
71
+
72
+ raise StandardError.new("You can only include or join relations, not both!") if ([:include, :join] - options.keys ).empty?
73
+
74
+ # Build the query
75
+ query = Rooble::build_query(search_term, fields, options)
76
+
77
+ if options.has_key? :include
78
+ records = self.includes(options[:include]).where(query)
79
+ elsif options.has_key? :join
80
+ records = self.joins(options[:join]).where(query)
81
+ else
82
+ records = self.where(query)
83
+ end
84
+
85
+ records
86
+ end
87
+ end
88
+
89
+ ActiveRecord::Base.send(:include, Rooble)
90
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rooble
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gustavo Rubio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.0.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.0.0
53
+ description: Allows to paginate and search through ActiveRecord associations
54
+ email: gus@ahivamos.net
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - lib/rooble.rb
60
+ homepage: http://github.com/gusrub/rooble
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Yet another unnecessary and selfishly-created pagination and search gem for
84
+ rails
85
+ test_files: []