
Share your cancer code!
Today I was doing a read-through of existing code to prepare for an upcoming feature, and I came across this abomination. # controller code # GET /project_team_tasks def show @teams = project . teams TeamTask . define_teams_quantity_distributions_attr_readers ( @teams ) end # model code def self . define_teams_quantity_distributions_attr_readers ( teams ) teams . each do | team | Budget . component_groups . each do | component_group | define_method ( "team_ #{ team . id } _assembled_elements_quantity_ #{ component_group } " ) do teams_quantity_distributions [ "team_ #{ team . id } _assembled_elements_quantity_ #{ component_group } " ] end end end end # then in view team . tasks . send ( "team_ #{ team . id } _assembled_elements_quantity_1" ) As far as I understand, calling the class method defines an instance method (a getter) for every team*component_group pair. Apparently the underlying data are stored in TeamTask instance teams_quantity_distributions jsonb field. This code is pernic
Continue reading on Dev.to
Opens in a new tab



