makandra / active_type

Make any Ruby object quack like ActiveRecord
MIT License
1.09k stars 74 forks source link

`change_assocation` with only a scope proc fails with an error #168

Closed foobear closed 2 years ago

foobear commented 2 years ago

Consider the following model.

class Post < ApplicationRecord
  belongs_to :user, optional: true
end

Now consider a form model which resolves a post's user only to visible users.

class Frontend::Post < ActiveType::Record[::Post]
  change_association :user, -> { visible }
end

The above will not work, and instead result in an error:

lib/active_type/change_association.rb:18:in `merge': no implicit conversion of nil into Hash (TypeError)

Instead, passing an (empty) options hash is required to make it work:

class Frontend::Post < ActiveType::Record[::Post]
  change_association :user, -> { visible }, {}
end

I assume the culprit is that the new_options argument defaults to nil instead of {}: https://github.com/makandra/active_type/blob/v2.3.0/lib/active_type/change_association.rb#L9-L18

The issue is only revealed when passing a scope proc due to https://github.com/makandra/active_type/blob/v2.3.0/lib/active_type/change_association.rb#L11-L14

FLeinzi commented 2 years ago

Hello foobear,

your assumption seems right. I made a fix that will be released soon as version 2.3.1.