Skip to content

Commit ea38087

Browse files
author
David Heinemeier Hansson
committed
Merge pull request rails#13253 from strzalek/variants-inline2
Variants inline syntax v2
2 parents e74a9f9 + edacdbf commit ea38087

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

actionpack/lib/action_controller/metal/mime_responds.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ class Collector
430430

431431
def initialize(mimes)
432432
@responses = {}
433-
mimes.each { |mime| send(mime) }
433+
434+
mimes.each { |mime| @responses["Mime::#{mime.upcase}".constantize] = nil }
434435
end
435436

436437
def any(*args, &block)
@@ -444,12 +445,18 @@ def any(*args, &block)
444445

445446
def custom(mime_type, &block)
446447
mime_type = Mime::Type.lookup(mime_type.to_s) unless mime_type.is_a?(Mime::Type)
447-
@responses[mime_type] ||= block
448+
@responses[mime_type] ||= if block_given?
449+
block
450+
else
451+
VariantCollector.new
452+
end
448453
end
449454

450455
def response(variant)
451456
response = @responses.fetch(format, @responses[Mime::ALL])
452-
if response.nil? || response.arity == 0
457+
if response.is_a?(VariantCollector)
458+
response.variant(variant)
459+
elsif response.nil? || response.arity == 0
453460
response
454461
else
455462
lambda { response.call VariantFilter.new(variant) }
@@ -460,6 +467,22 @@ def negotiate_format(request)
460467
@format = request.negotiate_mime(@responses.keys)
461468
end
462469

470+
#Used for inline syntax
471+
class VariantCollector #:nodoc:
472+
def initialize
473+
@variants = {}
474+
end
475+
476+
def method_missing(name, *args, &block)
477+
@variants[name] = block if block_given?
478+
end
479+
480+
def variant(name)
481+
@variants[name.nil? ? :none : name]
482+
end
483+
end
484+
485+
#Used for nested block syntax
463486
class VariantFilter #:nodoc:
464487
def initialize(variant)
465488
@variant = variant

actionpack/test/controller/mime/respond_to_test.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ def variant_plus_none_for_format
175175
end
176176
end
177177

178+
def variant_inline_syntax
179+
respond_to do |format|
180+
format.js { render text: "js" }
181+
format.html.none { render text: "none" }
182+
format.html.phone { render text: "phone" }
183+
end
184+
end
185+
186+
def variant_inline_syntax_without_block
187+
respond_to do |format|
188+
format.js
189+
format.html.none
190+
format.html.phone
191+
end
192+
end
193+
178194
protected
179195
def set_layout
180196
case action_name
@@ -554,10 +570,31 @@ def test_multiple_variants_for_format
554570
assert_equal "tablet", @response.body
555571
end
556572

557-
558573
def test_no_variant_in_variant_setup
559574
get :variant_plus_none_for_format
560575
assert_equal "text/html", @response.content_type
561576
assert_equal "none", @response.body
562577
end
578+
579+
def test_variant_inline_syntax
580+
get :variant_inline_syntax, format: :js
581+
assert_equal "text/javascript", @response.content_type
582+
assert_equal "js", @response.body
583+
584+
get :variant_inline_syntax
585+
assert_equal "text/html", @response.content_type
586+
assert_equal "none", @response.body
587+
588+
@request.variant = :phone
589+
get :variant_inline_syntax
590+
assert_equal "text/html", @response.content_type
591+
assert_equal "phone", @response.body
592+
end
593+
594+
def test_variant_inline_syntax_without_block
595+
@request.variant = :phone
596+
get :variant_inline_syntax_without_block
597+
assert_equal "text/html", @response.content_type
598+
assert_equal "phone", @response.body
599+
end
563600
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
phone

0 commit comments

Comments
 (0)