Skip to content

[Rails5] Update Event#subclass_from_attributes method

What does this MR do?

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

This MR 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

Event.where(action: Event::PUSHED) returns PushEvent objects:

% RAILS5=1 rails console
Loading development environment (Rails 5.0.6)
[1] pry(main)> Event.where(action: Event::PUSHED)
  Event Load (0.7ms)  SELECT "events".* FROM "events" WHERE "events"."action" = $1  [["action", 5]]
=> [#<PushEvent:0x0000000006c07eb0 id: 10, project_id: 1, author_id: 1, target_id: nil, created_at: 2018-04-11 10:39:14 UTC, updated_at: 2018-04-11 10:39:14 UTC, action: 5, target_type: nil>]
% rails console 
Loading development environment (Rails 4.2.10)
[1] pry(main)> Event.where(action: Event::PUSHED)
  Event Load (0.6ms)  SELECT "events".* FROM "events" WHERE "events"."action" = $1  [["action", 5]]
=> [#<PushEvent:0x000000000789fc40 id: 10, project_id: 1, author_id: 1, target_id: nil, created_at: 2018-04-11 10:39:14 UTC, updated_at: 2018-04-11 10:39:14 UTC, action: 5, target_type: nil>]

Why was this MR needed?

Migration to Rails 5.0.

Screenshots (if relevant)

No.

Does this MR meet the acceptance criteria?

What are the relevant issue numbers?

#14286 (closed) and !12841 (closed)

Edited by Yorick Peterse

Merge request reports

Loading