Permalink
Browse files

Update generators

  • Loading branch information...
1 parent 1a8020a commit e84d55b22a31c2e8dcfc8aa409c4f30fc77eaf00 @holysugar committed Apr 26, 2012
@@ -2,7 +2,7 @@ class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
- @users = User.all
+ @users = User.page params[:page]
respond_with(@users)
end
View
@@ -1,3 +1,6 @@
class User < ActiveRecord::Base
- attr_accessible :money, :name
+ validates :money, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0}
+ validates :name, :uniqueness => true, :presence => true
+
+ attr_accessible :name, :money
end
@@ -16,18 +16,26 @@ def generate_decorator
def controller_with_kaminari
if Kernel.const_defined?("Kaminari")
- gsub_file("app/controllers/#{table_name}_controller.rb", /(@#{singular_name} = #{class_name})\.all/) do |match|
- match + '.page params[:page]'
+ gsub_file("app/controllers/#{table_name}_controller.rb", "@#{table_name} = #{class_name}.all") do |match|
+ match.sub('.all', '.page params[:page]')
end
end
end
def fix_spec_with_responder_controller
- gsub_file "spec/controllers/#{table_name}_controller_spec.rb", "#{class_name}.any_instance.stub(:save).and_return(false)" do |match|
- "#" + match
- end
+ gsub_file "spec/controllers/#{table_name}_controller_spec.rb", "#{class_name}.any_instance.stub(:save).and_return(false)", \
+ "#{class_name}.any_instance.stub(:errors){ ActiveModel::Errors.new(#{class_name}.new).tap{|e| e.add(:id, 'something wrong')} }"
+ end
+
+ def print_notification
+ say "Please check below:"
+ say " * Add validation to app/models/#{singular_name}.rb"
+ say " * Add valid model parameter to spec/controllers/#{table_name}_controller_spec.rb"
+ say " * Migrate DB (adding constraints if needed)"
end
+ undef generate_view_specs
+
end
end
end
@@ -24,7 +24,7 @@
# User. As you add validations to User, be sure to
# update the return value of this method accordingly.
def valid_attributes
- {}
+ {name: Forgery::Basic.text, money: Random.rand(10000000)}
end
# This should return the minimal set of values that should be in the session
@@ -88,14 +88,14 @@ def valid_session
describe "with invalid params" do
it "assigns a newly created but unsaved user as @user" do
# Trigger the behavior that occurs when invalid params are submitted
- #User.any_instance.stub(:save).and_return(false)
+ User.any_instance.stub(:errors){ ActiveModel::Errors.new(User.new).tap{|e| e.add(:id, 'something wrong')} }
post :create, {:user => {}}, valid_session
assigns(:user).should be_a_new(User)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
- #User.any_instance.stub(:save).and_return(false)
+ User.any_instance.stub(:errors){ ActiveModel::Errors.new(User.new).tap{|e| e.add(:id, 'something wrong')} }
post :create, {:user => {}}, valid_session
response.should render_template("new")
end
@@ -131,15 +131,15 @@ def valid_session
it "assigns the user as @user" do
user = User.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
- #User.any_instance.stub(:save).and_return(false)
+ User.any_instance.stub(:errors){ ActiveModel::Errors.new(User.new).tap{|e| e.add(:id, 'something wrong')} }
put :update, {:id => user.to_param, :user => {}}, valid_session
assigns(:user).should eq(user)
end
it "re-renders the 'edit' template" do
user = User.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
- #User.any_instance.stub(:save).and_return(false)
+ User.any_instance.stub(:errors){ ActiveModel::Errors.new(User.new).tap{|e| e.add(:id, 'something wrong')} }
put :update, {:id => user.to_param, :user => {}}, valid_session
response.should render_template("edit")
end

0 comments on commit e84d55b

Please sign in to comment.