ruby on rails - Make scope and instance method with same conditions more DRY -
say have model defines scope, conditions of used in instance method:
class thing < activerecord::base scope :complete?, -> { where( foo: true, bar: true, baz: true ) } def complete? foo? && bar? && baz? end end
if needed change definition of complete?
, have remember change both scope , instance method. there nice way consolidate these 2 definitions , make code bit more dry?
the problem gets more complex if 1 of conditions instance method:
class thing < activerecord::base scope :complete?, -> { where( foo: true, bar: true, # can't use instance method `base?` here `baz` condition # have duplicate logic: name: 'baz' ) } def baz? name == 'baz' end def complete? foo? && bar? && baz? end end
dry means "don't repeat yourself". in snippets don't see duplication whatsoever. there's literally nothing dry up.
what meant, though,
there 1 piece of information 2 methods generated. if information changes, how able change once, don't accidentally forget update other place?
well, can metaprogramming gymnastics such this:
class thing < activerecord::base fields_for_complete = { foo: true, bar: true, baz: true, } scope :complete?, -> { where(fields_for_complete) } def complete? fields_for_complete.all? |method_name, expected_value| self.public_send(method_name) == expected_value end end end
it's decide 1 easier read, understand , maintain.
me, choose first (somewhat duplicated) version. makes me think less. , it's obvious other rails programmer, might join project.
Comments
Post a Comment