bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 116 forks source link

Avoid calling filters instance method prematurely #210

Closed banhill closed 7 years ago

banhill commented 7 years ago

I propose this change to avoid calling an instance method on a not completely initialized object. More precisely to fix the case when an if or unless condition references an attribute that hasn't been set yet.

module RoleDependant
  extend ActiveSupport::Concern

  included do
    attr_accessor :role

    delegate :admin?, to: :role
  end
end

class FooGrid
  include Datagrid, RoleDependant
  ...
  filter(..., if: :admin?)
end

FooGrid.new(role: some_role)

This gives a NoMethodError, because role is nil, when enabled? is evaluated.

bogdan commented 7 years ago

Can you add a test?

banhill commented 7 years ago

Sure, I'll write it in the next couple of days.

Btw the build failure for ruby v2.4.0 is sporadic and can also be reproduced on master.

bogdan commented 7 years ago

Released 1.5.3 with the patch. Thanks.

banhill commented 7 years ago

No problem, glad I could help.