Skip to content

Commit

Permalink
Set default filter values at initializing time
Browse files Browse the repository at this point in the history
  • Loading branch information
dubadub committed Jul 18, 2019
1 parent 92289fe commit 30e7b97
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/filtered/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ def initialize(params = {}, &block)
end

yield self if block_given?

fields.each do |name, value, definition|
next if value || !definition.default_computer

fields[name] = definition.default_computer.(self)
end
end


Expand Down Expand Up @@ -221,8 +227,6 @@ def entitled_fields
return enum_for(:entitled_fields) unless block_given?

fields.each do |name, value, definition|
value = definition.default_computer.(self) if !value && definition.default_computer

value_accepted = eval_option_proc(definition.acceptance_computer, value)
value_declined = eval_option_proc(definition.decline_computer, value)

Expand Down
2 changes: 1 addition & 1 deletion lib/filtered/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Filtered
VERSION = "0.1.0"
VERSION = "0.1.1"
end
3 changes: 3 additions & 0 deletions spec/filtered_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ class MyNewFilter < Filtered::Base
filter = MyNewFilter.new

expect(filter).to have_filter_value(year: 2019)
expect(filter.year).to eq(2019)
end

it "supports 'default' as proc" do
Expand All @@ -405,6 +406,7 @@ class MyNewFilter < Filtered::Base
end

expect(filter).to have_filter_value(year: 2019)
expect(filter.year).to eq(2019)
end

it "supports 'default' as method name" do
Expand All @@ -419,6 +421,7 @@ def default_year
filter = MyNewFilter.new

expect(filter).to have_filter_value(year: 2019)
expect(filter.year).to eq(2019)
end
end

Expand Down

0 comments on commit 30e7b97

Please sign in to comment.