Skip to content

Commit

Permalink
instance_exec to the rescue
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Jun 26, 2015
1 parent 854f8be commit ef2e36f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/filterer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def order_by_sort_option(opt)
def sort_proc_to_string(opt)
sort_key = opt[:key]
matches = sort_key.is_a?(Regexp) && params[:sort].match(sort_key)
opt[:string_or_proc].call(matches)
instance_exec matches, &opt[:string_or_proc]
end

def default_sort_option
Expand Down
2 changes: 1 addition & 1 deletion lib/filterer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Filterer
VERSION = '1.0.0.beta.3'
VERSION = '1.0.0.beta.4'
end
12 changes: 12 additions & 0 deletions spec/lib/filterer/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,20 @@ def starting_query
FakeQuery.new
end

def something_important
'yeehaw'
end

sort_option 'id', default: true
sort_option Regexp.new('foo([0-9]+)'), -> (matches) { matches[1] }
sort_option Regexp.new('zoo([0-9]+)'), -> (matches) {
if matches[1].to_i > 10
'zoo'
end
}
sort_option 'context', -> (_matches) {
something_important
}
end

class SortingFiltererF < SortingFiltererE
Expand Down Expand Up @@ -248,6 +255,11 @@ class PaginationFiltererInherit < PaginationFiltererB
filterer = SortingFiltererF.new(sort: 'zoo111')
end

it 'calls with context' do
expect_any_instance_of(FakeQuery).to receive(:order).with('yeehaw asc , tiebreak').and_return(FakeQuery.new)
filterer = SortingFiltererF.new(sort: 'context')
end

it 'applies the default sort if the proc returns nil' do
allow_any_instance_of(FakeQuery).to(
receive_message_chain(:model, :table_name).
Expand Down

0 comments on commit ef2e36f

Please sign in to comment.