Skip to content

Commit 82f3497

Browse files
authored
ActiveSupport inflector methods (#2330)
* Replace convert_to_short_name by ActiveSupport inflector methods * Use delete_suffix! * Changelog entry
1 parent 96e2faf commit 82f3497

File tree

3 files changed

+4
-10
lines changed

3 files changed

+4
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).
66
* [#2327](https://github.com/ruby-grape/grape/pull/2327): Use ActiveSupport deprecation - [@ericproulx](https://github.com/ericproulx).
7+
* [#2330](https://github.com/ruby-grape/grape/pull/2330): Use ActiveSupport inflector - [@ericproulx](https://github.com/ericproulx).
78
* Your contribution here.
89

910
#### Fixes

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
require 'active_support/core_ext/object/duplicable'
2828
require 'active_support/dependencies/autoload'
2929
require 'active_support/deprecation'
30+
require 'active_support/inflector'
3031
require 'active_support/notifications'
3132
require 'i18n'
3233

lib/grape/validations/validators/base.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,11 @@ def validate!(params)
6161
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
6262
end
6363

64-
def self.convert_to_short_name(klass)
65-
ret = klass.name.gsub(/::/, '/')
66-
ret.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
67-
ret.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
68-
ret.tr!('-', '_')
69-
ret.downcase!
70-
File.basename(ret, '_validator')
71-
end
72-
7364
def self.inherited(klass)
7465
return if klass.name.blank?
7566

76-
Validations.register_validator(convert_to_short_name(klass), klass)
67+
short_validator_name = klass.name.demodulize.underscore.delete_suffix!('_validator')
68+
Validations.register_validator(short_validator_name, klass)
7769
end
7870

7971
def message(default_key = nil)

0 commit comments

Comments
 (0)