Strict mode of `composed_of` in Ruby on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ApplicationRecord.singleton_class.prepend(Module.new do | |
def composed_of(part_id, options = {}) | |
super(part_id, options.except(:strict)) | |
return if !options[:strict] || !options[:mapping] | |
options[:mapping].flatten.each_slice(2) do |attr_name, _| | |
next if attr_name.to_sym == part_id.to_sym | |
define_method("#{attr_name}=") do |_val| | |
raise "Can't change the attributes mapped to `#{part_id}` directly." | |
end | |
end | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment