Skip to content

Commit

Permalink
Merge pull request #1153 from towanda/fix-boolean
Browse files Browse the repository at this point in the history
Fixes boolean declaration in an external helper file.
  • Loading branch information
dblock committed Sep 14, 2015
2 parents d3841f1 + ff6b031 commit 69f6ccb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#### Fixes

* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).
* [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest).
* [#1114](https://github.com/ruby-grape/grape/pull/1114): Fix regression which broke identical endpoints with different versions - [@suan](https://github.com/suan).
* [#1109](https://github.com/ruby-grape/grape/pull/1109): Memoize Virtus attribute and fix memory leak - [@marshall-lee](https://github.com/marshall-lee).
Expand Down
6 changes: 6 additions & 0 deletions lib/grape/dsl/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module ClassMethods
def helpers(new_mod = nil, &block)
if block_given? || new_mod
mod = new_mod || Module.new
define_boolean_in_mod(mod)
if new_mod
inject_api_helpers_to_mod(new_mod) if new_mod.is_a?(BaseHelper)
end
Expand All @@ -51,6 +52,11 @@ def helpers(new_mod = nil, &block)

protected

def define_boolean_in_mod(mod)
return if defined? mod::Boolean
mod.const_set('Boolean', Virtus::Attribute::Boolean)
end

def inject_api_helpers_to_mod(mod, &_block)
mod.extend(BaseHelper)
yield if block_given?
Expand Down
16 changes: 16 additions & 0 deletions spec/grape/dsl/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ def self.mod
end
end
end

module BooleanParam
extend Grape::API::Helpers

params :requires_toggle_prm do
requires :toggle_prm, type: Boolean
end
end

describe Helpers do
subject { Class.new(HelpersSpec::Dummy) }
let(:proc) do
Expand Down Expand Up @@ -39,6 +48,13 @@ def test

expect(subject.mod).to eq mod
end

context 'with an external file' do
it 'sets Boolean as a Virtus::Attribute::Boolean' do
subject.helpers BooleanParam
expect(subject.mod::Boolean).to eq Virtus::Attribute::Boolean
end
end
end
end
end
Expand Down

0 comments on commit 69f6ccb

Please sign in to comment.