[Rails5] Update Event#subclass_from_attributes method

In Rails 5.0 the `ActiveRecord::Inheritance::subclass_from_attributes`
method was updated.
Now it calls the `find_sti_class` method [1] which is overriden in the `Event`
model and returns needed class (`Event` vs `PushEvent`).

[1]: https://github.com/rails/rails/blob/5-0-stable/activerecord/lib/active_record/inheritance.rb#L209

This commit fixes the errors like
```
143) User#contributed_projects doesn't include IDs for unrelated projects
      Failure/Error: action = attrs.with_indifferent_access[inheritance_column].to_i

      NoMethodError:
        undefined method `with_indifferent_access' for nil:NilClass
      # ./app/models/event.rb:118:in `subclass_from_attributes'
```
which are raised on the `RAILS5=1 rspec ...` command.
This commit is contained in:
blackst0ne 2018-04-11 22:16:59 +11:00
parent fc7d76ae58
commit c57cddf5b9
1 changed files with 3 additions and 0 deletions

View File

@ -110,7 +110,10 @@ class Event < ActiveRecord::Base
end end
end end
# Remove this method when removing Gitlab.rails5? code.
def subclass_from_attributes(attrs) def subclass_from_attributes(attrs)
return super if Gitlab.rails5?
# Without this Rails will keep calling this method on the returned class, # Without this Rails will keep calling this method on the returned class,
# resulting in an infinite loop. # resulting in an infinite loop.
return unless self == Event return unless self == Event