Permalink
Browse files

Added [resource]_table and [resource]_row helpers modeled after the l…

…ist helpers
  • Loading branch information...
1 parent 8303e42 commit 80ab1778386dc7f4c6df9ceab5b03aa2ef3f2282 @ingoweiss ingoweiss committed Oct 9, 2008
Showing with 112 additions and 1 deletion.
  1. +35 −1 lib/resourceful_views.rb
  2. +77 −0 spec/table_helpers_spec.rb
View
@@ -27,6 +27,7 @@ def build_and_install_helpers_for_resource(resource) # :nodoc:
build_edit_helper(resource)
build_destroy_helper(resource)
build_list_helpers(resource)
+ build_table_helpers(resource)
build_create_helper(resource)
build_update_helper(resource)
install_helpers
@@ -344,8 +345,41 @@ def #{resource.singular}_item(*args, &block)
end
end_eval
end
+
+
+
+ # Build the table helpers
+ #
+ # === Examples
+ #
+ # <% user_table do %>
+ # ...
+ # <%- end -%>
+ #
+ # renders:
+ #
+ # <table class="user_table">
+ # ...
+ # </table>
+ #
+ def build_table_helpers(resource)
+ @module.module_eval <<-end_eval
+ def #{resource.singular}_table(opts={}, &block)
+ content = capture(&block)
+ opts[:class] = ResourcefulViews.resourceful_classnames('#{resource.singular}_table', *(opts.delete(:class) || '').split)
+ concat(content_tag(:table, content, opts), block.binding)
+ end
+ def #{resource.singular}_row(*args, &block)
+ opts = args.extract_options!
+ opts[:class] = ResourcefulViews.resourceful_classnames('#{resource.singular}', *(opts.delete(:class) || '').split)
+ opts[:id] = '#{resource.singular}_' + args.first.id.to_s unless args.empty?
+ content = capture(&block)
+ concat(content_tag(:tr, content, opts), block.binding)
+ end
+ end_eval
+ end
+
-
# Build the 'create_[resource]' helper
#
View
@@ -0,0 +1,77 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe 'resource_table' do
+
+ before do
+ ActionController::Routing::Routes.draw do |map|
+ map.resources :users
+ end
+ @view = ActionView::Base.new
+ end
+
+ it "should render a table with CLASS 'resource_table'" do
+ _erbout = ''
+ @view.user_table do
+ _erbout << 'some-content'
+ end
+ _erbout.should have_tag('table.user_table', 'some-content')
+ end
+
+ it "should allow passing in additional classnames" do
+ _erbout = ''
+ @view.user_table(:class => 'online') do
+ _erbout << 'some-content'
+ end
+ _erbout.should have_tag('table.user_table.online.online_user_table', 'some-content')
+ end
+
+ it "should allow giving the table an id via the :id option" do
+ _erbout = ''
+ @view.user_table(:id => 'online_users') do
+ _erbout << 'some-content'
+ end
+ _erbout.should have_tag('table#online_users', 'some-content')
+ end
+
+end
+
+
+describe 'resource_row' do
+
+ before do
+ ActionController::Routing::Routes.draw do |map|
+ map.resources :users
+ end
+ @view = ActionView::Base.new
+ end
+
+ it "should a list item with resource class" do
+ _erbout = ''
+ @view.user_row do
+ _erbout << 'some-content'
+ end
+ _erbout.should have_tag('tr.user', 'some-content')
+ end
+
+ it "should allow passing in additional classes" do
+ _erbout = ''
+ @view.user_row(:class => 'online') do
+ _erbout << 'some-content'
+ end
+ _erbout.should have_tag('tr.user.online.online_user', 'some-content')
+ end
+
+ it "should allow passing in a model, the id of which will be used for the id" do
+ _erbout = ''
+ user = mock('user', :id => 1)
+ @view.user_row(user) do
+ _erbout << 'some-content'
+ end
+ _erbout.should have_tag('tr.user#user_1', 'some-content')
+ end
+
+end
+
+
+
+

0 comments on commit 80ab177

Please sign in to comment.