Skip to content

Fixes boolean declaration in an external helper file. #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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