Skip to content
  • Sean Doyle's avatar
    e9ee137d
    Infer default `:inverse_of` option for `delegated_type` · e9ee137d
    Sean Doyle authored
    Prior to this commit, delegated types were unable to infer their inverse
    associations, so saving records built with the generated
    `#build_ASSOCIATION` methods weren't writing to all the necessary
    places. For example:
    
    ```ruby
    entry = Entry.create! entryable: Message.new(subject: "Hello world!")
    entry.message.subject # => "Hello world!"
    
    entry.build_entryable(subject: "Goodbye world!").save!
    entry.reload.message.subject # => "Hello world!"
    ```
    
    The fact that the `Entry` test model declared a `delegated_type
    :entryable` definition with `types: %w[ Message Comment ]` was never
    reciprocated in the appropriate models.
    
    In order to pass the tests, this commit needed to define the
    corresponding `has_one :entry` associations. To do so, introduce the
    `Entryable` concern in the same style as the one mentioned in the
    documentation. The same extraction is made for a `UuidEntryable` concern
    mixed into `UuidMessage` and `UuidComment`.
    
    Unfortunately, defining `delegated_type :thing, types: %w[ Post ]` was
    more tricky to fix. The `Post` test model is widely used, so defining a
    `has_one` had farther-reaching effects than intended. To resolve that
    issue, this commit redefines `:thing` to use `types: %w[ Essay ]`, which
    has much fewer unintended side effects.
    e9ee137d
    Infer default `:inverse_of` option for `delegated_type`
    Sean Doyle authored
    Prior to this commit, delegated types were unable to infer their inverse
    associations, so saving records built with the generated
    `#build_ASSOCIATION` methods weren't writing to all the necessary
    places. For example:
    
    ```ruby
    entry = Entry.create! entryable: Message.new(subject: "Hello world!")
    entry.message.subject # => "Hello world!"
    
    entry.build_entryable(subject: "Goodbye world!").save!
    entry.reload.message.subject # => "Hello world!"
    ```
    
    The fact that the `Entry` test model declared a `delegated_type
    :entryable` definition with `types: %w[ Message Comment ]` was never
    reciprocated in the appropriate models.
    
    In order to pass the tests, this commit needed to define the
    corresponding `has_one :entry` associations. To do so, introduce the
    `Entryable` concern in the same style as the one mentioned in the
    documentation. The same extraction is made for a `UuidEntryable` concern
    mixed into `UuidMessage` and `UuidComment`.
    
    Unfortunately, defining `delegated_type :thing, types: %w[ Post ]` was
    more tricky to fix. The `Post` test model is widely used, so defining a
    `has_one` had farther-reaching effects than intended. To resolve that
    issue, this commit redefines `:thing` to use `types: %w[ Essay ]`, which
    has much fewer unintended side effects.
Loading