Skip to content

Commit

Permalink
allow adding defaults
Browse files Browse the repository at this point in the history
closes #10
closes #7
  • Loading branch information
ajb committed Dec 3, 2014
1 parent 6b0626a commit d80b7fa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class PersonFilterer < Filterer::Base
def param_admin(x)
@results.where(admin: true)
end

# Optional default params
def defaults
{
direction: 'desc'
}
end
end
```

Expand Down
6 changes: 5 additions & 1 deletion lib/filterer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ def chain(params = {}, opts = {})
end
end

def defaults
{}
end

def initialize(params = {}, opts = {})
@params, @opts = params.dup, opts
@params, @opts = defaults.merge(params), opts
setup_meta
find_results
end
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/filterer/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ def count(*args); COUNT end;

class DefaultFilterer < Filterer::Base; end;

class MutationFilterer < Filterer::Base
def starting_query
# This would really be doing it wrong...
params[:foo] = 'bar'

FakeQuery.new
end
end

class DefaultParamsFilterer < Filterer::Base
def starting_query
FakeQuery.new
end

def defaults
{
foo: 'bar'
}
end
end

class UnscopedFilterer < Filterer::Base
def starting_query
Person.select('name, email')
Expand Down Expand Up @@ -101,6 +122,17 @@ class PaginationFiltererInherit < PaginationFiltererB
}.to_not raise_error
end

it 'does not mutate the params hash' do
params = {}
filterer = MutationFilterer.new(params)
expect(params).to eq({})
end

it 'adds default params' do
filterer = DefaultParamsFilterer.new({})
expect(filterer.params).to eq(foo: 'bar')
end

it 'basic smoke test' do
expect_any_instance_of(SmokeTestFilterer).to receive(:starting_query).and_return(f = FakeQuery.new)
expect_any_instance_of(SmokeTestFilterer).to receive(:respond_to?).with(:custom_meta_data).and_return(false)
Expand Down

0 comments on commit d80b7fa

Please sign in to comment.