brainspec / enumerize

Enumerated attributes with I18n and ActiveRecord/Mongoid support
MIT License
1.72k stars 190 forks source link

#as_json on Enumerize::Value instance not returns String #436

Closed atilla777 closed 8 months ago

atilla777 commented 10 months ago

Active Support has an #as_json definition for String (as monkey patch) that returns self (String instance). The Enumerize::Value class is inherited from String and does not have its own #as_json definition. So, when we call as_json on an Enumerize::Value instance, we get this instance instead of its String representation, which can be used as a valid JSON value. This can be a problem, for example, when we call as_json on a Rails model record that represents this record as valid JSON, which will be used as an argument in Sidekiq.perform_async (this happens when Sidekiq.strict_args! is set). This problem can be solved in this way:

module Enumerize
  class Value
    def as_json(_options = nil) = to_s
  end
end