Skip to content

Instantly share code, notes, and snippets.

@mallain
Created June 7, 2010 09:29
Show Gist options
  • Save mallain/428454 to your computer and use it in GitHub Desktop.
Save mallain/428454 to your computer and use it in GitHub Desktop.
<% form_tag :controller => '/home', :action => 'agency_feedback' do -%>
<%= select("home", "agency", agencies_optgroup(current_agency)) %>
<%= select("home", "feedback", @feedbacks.collect {|p| [ p.formated, p.id ] }, {:selected => current_time.id}) %>
<br />
<%= submit_tag 'Change view' %>
<% end -%>
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
filter_parameter_logging :password, :password_confirmation
helper_method :current_user_session, :current_user, :current_agency, :current_time
before_filter :load_agency_time
layout 'application'
private
# Generate a edit or new path
# model as String
# submenu as Symbol
# return new_or_edit_path
def new_or_edit_ressource(model, submenu)
result = ''
@model = model.classify.constantize
@object = @model.by_agency_and_feedback(current_agency, current_time)
ressource = [submenu]
if @object.size == 0
ressource << @model.new
result = new_polymorphic_path(ressource)
else
ressource << @object.first
result = edit_polymorphic_path(ressource)
end
result
end
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def current_agency
#TODO Initialize variable agency_id from last validation feedback
session[:agency] ? agency_id = session[:agency] : agency_id = Agency.first.id
@current_agency = Agency.find(agency_id)
return @current_agency if defined?(current_user)
end
def current_time
#TODO Initialize variable feedback_id from last validation feedback
session[:feedback] ? feedback_id = session[:feedback] : feedback_id = Feedback.last.id
@current_time = Feedback.find(feedback_id)
return @current_time if defined?(current_user)
end
def load_agency_time
@agencies = Agency.all
@feedbacks = Feedback.all
end
def require_user
unless current_user
flash[:notice] = "You must be logged in to access this page"
redirect_to login_path
return false
end
end
def require_no_user
if current_user
flash[:notice] = "You must be logged out to access this page"
redirect_to root_url
return false
end
end
end
51) Error:
test: logged in on GET to :show should respond with success. (Admin::SubcontractTypesControllerTest):
ActionView::TemplateError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
On line #3 of app/views/shared/_agency_time_form.html.erb
1: <% form_tag :controller => '/home', :action => 'agency_feedback' do -%>
2:
3: <%= select("home", "agency", agencies_optgroup(current_agency)) %>
4: <%= select("home", "feedback", @feedbacks.collect {|p| [ p.formated, p.id ] }, {:selected => current_time.id}) %>
5: <br />
6:
app/controllers/application_controller.rb:53:in `current_agency'
(eval):2:in `send'
(eval):2:in `current_agency'
app/views/shared/_agency_time_form.html.erb:3
app/views/shared/_agency_time_form.html.erb:1
app/views/layouts/application.html.erb:25
vendor/gems/resource_controller-0.6.6/lib/resource_controller/helpers/internal.rb:12:in `response_for'
vendor/gems/resource_controller-0.6.6/lib/resource_controller/actions.rb:13:in `show'
/test/functional/admin/subcontract_types_controller_test.rb:20:in `__bind_1275902761_939878'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `each'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:361:in `test: logged in on GET to :show should respond with success. '
/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
/usr/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in `run_suite'
/usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:67:in `start_mediator'
/usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:41:in `start'
/usr/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:29:in `run'
/usr/lib/ruby/1.8/test/unit/autorunner.rb:216:in `run'
/usr/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
/usr/lib/ruby/1.8/test/unit.rb:279
rake (0.8.7) lib/rake/rake_test_loader.rb:5
class Admin::SubcontractTypesController < Admin::AdminController
resource_controller
end
require 'test_helper'
class Admin::SubcontractTypesControllerTest < ActionController::TestCase
context "logged in" do
setup do
UserSession.create(Factory(:user))
@subcontract_type = Factory(:subcontract_type)
end
context "on GET to :index" do
setup do
get :index
end
should_respond_with :success
end
#TODO How to include helpers in ControllerTest ?
context "on GET to :show" do
setup do
get :show, {:id => @subcontract_type.id}
end
should_respond_with :success
end
context "on GET to :new" do
setup do
get :new
end
should_render_a_form
should_respond_with :success
end
end
end
@RobertDober
Copy link

def current_agency
return nil unless defined? current_user

agency_id = session[:agency] || Agency.first.id
@current_agency = Agency.find(agency_id)
end

@RobertDober
Copy link

Avec CRs

def current_agency

return nil unless defined? current_user

agency_id = session[:agency] || Agency.first.id

@current_agency = Agency.find(agency_id)

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment