mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Remove support for liquid_methods Module extension
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# Liquid Change Log
|
# Liquid Change Log
|
||||||
|
|
||||||
## 4.0.0 / not yet released / branch "master"
|
## 4.0.0 / not yet released / branch "master"
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Add sort_natural filter (#554) [Martin Hanzel, arthanzel]
|
* Add sort_natural filter (#554) [Martin Hanzel, arthanzel]
|
||||||
* Add forloop.parentloop as a reference to the parent loop (#520) [Justin Li, pushrax]
|
* Add forloop.parentloop as a reference to the parent loop (#520) [Justin Li, pushrax]
|
||||||
@@ -8,6 +9,7 @@
|
|||||||
* Add concat filter to concatenate arrays (#429) [Diogo Beato, dvbeato]
|
* Add concat filter to concatenate arrays (#429) [Diogo Beato, dvbeato]
|
||||||
* Ruby 1.9 support dropped (#491) [Justin Li, pushrax]
|
* Ruby 1.9 support dropped (#491) [Justin Li, pushrax]
|
||||||
* Liquid::Template.file_system's read_template_file method is no longer passed the context. (#441) [James Reid-Smith, sunblaze]
|
* Liquid::Template.file_system's read_template_file method is no longer passed the context. (#441) [James Reid-Smith, sunblaze]
|
||||||
|
* Remove support for `liquid_methods`
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
* Fix naming of the "context variable" when dynamically including a template (#559) [Justin Li, pushrax]
|
* Fix naming of the "context variable" when dynamically including a template (#559) [Justin Li, pushrax]
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ require 'liquid/resource_limits'
|
|||||||
require 'liquid/template'
|
require 'liquid/template'
|
||||||
require 'liquid/standardfilters'
|
require 'liquid/standardfilters'
|
||||||
require 'liquid/condition'
|
require 'liquid/condition'
|
||||||
require 'liquid/module_ex'
|
|
||||||
require 'liquid/utils'
|
require 'liquid/utils'
|
||||||
require 'liquid/token'
|
require 'liquid/token'
|
||||||
|
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
# Copyright 2007 by Domizio Demichelis
|
|
||||||
# This library is free software. It may be used, redistributed and/or modified
|
|
||||||
# under the same terms as Ruby itself
|
|
||||||
#
|
|
||||||
# This extension is used in order to expose the object of the implementing class
|
|
||||||
# to liquid as it were a Drop. It also limits the liquid-callable methods of the instance
|
|
||||||
# to the allowed method passed with the liquid_methods call
|
|
||||||
# Example:
|
|
||||||
#
|
|
||||||
# class SomeClass
|
|
||||||
# liquid_methods :an_allowed_method
|
|
||||||
#
|
|
||||||
# def an_allowed_method
|
|
||||||
# 'this comes from an allowed method'
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# def unallowed_method
|
|
||||||
# 'this will never be an output'
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# if you want to extend the drop to other methods you can defines more methods
|
|
||||||
# in the class <YourClass>::LiquidDropClass
|
|
||||||
#
|
|
||||||
# class SomeClass::LiquidDropClass
|
|
||||||
# def another_allowed_method
|
|
||||||
# 'and this from another allowed method'
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
#
|
|
||||||
# usage:
|
|
||||||
# @something = SomeClass.new
|
|
||||||
#
|
|
||||||
# template:
|
|
||||||
# {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}}
|
|
||||||
#
|
|
||||||
# output:
|
|
||||||
# 'this comes from an allowed method and this from another allowed method'
|
|
||||||
#
|
|
||||||
# You can also chain associations, by adding the liquid_method call in the
|
|
||||||
# association models.
|
|
||||||
#
|
|
||||||
class Module
|
|
||||||
def liquid_methods(*allowed_methods)
|
|
||||||
drop_class = eval "class #{self}::LiquidDropClass < Liquid::Drop; self; end"
|
|
||||||
|
|
||||||
define_method :to_liquid do
|
|
||||||
drop_class.new(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
drop_class.class_eval do
|
|
||||||
def initialize(object)
|
|
||||||
@object = object
|
|
||||||
end
|
|
||||||
|
|
||||||
allowed_methods.each do |sym|
|
|
||||||
define_method sym do
|
|
||||||
@object.send sym
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -15,12 +15,10 @@ class TemplateContextDrop < Liquid::Drop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class SomethingWithLength
|
class SomethingWithLength < Liquid::Drop
|
||||||
def length
|
def length
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
liquid_methods :length
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class ErroneousDrop < Liquid::Drop
|
class ErroneousDrop < Liquid::Drop
|
||||||
|
|||||||
@@ -1,90 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
class TestClassA
|
|
||||||
liquid_methods :allowedA, :chainedB
|
|
||||||
def allowedA
|
|
||||||
'allowedA'
|
|
||||||
end
|
|
||||||
|
|
||||||
def restrictedA
|
|
||||||
'restrictedA'
|
|
||||||
end
|
|
||||||
|
|
||||||
def chainedB
|
|
||||||
TestClassB.new
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class TestClassB
|
|
||||||
liquid_methods :allowedB, :chainedC
|
|
||||||
def allowedB
|
|
||||||
'allowedB'
|
|
||||||
end
|
|
||||||
|
|
||||||
def chainedC
|
|
||||||
TestClassC.new
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class TestClassC
|
|
||||||
liquid_methods :allowedC
|
|
||||||
def allowedC
|
|
||||||
'allowedC'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class TestClassC::LiquidDropClass
|
|
||||||
def another_allowedC
|
|
||||||
'another_allowedC'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModuleExUnitTest < Minitest::Test
|
|
||||||
include Liquid
|
|
||||||
|
|
||||||
def setup
|
|
||||||
@a = TestClassA.new
|
|
||||||
@b = TestClassB.new
|
|
||||||
@c = TestClassC.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_should_create_LiquidDropClass
|
|
||||||
assert TestClassA::LiquidDropClass
|
|
||||||
assert TestClassB::LiquidDropClass
|
|
||||||
assert TestClassC::LiquidDropClass
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_should_respond_to_liquid
|
|
||||||
assert @a.respond_to?(:to_liquid)
|
|
||||||
assert @b.respond_to?(:to_liquid)
|
|
||||||
assert @c.respond_to?(:to_liquid)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_should_return_LiquidDropClass_object
|
|
||||||
assert @a.to_liquid.is_a?(TestClassA::LiquidDropClass)
|
|
||||||
assert @b.to_liquid.is_a?(TestClassB::LiquidDropClass)
|
|
||||||
assert @c.to_liquid.is_a?(TestClassC::LiquidDropClass)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_should_respond_to_liquid_methods
|
|
||||||
assert @a.to_liquid.respond_to?(:allowedA)
|
|
||||||
assert @a.to_liquid.respond_to?(:chainedB)
|
|
||||||
assert @b.to_liquid.respond_to?(:allowedB)
|
|
||||||
assert @b.to_liquid.respond_to?(:chainedC)
|
|
||||||
assert @c.to_liquid.respond_to?(:allowedC)
|
|
||||||
assert @c.to_liquid.respond_to?(:another_allowedC)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_should_not_respond_to_restricted_methods
|
|
||||||
assert ! @a.to_liquid.respond_to?(:restricted)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_should_use_regular_objects_as_drops
|
|
||||||
assert_template_result 'allowedA', "{{ a.allowedA }}", 'a' => @a
|
|
||||||
assert_template_result 'allowedB', "{{ a.chainedB.allowedB }}", 'a' => @a
|
|
||||||
assert_template_result 'allowedC', "{{ a.chainedB.chainedC.allowedC }}", 'a' => @a
|
|
||||||
assert_template_result 'another_allowedC', "{{ a.chainedB.chainedC.another_allowedC }}", 'a' => @a
|
|
||||||
assert_template_result '', "{{ a.restricted }}", 'a' => @a
|
|
||||||
assert_template_result '', "{{ a.unknown }}", 'a' => @a
|
|
||||||
end
|
|
||||||
end # ModuleExTest
|
|
||||||
Reference in New Issue
Block a user