mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 10:20:43 -07:00
Switch to aliasing methods
This commit is contained in:
@@ -80,3 +80,7 @@ require 'liquid/usage'
|
|||||||
# Load all the tags of the standard library
|
# Load all the tags of the standard library
|
||||||
#
|
#
|
||||||
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
|
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
|
||||||
|
|
||||||
|
# Load all usage tracking
|
||||||
|
#
|
||||||
|
Dir["#{__dir__}/liquid/usages/*.rb"].each { |f| require f }
|
||||||
|
|||||||
+5
-1
@@ -4,7 +4,11 @@ module Liquid
|
|||||||
@messages = {}
|
@messages = {}
|
||||||
class << self
|
class << self
|
||||||
def enable
|
def enable
|
||||||
Dir["#{__dir__}/usages/*.rb"].each { |f| require f }
|
Liquid::Context.send(:alias_method, :try_variable_find_in_environments, :try_variable_find_in_environments_usage)
|
||||||
|
end
|
||||||
|
|
||||||
|
def disable
|
||||||
|
Liquid::Context.send(:alias_method, :try_variable_find_in_environments, :try_variable_find_in_environments_original)
|
||||||
end
|
end
|
||||||
|
|
||||||
def track(message)
|
def track(message)
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class Context
|
class Context
|
||||||
remove_method :try_variable_find_in_environments
|
alias try_variable_find_in_environments_original try_variable_find_in_environments
|
||||||
def try_variable_find_in_environments(key, raise_on_not_found:)
|
|
||||||
|
def try_variable_find_in_environments_usage(key, raise_on_not_found:)
|
||||||
Usage.track("Using try_variable_find_in_environment")
|
Usage.track("Using try_variable_find_in_environment")
|
||||||
@environments.each do |environment|
|
@environments.each do |environment|
|
||||||
found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found)
|
found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found)
|
||||||
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
||||||
return found_variable
|
return found_variable
|
||||||
end
|
end
|
||||||
|
|
||||||
Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key)
|
Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key)
|
||||||
end
|
end
|
||||||
@static_environments.each do |environment|
|
@static_environments.each do |environment|
|
||||||
@@ -15,6 +17,7 @@ module Liquid
|
|||||||
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
||||||
return found_variable
|
return found_variable
|
||||||
end
|
end
|
||||||
|
|
||||||
Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key)
|
Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key)
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module Liquid
|
|
||||||
class TestUsage < Usage
|
|
||||||
@messages = {}
|
|
||||||
class << self
|
|
||||||
def enable
|
|
||||||
Dir["#{__dir__}/usages/*.rb"].each { |f| require f }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class UsageTest < Minitest::Test
|
|
||||||
include Liquid
|
|
||||||
|
|
||||||
Usage.enable
|
|
||||||
|
|
||||||
def test_test_usages
|
|
||||||
Dir["#{__dir__}/usages/*.rb"].each { |f| require f }
|
|
||||||
|
|
||||||
template = Template.parse(%({{test}}))
|
|
||||||
assert_equal 'worked', template.render!('test' => 'worked')
|
|
||||||
assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully')
|
|
||||||
assert_equal true, Usage.results["Using try_variable_find_in_environment"]
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_live_usages
|
|
||||||
template = Template.parse(%({{test}}))
|
|
||||||
assert_equal 'worked', template.render!('test' => 'worked')
|
|
||||||
assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully')
|
|
||||||
assert_equal true, Usage.results["Usage is enabled"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class TryVariablesUsageTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_test_usages
|
||||||
|
Usage.enable
|
||||||
|
template = Template.parse(%({{test}}))
|
||||||
|
assert_equal 'worked', template.render!('test' => 'worked')
|
||||||
|
assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully')
|
||||||
|
assert_equal true, Usage.results["Using try_variable_find_in_environment"]
|
||||||
|
Usage.disable
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class UsageEnabledUsageTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_live_usages
|
||||||
|
assert_equal true, Usage.results["Usage is enabled"]
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user