Skip to content

Commit

Permalink
Merge pull request #51550 from wynksaiddestroy/fix-custom-type-attrib…
Browse files Browse the repository at this point in the history
…ute-example

Avoid name collision with builtin postgresql money type in custom type example [ci skip]
  • Loading branch information
yahonda committed Apr 12, 2024
2 parents 20fe00d + 46c9012 commit 7d17add
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions activerecord/lib/active_record/attributes.rb
Expand Up @@ -135,7 +135,7 @@ module ClassMethods
# expected API. It is recommended that your type objects inherit from an
# existing type, or from ActiveRecord::Type::Value
#
# class MoneyType < ActiveRecord::Type::Integer
# class PriceType < ActiveRecord::Type::Integer
# def cast(value)
# if !value.kind_of?(Numeric) && value.include?('$')
# price_in_dollars = value.gsub(/\$/, '').to_f
Expand All @@ -147,11 +147,11 @@ module ClassMethods
# end
#
# # config/initializers/types.rb
# ActiveRecord::Type.register(:money, MoneyType)
# ActiveRecord::Type.register(:price, PriceType)
#
# # app/models/store_listing.rb
# class StoreListing < ActiveRecord::Base
# attribute :price_in_cents, :money
# attribute :price_in_cents, :price
# end
#
# store_listing = StoreListing.new(price_in_cents: '$10.00')
Expand All @@ -171,7 +171,7 @@ module ClassMethods
# class Money < Struct.new(:amount, :currency)
# end
#
# class MoneyType < ActiveRecord::Type::Value
# class PriceType < ActiveRecord::Type::Value
# def initialize(currency_converter:)
# @currency_converter = currency_converter
# end
Expand All @@ -186,12 +186,12 @@ module ClassMethods
# end
#
# # config/initializers/types.rb
# ActiveRecord::Type.register(:money, MoneyType)
# ActiveRecord::Type.register(:price, PriceType)
#
# # app/models/product.rb
# class Product < ActiveRecord::Base
# currency_converter = ConversionRatesFromTheInternet.new
# attribute :price_in_bitcoins, :money, currency_converter: currency_converter
# attribute :price_in_bitcoins, :price, currency_converter: currency_converter
# end
#
# Product.where(price_in_bitcoins: Money.new(5, "USD"))
Expand Down

0 comments on commit 7d17add

Please sign in to comment.