Skip to content

Commit

Permalink
feature: removed data, and pure virtual method - find (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskorobicyn committed May 5, 2016
1 parent 274a7af commit baffa09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Findit

Tired of writing fat controllers? But you must do all these queries.. There is a solution, move it to a Finder class.
Tired of writing fat controllers? But you must do all these queries.. There is a solution, move it to a special Finder class!

Don't write this:
Stop writing this:

```ruby
class SomeController
Expand Down Expand Up @@ -47,7 +47,9 @@ class SomeFinder
# some initialize, maybe params parse
end

def call
private

def find
# put here you find logic
end
end
Expand Down Expand Up @@ -91,13 +93,15 @@ Or install it yourself as:

### Collections

It makes Finder work as Enumerator.
Result can be accessed with `each`, `[]` and `size` methods, but to make things work you *must* implement `call` method.
It makes Finder work as Enumerator with lazy load.
Result can be accessed with `each`, `[]` and `size` methods, but to make things work you *must* implement `find` method.
```
class PostFinder
incliude Findit::Collections
def call
private # make it private, so no one call it without lazy load
def find
Post.where(user_id: 1)
end
end
Expand Down Expand Up @@ -130,13 +134,14 @@ class PostFinder
end

# custom initializer, do whatever you want here
def initialize(user, options = {})
@user = user
def initialize(options = {})
@user = options.fetch(:user)
@query = options[:query]
end

private
# Here we fetch results. You MUST implement it
def call
def find
scope = scope.where(user_id: @user.id)
scope = scope.where('description like :query', query: @query) if @query.present?
scope
Expand Down
20 changes: 8 additions & 12 deletions lib/findit/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@
# [@user.id, @query]
# end
#
# cache_tags do
# {user_id: @user.id}
# end
#
# expire_in 30.minutes
#
# def initialize(user, options = {})
# @user = user
# @query = options[:query]
# end
#
# def call
# private
#
# def find
# scope = scope.where(user_id: @user.id)
# scope = scope.where('description like :query', query: @query) if @query.present?
# scope
Expand All @@ -44,7 +40,7 @@ module Collections
extend ActiveSupport::Concern

included do
delegate :each, :[], :size, :empty?, to: :data
delegate :each, :[], :size, :empty?, to: :call
end

module ClassMethods
Expand All @@ -55,13 +51,13 @@ def cache_key(&block)
end
end

def call
def find
end
undef :call
undef :find

def data
def call
return @data if defined?(@data)
@data = call
@data = find
end
end
end
11 changes: 4 additions & 7 deletions spec/internal/app/finders/post_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ def initialize(user, options = {})
@query = options[:query]
end

def call
private

def find
scope = Post.where(user_id: @user.id)
scope = scope.where('text like :query', query: "%#{@query}%") if @query.present?
scope
end

def first_post
cache('first_post') do
data.first
end
end
end

0 comments on commit baffa09

Please sign in to comment.