Fix getting `attributes` before virtual attribute was ever read

- Lookup existing relation with ID and relation_type not nil (some
relation column > 0)

If you access in this order:
- Get `relation.attributes` -> relation_type is nil
- Get `relation.relaton_type` -> nil always after that

If you access the other way around, everything's okay:
- Get `relation.relaton_type` -> expected value
- Get `relation.attributes` -> expecte dvalue
pull/6637/head
Oliver Günther 6 years ago
parent aa9897561b
commit 460edf0122
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 9
      app/models/concerns/virtual_attribute.rb
  2. 2
      app/services/relations/create_service.rb

@ -40,10 +40,19 @@ module Concerns
_define_virtual_attribute_setter(attribute)
_define_virtual_attribute_getter(attribute, &block)
_define_virtual_attribute_reload(attribute)
_define_virtual_attributes_hook(attribute)
end
private
def _define_virtual_attributes_hook(attribute)
define_method :attributes do |*args|
# Ensure attribute has been read
send(attribute)
super(*args)
end
end
def _define_virtual_attribute_setter(attribute)
define_method "#{attribute}=" do |value|
set_virtual_attribute(attribute, value) if send(attribute) != value

@ -35,8 +35,6 @@ class Relations::CreateService < Relations::BaseService
end
def call(relation, send_notifications: true)
initialize_contract! relation
in_context(send_notifications) do
update_relation relation, {}
end

Loading…
Cancel
Save