rails icon indicating copy to clipboard operation
rails copied to clipboard

Add mapping to stored attributes

Open mechnicov opened this issue 1 month ago • 1 comments

Motivation / Background

Sometimes when call stored attribute, it requires specific naming semantics. For example when you save some data from external source. This Pull Request adds mapping for store attribute keys

Detail

Proposed changes

class User < ApplicationRecord
  store_accessor :settings, :language, { country: :country_of_residence }
end

user = User.new(country: "US")
user.country # => "US"
user.settings["country_of_residence"] # => "US"
user.settings["country_of_residence"] = "CA"
user.country # => "CA"
user.country = "NL"
user.country # => "NL"
user.settings["country_of_residence"] # => "NL"

Additional information

This was intended as non-breaking change

  • [x] This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • [x] Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • [x] Tests are added or updated if you fix a bug or add a feature.
  • [x] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

mechnicov avatar Jun 01 '24 00:06 mechnicov