findit 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Appraisals +20 -0
- data/Gemfile +4 -0
- data/Makefile +23 -0
- data/README.md +160 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config.ru +7 -0
- data/findit.gemspec +31 -0
- data/lib/findit/collections.rb +67 -0
- data/lib/findit/version.rb +3 -0
- data/lib/findit.rb +6 -0
- metadata +196 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a2e4ae4411e438b0ca6b05ee3c908af0705c33e1
|
4
|
+
data.tar.gz: 2e227b91fa3311eaf118e342065e45f1de42c4a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2104233901b47ecabf8062e6641fdf78c3bcd1f54d19ec923623e9548362c5c4cef814cdf4609deee08e05317906d268bd7564d4a5e56c7c031597b6a3383e74
|
7
|
+
data.tar.gz: b717349e17368e0e70fed8c1f8a9515a55d608b5d5312fc3057adeeb002bf250da6a64f7a16d41524930314deab01f08781cf68544e1fede45f805d852ad243a
|
data/.gitignore
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
appraise 'rails3.1' do
|
2
|
+
gem 'rails', '~> 3.1.0'
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise 'rails3.2' do
|
6
|
+
gem 'rails', '~> 3.2.0'
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise 'rails4.0' do
|
10
|
+
gem 'rails', '~> 4.0.0'
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise 'rails4.1' do
|
14
|
+
gem 'rails', '~> 4.1.0'
|
15
|
+
end
|
16
|
+
|
17
|
+
appraise 'rails4.2' do
|
18
|
+
gem 'rails', '~> 4.2.0'
|
19
|
+
end
|
20
|
+
|
data/Gemfile
ADDED
data/Makefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
RAILS_ENV = test
|
2
|
+
BUNDLE_VERSION = 1.11.2
|
3
|
+
BUNDLE = RAILS_ENV=${RAILS_ENV} bundle _${BUNDLE_VERSION}_
|
4
|
+
BUNDLE_OPTIONS = -j 2
|
5
|
+
RSPEC = rspec
|
6
|
+
APPRAISAL = appraisal
|
7
|
+
|
8
|
+
all: test
|
9
|
+
|
10
|
+
test: bundler/install appraisal/install
|
11
|
+
${BUNDLE} exec ${APPRAISAL} ${RSPEC} spec 2>&1
|
12
|
+
|
13
|
+
bundler/install:
|
14
|
+
gem install bundler --version=${BUNDLE_VERSION};
|
15
|
+
${BUNDLE} install ${BUNDLE_OPTIONS}
|
16
|
+
|
17
|
+
appraisal/install:
|
18
|
+
${BUNDLE} exec ${APPRAISAL} install
|
19
|
+
|
20
|
+
clean:
|
21
|
+
rm -f Gemfile.lock
|
22
|
+
rm -rf gemfiles
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
# Findit
|
2
|
+
|
3
|
+
Tired of writing fat controllers? But you must do all these queries.. There is a solution, move it to a Finder class.
|
4
|
+
|
5
|
+
Don't write this:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class SomeController
|
9
|
+
def index
|
10
|
+
@cache_key = (
|
11
|
+
# many_values
|
12
|
+
)
|
13
|
+
return if fragment_exists?(@cache_key)
|
14
|
+
search_scope = SearchEngine.scope
|
15
|
+
search_scope.add(some_conditions)
|
16
|
+
search_scope.add(some_conditions)
|
17
|
+
search_scope.add(some_conditions)
|
18
|
+
search_scope.add(some_conditions)
|
19
|
+
search_scope.add(some_conditions)
|
20
|
+
search_scope.add(some_conditions)
|
21
|
+
search_scope.add(some_conditions)
|
22
|
+
result = search_scope.search_and_return_ids
|
23
|
+
@scope = scope.where(ids: result)
|
24
|
+
....
|
25
|
+
end
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Just do this:
|
30
|
+
```ruby
|
31
|
+
# /app/controllers/some_controller.rb
|
32
|
+
class SomeController
|
33
|
+
def index
|
34
|
+
@scope = SomeFinder.new(params)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# app/finders/some_finder.rb
|
39
|
+
class SomeFinder
|
40
|
+
include Findit::Collections
|
41
|
+
|
42
|
+
cache_key do
|
43
|
+
# calculate you cache_key from params
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize(params)
|
47
|
+
# some initialize, maybe params parse
|
48
|
+
end
|
49
|
+
|
50
|
+
def call
|
51
|
+
# put here you find logic
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
And that it! Now you can iterate over finder results by simple `each`:
|
57
|
+
```ruby
|
58
|
+
@scope = SomeFinder.new(params)
|
59
|
+
@scope.each do |d|
|
60
|
+
print d.description
|
61
|
+
end
|
62
|
+
```
|
63
|
+
or perform caching like ActiveRecord::Base
|
64
|
+
```ruby
|
65
|
+
# app/some_view
|
66
|
+
<% cache @scope do %>
|
67
|
+
<%scope.each do |res|%>
|
68
|
+
...
|
69
|
+
<%end%>
|
70
|
+
<%end%>
|
71
|
+
|
72
|
+
```
|
73
|
+
|
74
|
+
## Installation
|
75
|
+
|
76
|
+
Add this line to your application's Gemfile:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
gem 'findit'
|
80
|
+
```
|
81
|
+
|
82
|
+
And then execute:
|
83
|
+
|
84
|
+
$ bundle
|
85
|
+
|
86
|
+
Or install it yourself as:
|
87
|
+
|
88
|
+
$ gem install findit
|
89
|
+
|
90
|
+
## Per module documentation
|
91
|
+
|
92
|
+
### Collections
|
93
|
+
|
94
|
+
It makes Finder work as Enumerator.
|
95
|
+
Result can be accessed with `each`, `[]` and `size` methods, but to make things work you *must* implement `call` method.
|
96
|
+
```
|
97
|
+
class PostFinder
|
98
|
+
incliude Findit::Collections
|
99
|
+
|
100
|
+
def call
|
101
|
+
Post.where(user_id: 1)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
@posts = PostFinder.new
|
106
|
+
|
107
|
+
# load all matching posts and iterate over collection
|
108
|
+
@posts.each do |post|
|
109
|
+
print post.title
|
110
|
+
end
|
111
|
+
|
112
|
+
@posts[10] # get 10 element of posts
|
113
|
+
|
114
|
+
@posts.size # size of PostFinder results
|
115
|
+
|
116
|
+
# Also you can access result direcly by using `data` method.
|
117
|
+
@posts.data # access to all posts
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
For easier caching expirience we provide DSL to define you custom `cache_key`
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
#/app/finders/posts_finders.rb
|
125
|
+
class PostFinder
|
126
|
+
include Findit::Collections
|
127
|
+
|
128
|
+
cache_key do
|
129
|
+
[@user.id, @query] # here you put any stuff that result of finder depend on it
|
130
|
+
end
|
131
|
+
|
132
|
+
# custom initializer, do whatever you want here
|
133
|
+
def initialize(user, options = {})
|
134
|
+
@user = user
|
135
|
+
@query = options[:query]
|
136
|
+
end
|
137
|
+
|
138
|
+
# Here we fetch results. You MUST implement it
|
139
|
+
def call
|
140
|
+
scope = scope.where(user_id: @user.id)
|
141
|
+
scope = scope.where('description like :query', query: @query) if @query.present?
|
142
|
+
scope
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
#/app/controllers/posts_controller.rb
|
147
|
+
class PostsController < ApplicationController
|
148
|
+
def index
|
149
|
+
@posts = PostFinder.new(user: current_user)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
#/app/views/posts/index.html.haml
|
154
|
+
<% cache(@posts, expire_in: 30.minutes) do %>
|
155
|
+
<%=render 'post' colection: @posts, as: :post%> # it will automaticly iterate over finder results by each method
|
156
|
+
```
|
157
|
+
|
158
|
+
## Contributing
|
159
|
+
|
160
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/findit.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "findit"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/config.ru
ADDED
data/findit.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'findit/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "findit"
|
8
|
+
spec.version = Findit::VERSION
|
9
|
+
spec.authors = ["Denis Korobicyn"]
|
10
|
+
spec.email = ["deniskorobitcin@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Extensions for Finder classes}
|
13
|
+
spec.homepage = "https://github.com/abak-press/findit"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency 'activesupport', '>= 3.1'
|
21
|
+
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", ">= 3.2"
|
24
|
+
spec.add_development_dependency "rspec-rails", ">= 3.2"
|
25
|
+
spec.add_development_dependency 'combustion', '>= 0.5'
|
26
|
+
spec.add_development_dependency "appraisal", ">= 2.1.0"
|
27
|
+
spec.add_development_dependency 'pry-debugger'
|
28
|
+
spec.add_development_dependency 'shoulda-matchers', '< 3.0.0'
|
29
|
+
spec.add_development_dependency 'simplecov'
|
30
|
+
spec.add_development_dependency 'sqlite3'
|
31
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#
|
2
|
+
# Example usage:
|
3
|
+
#
|
4
|
+
# #/app/finders/posts_finders.rb
|
5
|
+
# class PostFinder
|
6
|
+
# include Findit::Collections
|
7
|
+
#
|
8
|
+
# cache_key do
|
9
|
+
# [@user.id, @query]
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# cache_tags do
|
13
|
+
# {user_id: @user.id}
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# expire_in 30.minutes
|
17
|
+
#
|
18
|
+
# def initialize(user, options = {})
|
19
|
+
# @user = user
|
20
|
+
# @query = options[:query]
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def call
|
24
|
+
# scope = scope.where(user_id: @user.id)
|
25
|
+
# scope = scope.where('description like :query', query: @query) if @query.present?
|
26
|
+
# scope
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# #/app/controllers/posts_controller.rb
|
31
|
+
# class PostsController < ApplicationController
|
32
|
+
# def index
|
33
|
+
# @posts = PostFinder.new(user: current_user)
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# #/app/views/posts/index.html.erb
|
38
|
+
# <% cache(@posts, tags: @posts.cache_tags, expire_in: @posts.expire_in) do %>
|
39
|
+
# <%= render 'post' colection: @posts, as: :post%>
|
40
|
+
#
|
41
|
+
module Findit
|
42
|
+
module Collections
|
43
|
+
include Enumerable
|
44
|
+
extend ActiveSupport::Concern
|
45
|
+
|
46
|
+
included do
|
47
|
+
delegate :each, :[], :size, :empty?, to: :data
|
48
|
+
end
|
49
|
+
|
50
|
+
module ClassMethods
|
51
|
+
def cache_key(&block)
|
52
|
+
define_method :cache_key do
|
53
|
+
@cache_key ||= ActiveSupport::Cache.expand_cache_key(instance_exec(&block))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def call
|
59
|
+
end
|
60
|
+
undef :call
|
61
|
+
|
62
|
+
def data
|
63
|
+
return @data if defined?(@data)
|
64
|
+
@data = call
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/findit.rb
ADDED
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: findit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Korobicyn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
version_requirements: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - '>='
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '3.1'
|
19
|
+
name: activesupport
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - '>='
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '3.1'
|
25
|
+
type: :runtime
|
26
|
+
prerelease: false
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
version_requirements: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '10.0'
|
33
|
+
name: rake
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '10.0'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.2'
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '3.2'
|
53
|
+
type: :development
|
54
|
+
prerelease: false
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.2'
|
61
|
+
name: rspec-rails
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '3.2'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0.5'
|
75
|
+
name: combustion
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0.5'
|
81
|
+
type: :development
|
82
|
+
prerelease: false
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 2.1.0
|
89
|
+
name: appraisal
|
90
|
+
requirement: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 2.1.0
|
95
|
+
type: :development
|
96
|
+
prerelease: false
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
name: pry-debugger
|
104
|
+
requirement: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
type: :development
|
110
|
+
prerelease: false
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - <
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 3.0.0
|
117
|
+
name: shoulda-matchers
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - <
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 3.0.0
|
123
|
+
type: :development
|
124
|
+
prerelease: false
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
name: simplecov
|
132
|
+
requirement: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
type: :development
|
138
|
+
prerelease: false
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
name: sqlite3
|
146
|
+
requirement: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
type: :development
|
152
|
+
prerelease: false
|
153
|
+
description:
|
154
|
+
email:
|
155
|
+
- deniskorobitcin@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- .gitignore
|
161
|
+
- Appraisals
|
162
|
+
- Gemfile
|
163
|
+
- Makefile
|
164
|
+
- README.md
|
165
|
+
- Rakefile
|
166
|
+
- bin/console
|
167
|
+
- bin/setup
|
168
|
+
- config.ru
|
169
|
+
- findit.gemspec
|
170
|
+
- lib/findit.rb
|
171
|
+
- lib/findit/collections.rb
|
172
|
+
- lib/findit/version.rb
|
173
|
+
homepage: https://github.com/abak-press/findit
|
174
|
+
licenses: []
|
175
|
+
metadata: {}
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options: []
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
requirements:
|
182
|
+
- - '>='
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
|
+
requirements:
|
187
|
+
- - '>='
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project:
|
192
|
+
rubygems_version: 2.4.8
|
193
|
+
signing_key:
|
194
|
+
specification_version: 4
|
195
|
+
summary: Extensions for Finder classes
|
196
|
+
test_files: []
|