Skip to content

Inconsistent overriding of previously-defined helpers  #1213

@mwpastore

Description

@mwpastore

There's a slight inconsistency with the order in which Sinatra applies helpers when those helpers may have been previously defined. This is probably best explained with an example:

module HelperOne
  def one; '1' end
end

class MockApp < Sinatra::Base
  helpers do
    def one; nil end
    def two; nil end
  end

  helpers ::HelperOne do
    def two; '2' end
  end

  get('/one') { one }
  get('/two') { two }
end

One might reasonably expect one of two outcomes from the above:

  1. /one returns "1" and /two returns "2" (i.e. the later-defined helpers override the previously-defined helpers)
  2. /one returns nil and /two returns nil (i.e. the previously-defined helpers are "sticky" and cannot be overridden)

But what actually happens is:

  1. /one returns nil and /two returns "2"

So previously-defined helpers can be overridden in the block, but not in an included helpers module. This is inconsistent and confusing. To fix it, let's prepend the module(s) instead of include-ing them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions