4
4
require 'active_support/core_ext/string/output_safety'
5
5
6
6
module ActionView
7
+ # A string that returns itself as its JSON-encoded form.
8
+ class JsonLiteral < String
9
+ def as_json ( options = nil ) self end #:nodoc:
10
+ def encode_json ( encoder ) self end #:nodoc:
11
+ end
12
+
7
13
# = Action View Prototype Helpers
8
14
module Helpers
9
15
# Prototype[http://www.prototypejs.org/] is a JavaScript library that provides
@@ -147,6 +153,7 @@ def remote_function(options)
147
153
class JavaScriptGenerator #:nodoc:
148
154
def initialize ( context , &block ) #:nodoc:
149
155
@context , @lines = context , [ ]
156
+ def @lines . encoding ( ) last . to_s . encoding end
150
157
include_helpers_from_context
151
158
@context . with_output_buffer ( @lines ) do
152
159
@context . instance_exec ( self , &block )
@@ -155,7 +162,7 @@ def initialize(context, &block) #:nodoc:
155
162
156
163
private
157
164
def include_helpers_from_context
158
- extend @context . helpers if @context . respond_to? ( :helpers ) && @context . helpers
165
+ extend @context . controller . _helpers if @context . controller . respond_to? ( :_helpers ) && @context . controller . _helpers
159
166
extend GeneratorMethods
160
167
end
161
168
@@ -253,14 +260,21 @@ def [](id)
253
260
when String , Symbol , NilClass
254
261
JavaScriptElementProxy . new ( self , id )
255
262
else
256
- JavaScriptElementProxy . new ( self , ActionController :: RecordIdentifier . dom_id ( id ) )
263
+ JavaScriptElementProxy . new ( self , RecordIdentifier . dom_id ( id ) )
257
264
end
258
265
end
259
266
267
+ RecordIdentifier =
268
+ if defined? ActionView ::RecordIdentifier
269
+ ActionView ::RecordIdentifier
270
+ else
271
+ ActionController ::RecordIdentifier
272
+ end
273
+
260
274
# Returns an object whose <tt>to_json</tt> evaluates to +code+. Use this to pass a literal JavaScript
261
275
# expression as an argument to another JavaScriptGenerator method.
262
276
def literal ( code )
263
- :: ActiveSupport :: JSON :: Variable . new ( code . to_s )
277
+ JsonLiteral . new ( code . to_s )
264
278
end
265
279
266
280
# Returns a collection reference by finding it through a CSS +pattern+ in the DOM. This collection can then be
@@ -517,6 +531,15 @@ def delay(seconds = 1)
517
531
record "}, #{ ( seconds * 1000 ) . to_i } )"
518
532
end
519
533
534
+ def javascript_object_for ( object )
535
+ ::ActiveSupport ::JSON . encode ( object )
536
+ end
537
+
538
+ def arguments_for_call ( arguments , block = nil )
539
+ arguments << block_to_function ( block ) if block
540
+ arguments . map { |argument | javascript_object_for ( argument ) } . join ', '
541
+ end
542
+
520
543
private
521
544
def loop_on_multiple_args ( method , ids )
522
545
record ( ids . size >1 ?
@@ -547,7 +570,7 @@ def render(*options)
547
570
548
571
def with_formats ( *args )
549
572
return yield unless @context
550
-
573
+
551
574
lookup = @context . lookup_context
552
575
begin
553
576
old_formats , lookup . formats = lookup . formats , args
@@ -557,15 +580,6 @@ def with_formats(*args)
557
580
end
558
581
end
559
582
560
- def javascript_object_for ( object )
561
- ::ActiveSupport ::JSON . encode ( object )
562
- end
563
-
564
- def arguments_for_call ( arguments , block = nil )
565
- arguments << block_to_function ( block ) if block
566
- arguments . map { |argument | javascript_object_for ( argument ) } . join ', '
567
- end
568
-
569
583
def block_to_function ( block )
570
584
generator = self . class . new ( @context , &block )
571
585
literal ( "function() { #{ generator . to_s } }" )
@@ -655,7 +669,6 @@ def build_callbacks(options)
655
669
656
670
# Converts chained method calls on DOM proxy elements into JavaScript chains
657
671
class JavaScriptProxy < ActiveSupport ::ProxyObject #:nodoc:
658
-
659
672
def initialize ( generator , root = nil )
660
673
@generator = generator
661
674
@generator << root if root
@@ -730,7 +743,7 @@ def reload(options_for_replace = {})
730
743
731
744
class JavaScriptVariableProxy < JavaScriptProxy #:nodoc:
732
745
def initialize ( generator , variable )
733
- @variable = :: ActiveSupport :: JSON :: Variable . new ( variable )
746
+ @variable = JsonLiteral . new ( variable )
734
747
@empty = true # only record lines if we have to. gets rid of unnecessary linebreaks
735
748
super ( generator )
736
749
end
@@ -754,8 +767,8 @@ def append_to_function_chain!(call)
754
767
end
755
768
756
769
class JavaScriptCollectionProxy < JavaScriptProxy #:nodoc:
757
- ENUMERABLE_METHODS_WITH_RETURN = [ :all , :any , :collect , :map , :detect , :find , :find_all , :select , :max , :min , :partition , :reject , :sort_by , :in_groups_of , :each_slice ] unless defined? ENUMERABLE_METHODS_WITH_RETURN
758
- ENUMERABLE_METHODS = ENUMERABLE_METHODS_WITH_RETURN + [ :each ] unless defined? ENUMERABLE_METHODS
770
+ ENUMERABLE_METHODS_WITH_RETURN = [ :all , :any , :collect , :map , :detect , :find , :find_all , :select , :max , :min , :partition , :reject , :sort_by , :in_groups_of , :each_slice ]
771
+ ENUMERABLE_METHODS = ENUMERABLE_METHODS_WITH_RETURN + [ :each ]
759
772
attr_reader :generator
760
773
delegate :arguments_for_call , :to => :generator
761
774
@@ -773,7 +786,7 @@ def each_slice(variable, number, &block)
773
786
end
774
787
775
788
def grep ( variable , pattern , &block )
776
- enumerate :grep , :variable => variable , :return => true , :method_args => [ :: ActiveSupport :: JSON :: Variable . new ( pattern . inspect ) ] , :yield_args => %w( value index ) , &block
789
+ enumerate :grep , :variable => variable , :return => true , :method_args => [ JsonLiteral . new ( pattern . inspect ) ] , :yield_args => %w( value index ) , &block
777
790
end
778
791
779
792
def in_groups_of ( variable , number , fill_with = nil )
@@ -797,7 +810,7 @@ def zip(variable, *arguments, &block)
797
810
append_enumerable_function! ( "zip(#{ arguments_for_call arguments } " )
798
811
if block
799
812
function_chain [ -1 ] += ", function(array) {"
800
- yield :: ActiveSupport :: JSON :: Variable . new ( 'array' )
813
+ yield JsonLiteral . new ( 'array' )
801
814
add_return_statement!
802
815
@generator << '});'
803
816
else
@@ -810,7 +823,7 @@ def method_missing(method, *arguments, &block)
810
823
if ENUMERABLE_METHODS . include? ( method )
811
824
returnable = ENUMERABLE_METHODS_WITH_RETURN . include? ( method )
812
825
variable = arguments . first if returnable
813
- enumerate ( method , { :variable => ( arguments . first if returnable ) , :return => returnable , :yield_args => %w( value index ) } , &block )
826
+ enumerate ( method , { :variable => variable , :return => returnable , :yield_args => %w( value index ) } , &block )
814
827
else
815
828
super
816
829
end
0 commit comments