continuing the refactor of the admin section
This commit is contained in:
@@ -31,6 +31,11 @@ module BjController
|
||||
def controller_class_name
|
||||
"#{controller_name.classify.pluralize}Controller"
|
||||
end
|
||||
|
||||
def required_attribute_name
|
||||
m = model_name.classify.constantize
|
||||
m.validators.find{|v| v.is_a? ActiveModel::Validations::PresenceValidator }.try(:attributes).try(:first) || m.property_names.find{|k| k !~ /(_id|_at)$/}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -87,16 +87,14 @@ describe <%= controller_class_name %> do
|
||||
context "valid attributes" do
|
||||
it "located the requested <%= model_name %>" do
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>)
|
||||
@<%= model_name %>.reload
|
||||
assigns(:<%= model_name %>).should eq(@<%= model_name %>)
|
||||
end
|
||||
|
||||
it "changes @<%= model_name %>'s attributes" do
|
||||
attributes = attributes_for(:<%= model_name %>)
|
||||
attribute_to_change = attributes.keys.find{|k| k !~ /_id$/}
|
||||
attributes[attribute_to_change] = "ChangedByTest"
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: attributes
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>, <%= required_attribute_name %>: "ChangedByTest")
|
||||
@<%= model_name %>.reload
|
||||
@<%= model_name %>.send(attribute_to_change).should eq("ChangedByTest")
|
||||
@<%= model_name %>.<%= required_attribute_name %>.should eq("ChangedByTest")
|
||||
end
|
||||
|
||||
it "redirects to the updated <%= model_name %>" do
|
||||
@@ -104,5 +102,34 @@ describe <%= controller_class_name %> do
|
||||
response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, @#{model_name}]" : "@#{model_name}" %>
|
||||
end
|
||||
end
|
||||
|
||||
context "invalid attributes" do
|
||||
it "locates the requested <%= model_name %>" do
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: {<%= required_attribute_name %>: ''}
|
||||
assigns(:<%= model_name %>).should eq(@<%= model_name %>)
|
||||
end
|
||||
|
||||
it "re-renders the edit method" do
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: {<%= required_attribute_name %>: ''}
|
||||
response.should render_template :edit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE destroy' do
|
||||
before :each do
|
||||
@<%= model_name %> = create :<%= model_name %>
|
||||
end
|
||||
|
||||
it "deletes the <%= model_name %>" do
|
||||
expect{
|
||||
delete :destroy, id: @<%= model_name %>
|
||||
}.to change(<%= model_name.classify %>, :count).by(-1)
|
||||
end
|
||||
|
||||
it "redirects to <%= model_plural_name %>#index" do
|
||||
delete :destroy, id: @<%= model_name %>
|
||||
response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, :#{model_plural_name}]" : "#{model_plural_name}_path" %>
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user