Permalink
Browse files

define named instance variable for crud entry/entries

  • Loading branch information...
1 parent 4daaae0 commit fd907af83f995556da11b2e4ce5d28ae47a93d0b Pascal Zumkehr committed Mar 29, 2012
View
@@ -85,8 +85,8 @@ namespace :test do
desc "Adds pagination to the test app"
task :add_pagination => :generate_crud do
list_ctrl = File.join(TEST_APP_ROOT, 'app', 'controllers', 'list_controller.rb')
- file_replace(list_ctrl, "@entries = list_entries",
- "@entries = list_entries.page(params[:page]).per(10)")
+ file_replace(list_ctrl, /def list_entries\n\s+model_scope\s*\n/,
+ "def list_entries\n model_scope.page(params[:page]).per(10)")
file_replace(File.join(TEST_APP_ROOT, 'app', 'views', 'list', 'index.html.erb'),
"<%= render 'list' %>", "<%= paginate @entries %>\n\n<%= render 'list' %>")
file_replace(File.join(TEST_APP_ROOT, 'app', 'views', 'list', 'index.html.haml'),
@@ -117,7 +117,7 @@ def destroy(&block)
# Main accessor method for the handled model entry.
def entry
- @entry ||= params[:id] ? find_entry : build_entry
+ @entry ||= set_model_ivar(params[:id] ? find_entry : build_entry)
end
# Creates a new model entry.
@@ -6,7 +6,7 @@
# the user the same list as he left it.
class ListController < ApplicationController
- helper_method :model_class, :models_label, :path_args
+ helper_method :model_class, :models_label, :entries, :path_args
delegate :model_class, :models_label, :to => 'self.class'
@@ -19,13 +19,17 @@ class ListController < ApplicationController
# GET /entries
# GET /entries.json
def index(&block)
- @entries = list_entries
- customizable_respond_with(@entries, block)
+ customizable_respond_with(entries, block)
end
protected
+
+ # Helper method to access the entries to be displayed in the current index page in an uniform way.
+ def entries
+ @entries ||= set_model_ivar(list_entries)
+ end
- # The entries to be displayed in the current index page.
+ # The base relation used to filter the entries
def list_entries
model_scope
end
@@ -60,6 +64,28 @@ def render_with_callback(action)
run_callbacks(:"render_#{action}")
render action unless performed?
end
+
+ # Get the instance variable named after the given model.
+ # Returns the instance variable of the current model if none is given.
+ # If the collection variable is required, pass true as the second argument.
+ def get_model_ivar(model = model_class, plural = false)
+ name = model_class.name.underscore
+ name = name.pluralize if plural
+ instance_variable_get(:"@#{name}")
+ end
+
+ # Sets an instance variable with the underscored class name if the given value.
+ # If the value is a collection, sets the plural name.
+ def set_model_ivar(value)
+ name = if value.respond_to?(:klass) # ActiveRecord::Relation
+ value.klass.name.pluralize
+ elsif value.respond_to?(:each) # Array
+ value.first.klass.name.pluralize
+ else
+ value.class.name
+ end
+ instance_variable_set(:"@#{name.underscore}", value)
+ end
class << self
# Callbacks
@@ -283,7 +309,7 @@ def parents
# Loads the parent entry for the given ActiveRecord class.
# By default, performs a find with the class_name_id param.
def parent_entry(clazz)
- clazz.find(params["#{clazz.name.underscore}_id"])
+ set_model_ivar(clazz.find(params["#{clazz.name.underscore}_id"]))
end
# An array of objects used in url_for and related functions.
@@ -31,6 +31,8 @@ def test_index
assert_equal 6, assigns(:entries).size
assert_equal assigns(:entries).sort_by(&:name), assigns(:entries)
assert_equal Hash.new, session[:list_params]
+ assert_equal assigns(:entries), assigns(:crud_test_models)
+ assert_respond_to assigns(:crud_test_models), :klass
end
def test_index_js
@@ -122,8 +124,14 @@ def test_index_returning
def test_new
super
assert assigns(:companions)
+ assert_equal @controller.send(:entry), assigns(:crud_test_model)
assert_equal [:before_render_new, :before_render_form], @controller.called_callbacks
end
+
+ def test_show
+ super
+ assert_equal @controller.send(:entry), assigns(:crud_test_model)
+ end
def test_show_with_custom
get :show, test_params(:id => crud_test_models(:BBBBB).id)
@@ -138,11 +146,13 @@ def test_create
def test_edit
super
+ assert_equal @controller.send(:entry), assigns(:crud_test_model)
assert_equal [:before_render_edit, :before_render_form], @controller.called_callbacks
end
def test_update
super
+ assert_equal @controller.send(:entry), assigns(:crud_test_model)
assert_equal [:before_update, :before_save, :after_save, :after_update], @controller.called_callbacks
end
@@ -15,21 +15,22 @@ def test_index
super
assert_equal test_entry.country.cities.order('countries.code, cities.name').to_a, assigns(:entries).to_a
+ assert_equal @controller.send(:entries), assigns(:cities)
assert_equal [:admin, test_entry.country], @controller.send(:parents)
+ assert_equal test_entry.country, assigns(:country)
assert_equal test_entry.country, @controller.send(:parent)
assert_equal test_entry.country.cities, @controller.send(:model_scope)
assert_equal [:admin, test_entry.country, 2], @controller.send(:path_args, 2)
end
-#
- # def test_show
- # get :show, test_params(:id => test_entry.id)
- # assert_redirected_to_index
- # end
-#
- # def test_show_with_non_existing_id_raises_RecordNotFound #not
- # get :show, test_params(:id => 9999)
- # assert_redirected_to_index
- # end
+
+ def test_show
+ super
+
+ assert_equal @controller.send(:entry), assigns(:city)
+ assert_equal [:admin, test_entry.country], @controller.send(:parents)
+ assert_equal test_entry.country, assigns(:country)
+ assert_equal test_entry.country, @controller.send(:parent)
+ end
#
def test_create
super

0 comments on commit fd907af

Please sign in to comment.