Permalink
Browse files

Removed controller lookup duplication into ApplicationController

  • Loading branch information...
1 parent dba1821 commit 63bf61137105cefa5ad8d8e5a87712f7fc07ca19 @jcasimir jcasimir committed Apr 22, 2011
@@ -1,3 +1,9 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ def find_resource
+ class_name = params[:controller].singularize
+ klass = class_name.camelize.constantize
+ self.instance_variable_set "@" + class_name, klass.find(params[:id])
+ end
end
@@ -1,10 +1,11 @@
class CompaniesController < ApplicationController
+ before_filter :find_resource, :only => [:show, :destroy, :edit, :update]
+
def index
@companies = Company.all
end
def show
- @company = Company.find(params[:id])
end
def new
@@ -21,11 +22,9 @@ def create
end
def edit
- @company = Company.find(params[:id])
end
def update
- @company = Company.find(params[:id])
if @company.update_attributes(params[:company])
redirect_to @company, :notice => "Successfully updated company."
else
@@ -34,8 +33,8 @@ def update
end
def destroy
- @company = Company.find(params[:id])
@company.destroy
redirect_to companies_url, :notice => "Successfully destroyed company."
end
+
end
@@ -1,4 +1,6 @@
class EmailAddressesController < ApplicationController
+ before_filter :find_resource, :only => [:show, :destroy, :edit, :update]
+
def new
if params[:person_id]
contact = Person.find params[:person_id]
@@ -18,11 +20,9 @@ def create
end
def edit
- @email_address = EmailAddress.find(params[:id])
end
def update
- @email_address = EmailAddress.find(params[:id])
if @email_address.update_attributes(params[:email_address])
redirect_to @email_address.contact, :notice => "Successfully updated email address."
else
@@ -31,7 +31,6 @@ def update
end
def destroy
- @email_address = EmailAddress.find(params[:id])
@email_address.destroy
redirect_to @email_address.contact, :notice => "Successfully destroyed email address."
end
@@ -1,10 +1,11 @@
class PeopleController < ApplicationController
+ before_filter :find_resource, :only => [:show, :destroy, :edit, :update]
+
def index
@people = Person.all
end
def show
- @person = Person.find(params[:id])
end
def new
@@ -21,11 +22,9 @@ def create
end
def edit
- @person = Person.find(params[:id])
end
def update
- @person = Person.find(params[:id])
if @person.update_attributes(params[:person])
redirect_to @person, :notice => "Successfully updated person."
else
@@ -34,7 +33,7 @@ def update
end
def destroy
- @person = Person.find(params[:id])
+
@person.destroy
redirect_to people_url, :notice => "Successfully destroyed person."
end
@@ -1,4 +1,6 @@
class PhoneNumbersController < ApplicationController
+ before_filter :find_resource, :only => [:destroy, :edit, :update]
+
def new
if params[:person_id]
contact = Person.find params[:person_id]
@@ -18,20 +20,17 @@ def create
end
def edit
- @phone_number = PhoneNumber.find(params[:id])
end
def update
- @phone_number = PhoneNumber.find(params[:id])
if @phone_number.update_attributes(params[:phone_number])
redirect_to @phone_number.contact, :notice => "Successfully updated phone number."
else
render :action => 'edit'
end
end
- def destroy
- @phone_number = PhoneNumber.find(params[:id])
+ def destroy
@phone_number.destroy
redirect_to @phone_number.contact, :notice => "Successfully destroyed phone number."
end
@@ -1,8 +1,8 @@
- title "Companies"
-%div.companies
+.companies
- @companies.each do |company|
- div.company
+ .company
%h4= company.name
= render :partial => 'email_addresses/email_addresses', :object => company.email_addresses
= render :partial => 'phone_numbers/phone_numbers', :object => company.phone_numbers

0 comments on commit 63bf611

Please sign in to comment.