the action liquid tag handles file upload
did
committed Sep 25, 2016
commit 269b38fdcc95bbbcf45270bf16c7a4755cdece37
Showing 3
changed files with
44 additions
and 1 deletions
locomotive/steam/liquid/tags/action.rb b/lib/locomotive/steam/liquid/tags/action.rb
+19
-1
| @@ | @@ -38,7 +38,7 @@ module Locomotive |
| def render(context) | |
| Locomotive::Common::Logger.info "[action] executing #{@description}" | |
| - | service(context).run(super, context['params'], context) |
| + | service(context).run(super, safe_params(context), context) |
| '' | |
| end | |
| @@ | @@ -48,6 +48,24 @@ module Locomotive |
| context.registers[:services].action | |
| end | |
| + | def safe_params(context) |
| + | return {} if context['params'].blank? |
| + | |
| + | context['params'].dup.tap do |params| |
| + | # Tempfile can't be converted in Duktape for obvious reasons |
| + | replace_tempfile(params) |
| + | end |
| + | end |
| + | |
| + | def replace_tempfile(hash) |
| + | hash.each do |key, value| |
| + | case value |
| + | when Tempfile then hash[key] = value.path |
| + | when Hash then replace_tempfile(value) |
| + | end |
| + | end |
| + | end |
| + | |
| end | |
| ::Liquid::Template.register_tag('action'.freeze, Action) | |
locomotive/steam/services/content_entry_service.rb b/lib/locomotive/steam/services/content_entry_service.rb
+16
-0
| @@ | @@ -34,6 +34,8 @@ module Locomotive |
| _repository.create(entry) | |
| end | |
| + | logEntryOperation(type_slug, decorated_entry) |
| + | |
| _json_decorate(decorated_entry, as_json) | |
| end | |
| end | |
| @@ | @@ -48,6 +50,8 @@ module Locomotive |
| _repository.update(entry) | |
| end | |
| + | logEntryOperation(type_slug, decorated_entry) |
| + | |
| _json_decorate(decorated_entry, as_json) | |
| end | |
| end | |
| @@ | @@ -65,8 +69,20 @@ module Locomotive |
| content_type_repository.by_slug(slug) | |
| end | |
| + | def logger |
| + | Locomotive::Common::Logger |
| + | end |
| + | |
| private | |
| + | def logEntryOperation(type_slug, entry) |
| + | if (json = entry.as_json)['errors'].blank? |
| + | logger.info "[#{type_slug}] Entry persisted with success. #{json}" |
| + | else |
| + | logger.error "[#{type_slug}] Failed to persist entry. #{json}" |
| + | end |
| + | end |
| + | |
| def with_repository(type_or_slug) | |
| type = type_or_slug.respond_to?(:fields) ? type_or_slug : get_type(type_or_slug) | |
spec/unit/liquid/tags/action_spec.rb
+9
-0
| @@ | @@ -18,6 +18,15 @@ describe Locomotive::Steam::Liquid::Tags::Action do |
| it { subject; expect(context['foo']).to eq 42.0 } | |
| + | describe 'uploaded file' do |
| + | |
| + | let(:assigns) { { 'params' => { 'my_file' => { 'tempfile' => Tempfile.new('my_file') } } } } |
| + | let(:source) { '{% action "uploaded file" %}setProp("path", params.my_file.tempfile);{% endaction %}' } |
| + | |
| + | it { subject; expect(context['path']).to match /\/my_file/ } |
| + | |
| + | end |
| + | |
| end | |
| end | |