split up inflection method

pull/8045/head
ulferts 5 years ago
parent 0b342ec0d4
commit 34a61e62d0
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 39
      config/constants/open_project/inflector.rb
  2. 82
      config/initializers/zeitwerk.rb
  3. 15
      lib/open_project/plugins/acts_as_op_engine.rb
  4. 2
      modules/openid_connect/lib/open_project/openid_connect/engine.rb

@ -0,0 +1,39 @@
module OpenProject
class Inflector < Zeitwerk::GemInflector
alias_method :default_inflect, :camelize
def camelize(basename, abspath)
self.class.camelize_rules.each do |rule|
name = instance_exec(basename, abspath, &rule)
return name if name
end
super
end
private
def overrides
self.class.inflections.merge(super)
end
class << self
def rule(&block)
camelize_rules << block
end
def camelize_rules
@camelize_rules ||= []
end
def inflections
@inflections ||= {}
end
def inflection(overrides)
inflections.merge!(overrides)
end
end
end
end

@ -1,43 +1,55 @@
module OpenProject require Rails.root.join('config/constants/open_project/inflector')
class Inflector < Zeitwerk::GemInflector
# TODO: split up into a registry OpenProject::Inflector.rule do |_, abspath|
def camelize(basename, abspath) if abspath.match?(/open_project\/version(\.rb)?\z/)
if basename =~ /\A(.*)_api\z/ "VERSION"
super($1, abspath) + 'API' end
elsif basename =~ /\Aoauth_(.*)\z/ end
'OAuth' + super($1, abspath)
elsif basename =~ /\A(.*)_oauth\z/ OpenProject::Inflector.rule do |basename, abspath|
super($1, abspath) + 'OAuth' if basename =~ /\A(.*)_api\z/
elsif basename =~ /\A(.*)_sso\z/ default_inflect($1, abspath) + 'API'
super($1, abspath) + 'SSO' end
elsif basename =~ /\Aar_(.*)\z/ end
'AR' + super($1, abspath)
elsif basename =~ /\Apdf_export\z/ OpenProject::Inflector.rule do |basename, abspath|
'PDFExport' if basename =~ /\Aar_(.*)\z/
elsif abspath =~ /open_project\/version(\.rb)?\z/ 'AR' + default_inflect($1, abspath)
"VERSION"
else
super
end
end
end end
end end
OpenProject::Inflector.rule do |basename, abspath|
if basename =~ /\Aoauth_(.*)\z/
'OAuth' + default_inflect($1, abspath)
elsif basename =~ /\A(.*)_oauth\z/
default_inflect($1, abspath) + 'OAuth'
elsif basename == 'oauth'
'OAuth'
end
end
OpenProject::Inflector.rule do |basename, abspath|
if basename =~ /\A(.*)_sso\z/
default_inflect($1, abspath) + 'SSO'
end
end
OpenProject::Inflector.inflection(
'api' => 'API',
'rss' => 'RSS',
'sha1' => 'SHA1',
'sso' => 'SSO',
'csv' => 'CSV',
'pdf' => 'PDF',
'scm' => 'SCM',
'imap' => 'IMAP',
'pop3' => 'POP3',
'openid_connect' => 'OpenIDConnect',
'pdf_export' => 'PDFExport'
)
Rails.autoloaders.each do |autoloader| Rails.autoloaders.each do |autoloader|
autoloader.inflector = OpenProject::Inflector.new(__FILE__) autoloader.inflector = OpenProject::Inflector.new(__FILE__)
autoloader.inflector.inflect(
'api' => 'API',
'rss' => 'RSS',
'sha1' => 'SHA1',
'oauth' => 'OAuth',
'sso' => 'SSO',
'csv' => 'CSV',
'pdf' => 'PDF',
'scm' => 'SCM',
'imap' => 'IMAP',
'pop3' => 'POP3',
'openid_connect' => 'OpenIDConnect'
)
end end
# Instruct zeitwerk to ignore all the engine gems' lib initialization files # Instruct zeitwerk to ignore all the engine gems' lib initialization files

@ -285,6 +285,21 @@ module OpenProject::Plugins
representer_class.prepend mod representer_class.prepend mod
end end
end end
# Add custom inflection for file name to class name mapping. Otherwise, the default zeitwerk
# #camelize method will be utilized.
#
# class_inflection_override('asap' => 'ASAP')
#
# inflector.camelize("asap", abspath) # => "ASAP"
#
# @param overrides [{String => String}]
# @return [void]
def class_inflection_override(overrides)
self.class.initializer "#{engine_name}.class_inflection_override" do
OpenProject::Inflector.inflection(overrides)
end
end
end end
end end
end end

@ -24,6 +24,8 @@ module OpenProject::OpenIDConnect
openid_connect/auth_provider-heroku.png openid_connect/auth_provider-heroku.png
) )
class_inflection_override('openid_connect' => 'OpenIDConnect')
register_auth_providers do register_auth_providers do
# Use OpenSSL default certificate store instead of HTTPClient's. # Use OpenSSL default certificate store instead of HTTPClient's.
# It's outdated and it's unclear how it's managed. # It's outdated and it's unclear how it's managed.

Loading…
Cancel
Save