mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-13 23:37:47 +00:00
Write symbols instead of hash rockets in HAML
This commit is contained in:
@@ -102,7 +102,15 @@ class HamlUp
|
||||
entries = hash.map do |key, value|
|
||||
value = stringify(value) if value.is_a? Hash
|
||||
|
||||
"#{key.inspect} => #{value}"
|
||||
# We prefer the Ruby 1.9 hash syntax with symbols followed by a colon
|
||||
# like this:
|
||||
#
|
||||
# %button{ disabled: true, "ng-class": "primary-button" }
|
||||
#
|
||||
# Symbols start with `:` which we slice off. It gets appended below.
|
||||
key = key.to_sym.inspect.slice(1..-1)
|
||||
|
||||
"#{key}: #{value}"
|
||||
end
|
||||
|
||||
"{ #{entries.join(', ')} }"
|
||||
|
||||
@@ -14,7 +14,7 @@ describe HamlUp, skip: !Gem::Dependency.new("", "~> 5.2").match?("", Haml::VERSI
|
||||
it "rewrites non-standard attribute hashes" do
|
||||
original = "%p{ng: {click: 'action', show: 'condition'}} label"
|
||||
template = call(original)
|
||||
expect(template).to eq "%p{ \"ng-click\" => 'action', \"ng-show\" => 'condition' } label"
|
||||
expect(template).to eq "%p{ \"ng-click\": 'action', \"ng-show\": 'condition' } label"
|
||||
end
|
||||
|
||||
it "preserves standard attribute hashes" do
|
||||
@@ -26,18 +26,18 @@ describe HamlUp, skip: !Gem::Dependency.new("", "~> 5.2").match?("", Haml::VERSI
|
||||
it "preserves standard attribute hashes while rewriting others" do
|
||||
original = "%p{data: {click: 'standard'}, ng: {click: 'not'}} label"
|
||||
template = call(original)
|
||||
expect(template).to eq "%p{ \"data\" => {click: 'standard'}, \"ng-click\" => 'not' } label"
|
||||
expect(template).to eq "%p{ data: {click: 'standard'}, \"ng-click\": 'not' } label"
|
||||
end
|
||||
|
||||
it "rewrites multi-line attributes" do
|
||||
original = <<~HAML
|
||||
%li{ ng: { class: "{active: selector.active}" } }
|
||||
%a{ "tooltip" => "{{selector.object.value}}", "tooltip-placement" => "bottom",
|
||||
%a{ tooltip: "{{selector.object.value}}", "tooltip-placement": "bottom",
|
||||
ng: { transclude: true, class: "{active: selector.active, 'has-tip': selector.object.value}" } }
|
||||
HAML
|
||||
expected = <<~HAML
|
||||
%li{ "ng-class" => "{active: selector.active}" }
|
||||
%a{ "tooltip" => "{{selector.object.value}}", "tooltip-placement" => "bottom", "ng-transclude" => true, "ng-class" => "{active: selector.active, 'has-tip': selector.object.value}" }
|
||||
%li{ "ng-class": "{active: selector.active}" }
|
||||
%a{ tooltip: "{{selector.object.value}}", "tooltip-placement": "bottom", "ng-transclude": true, "ng-class": "{active: selector.active, 'has-tip': selector.object.value}" }
|
||||
HAML
|
||||
template = call(original)
|
||||
expect(template).to eq expected
|
||||
|
||||
Reference in New Issue
Block a user