From 64a80ef756ae89e85470b598e9c09b66d50a8103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Mon, 15 Jan 2007 15:11:29 +0000 Subject: Allow inGroupsOf and eachSlice to be called through rjs. Closes #7046 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5942 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ .../lib/action_view/helpers/prototype_helper.rb | 22 ++++++++++++++++++--- actionpack/test/template/prototype_helper_test.rb | 23 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 2bc9e321e6..5720d9713f 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Allow inGroupsOf and eachSlice to be called through rjs. #7046 [Cody Fauser] + * Allow exempt_from_layout :rhtml. #6742, #7026 [dcmanges, Squeegy] * Recognize the .txt extension as Mime::TEXT [Rick] diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 71fe29db9a..43d04baf8d 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -784,7 +784,7 @@ module ActionView end class JavaScriptCollectionProxy < JavaScriptProxy #:nodoc: - ENUMERABLE_METHODS_WITH_RETURN = [:all, :any, :collect, :map, :detect, :find, :find_all, :select, :max, :min, :partition, :reject, :sort_by] unless defined? ENUMERABLE_METHODS_WITH_RETURN + 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 ENUMERABLE_METHODS = ENUMERABLE_METHODS_WITH_RETURN + [:each] unless defined? ENUMERABLE_METHODS attr_reader :generator delegate :arguments_for_call, :to => :generator @@ -792,11 +792,27 @@ module ActionView def initialize(generator, pattern) super(generator, @pattern = pattern) end - + + def each_slice(variable, number, &block) + if block + enumerate :eachSlice, :variable => variable, :method_args => [number], :yield_args => %w(value index), :return => true, &block + else + add_variable_assignment!(variable) + append_enumerable_function!("eachSlice(#{number.to_json});") + end + end + def grep(variable, pattern, &block) enumerate :grep, :variable => variable, :return => true, :method_args => [pattern], :yield_args => %w(value index), &block end - + + def in_groups_of(variable, number, fill_with = nil) + arguments = [number] + arguments << fill_with unless fill_with.nil? + add_variable_assignment!(variable) + append_enumerable_function!("inGroupsOf(#{arguments_for_call arguments});") + end + def inject(variable, memo, &block) enumerate :inject, :variable => variable, :method_args => [memo], :yield_args => %w(memo value index), :return => true, &block end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 7dc366b7d9..1b4f4408e3 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -423,6 +423,29 @@ return (value.className == "welcome"); EOS end + def test_collection_proxy_with_in_groups_of + @generator.select('p').in_groups_of('a', 3) + @generator.select('p').in_groups_of('a', 3, 'x') + assert_equal <<-EOS.strip, @generator.to_s +var a = $$("p").inGroupsOf(3); +var a = $$("p").inGroupsOf(3, "x"); + EOS + end + + def test_collection_proxy_with_each_slice + @generator.select('p').each_slice('a', 3) + @generator.select('p').each_slice('a', 3) do |group, index| + group.reverse + end + + assert_equal <<-EOS.strip, @generator.to_s +var a = $$("p").eachSlice(3); +var a = $$("p").eachSlice(3, function(value, index) { +return value.reverse(); +}); + EOS + end + def test_debug_rjs ActionView::Base.debug_rjs = true @generator['welcome'].replace_html 'Welcome' -- cgit v1.2.3