rails g scaffold PlantPart...

Also migration to remove the string plant_part.
This commit is contained in:
Miles Gould
2013-10-30 23:32:20 +00:00
parent 40eac92013
commit e9a3c0f4ee
29 changed files with 489 additions and 41 deletions

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View File

@@ -0,0 +1,83 @@
class PlantPartsController < ApplicationController
# GET /plant_parts
# GET /plant_parts.json
def index
@plant_parts = PlantPart.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @plant_parts }
end
end
# GET /plant_parts/1
# GET /plant_parts/1.json
def show
@plant_part = PlantPart.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @plant_part }
end
end
# GET /plant_parts/new
# GET /plant_parts/new.json
def new
@plant_part = PlantPart.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @plant_part }
end
end
# GET /plant_parts/1/edit
def edit
@plant_part = PlantPart.find(params[:id])
end
# POST /plant_parts
# POST /plant_parts.json
def create
@plant_part = PlantPart.new(params[:plant_part])
respond_to do |format|
if @plant_part.save
format.html { redirect_to @plant_part, notice: 'Plant part was successfully created.' }
format.json { render json: @plant_part, status: :created, location: @plant_part }
else
format.html { render action: "new" }
format.json { render json: @plant_part.errors, status: :unprocessable_entity }
end
end
end
# PUT /plant_parts/1
# PUT /plant_parts/1.json
def update
@plant_part = PlantPart.find(params[:id])
respond_to do |format|
if @plant_part.update_attributes(params[:plant_part])
format.html { redirect_to @plant_part, notice: 'Plant part was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @plant_part.errors, status: :unprocessable_entity }
end
end
end
# DELETE /plant_parts/1
# DELETE /plant_parts/1.json
def destroy
@plant_part = PlantPart.find(params[:id])
@plant_part.destroy
respond_to do |format|
format.html { redirect_to plant_parts_url }
format.json { head :no_content }
end
end
end

View File

@@ -0,0 +1,2 @@
module PlantPartsHelper
end

View File

@@ -3,10 +3,11 @@ class Harvest < ActiveRecord::Base
friendly_id :harvest_slug, use: :slugged
attr_accessible :crop_id, :harvested_at, :description, :owner_id,
:quantity, :unit, :weight_quantity, :weight_unit, :plant_part, :slug
:quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug
belongs_to :crop
belongs_to :owner, :class_name => 'Member'
belongs_to :plant_part
validates :quantity,
:numericality => { :only_integer => false },
@@ -44,26 +45,6 @@ class Harvest < ActiveRecord::Base
after_validation :cleanup_quantities
PLANT_PARTS = [
'fruit',
'flower',
'pollen',
'seed',
'pod',
'leaf',
'stem',
'bark',
'bulb',
'root',
'tuber',
'whole plant',
'other'
]
validates :plant_part, :inclusion => { :in => PLANT_PARTS,
:message => "%{value} is not a valid plant part" },
:allow_nil => true,
:allow_blank => true
def cleanup_quantities
if quantity == 0
self.quantity = nil

3
app/models/plant_part.rb Normal file
View File

@@ -0,0 +1,3 @@
class PlantPart < ActiveRecord::Base
attr_accessible :name
end

View File

@@ -10,7 +10,7 @@
= f.label 'What did you harvest?', :class => 'control-label'
.controls
= collection_select(:harvest, :crop_id, Crop.all, :id, :name, :selected => @harvest.crop_id || @crop.id)
= f.select(:plant_part, Harvest::PLANT_PARTS, { :include_blank => false }, :class => 'input-medium')
= collection_select(:harvest, :plant_part_id, PlantPart.all, :id, :name, :selected => @harvest.plant_part_id)
%span.help-block
Can't find what you're looking for?
= link_to "Request new crops.", Growstuff::Application.config.new_crops_request_link

View File

@@ -0,0 +1,13 @@
= form_for @plant_part do |f|
- if @plant_part.errors.any?
#error_explanation
%h2= "#{pluralize(@plant_part.errors.count, "error")} prohibited this plant_part from being saved:"
%ul
- @plant_part.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.actions
= f.submit 'Save'

View File

@@ -0,0 +1,7 @@
%h1 Editing plant_part
= render 'form'
= link_to 'Show', @plant_part
\|
= link_to 'Back', plant_parts_path

View File

@@ -0,0 +1,19 @@
%h1 Listing plant_parts
%table
%tr
%th Name
%th
%th
%th
- @plant_parts.each do |plant_part|
%tr
%td= plant_part.name
%td= link_to 'Show', plant_part
%td= link_to 'Edit', edit_plant_part_path(plant_part)
%td= link_to 'Destroy', plant_part, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Plant part', new_plant_part_path

View File

@@ -0,0 +1,5 @@
%h1 New plant_part
= render 'form'
= link_to 'Back', plant_parts_path

View File

@@ -0,0 +1,9 @@
%p#notice= notice
%p
%b Name:
= @plant_part.name
= link_to 'Edit', edit_plant_part_path(@plant_part)
\|
= link_to 'Back', plant_parts_path

View File

@@ -1,5 +1,8 @@
Growstuff::Application.routes.draw do
resources :plant_parts
devise_for :members, :controllers => { :registrations => "registrations" }
resources :members

View File

@@ -0,0 +1,9 @@
class CreatePlantParts < ActiveRecord::Migration
def change
create_table :plant_parts do |t|
t.string :name
t.timestamps
end
end
end

View File

@@ -0,0 +1,11 @@
class ChangePlantPartToPlantPartId < ActiveRecord::Migration
def up
remove_column :harvests, :plant_part
add_column :harvests, :plant_part_id, :integer
end
def down
remove_column :harvests, :plant_part_id
add_column :harvests, :plant_part, :string
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20131029053113) do
ActiveRecord::Schema.define(:version => 20131030231202) do
create_table "account_types", :force => true do |t|
t.string "name", :null => false
@@ -105,7 +105,7 @@ ActiveRecord::Schema.define(:version => 20131029053113) do
t.string "slug"
t.decimal "weight_quantity"
t.string "weight_unit"
t.string "plant_part"
t.integer "plant_part_id"
end
create_table "members", :force => true do |t|
@@ -205,6 +205,12 @@ ActiveRecord::Schema.define(:version => 20131029053113) do
t.integer "planting_id"
end
create_table "plant_parts", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "plantings", :force => true do |t|
t.integer "garden_id", :null => false
t.integer "crop_id", :null => false

View File

@@ -0,0 +1,164 @@
require 'spec_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
describe PlantPartsController do
# This should return the minimal set of attributes required to create a valid
# PlantPart. As you add validations to PlantPart, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ "name" => "MyString" }
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# PlantPartsController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all plant_parts as @plant_parts" do
plant_part = PlantPart.create! valid_attributes
get :index, {}, valid_session
assigns(:plant_parts).should eq([plant_part])
end
end
describe "GET show" do
it "assigns the requested plant_part as @plant_part" do
plant_part = PlantPart.create! valid_attributes
get :show, {:id => plant_part.to_param}, valid_session
assigns(:plant_part).should eq(plant_part)
end
end
describe "GET new" do
it "assigns a new plant_part as @plant_part" do
get :new, {}, valid_session
assigns(:plant_part).should be_a_new(PlantPart)
end
end
describe "GET edit" do
it "assigns the requested plant_part as @plant_part" do
plant_part = PlantPart.create! valid_attributes
get :edit, {:id => plant_part.to_param}, valid_session
assigns(:plant_part).should eq(plant_part)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new PlantPart" do
expect {
post :create, {:plant_part => valid_attributes}, valid_session
}.to change(PlantPart, :count).by(1)
end
it "assigns a newly created plant_part as @plant_part" do
post :create, {:plant_part => valid_attributes}, valid_session
assigns(:plant_part).should be_a(PlantPart)
assigns(:plant_part).should be_persisted
end
it "redirects to the created plant_part" do
post :create, {:plant_part => valid_attributes}, valid_session
response.should redirect_to(PlantPart.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved plant_part as @plant_part" do
# Trigger the behavior that occurs when invalid params are submitted
PlantPart.any_instance.stub(:save).and_return(false)
post :create, {:plant_part => { "name" => "invalid value" }}, valid_session
assigns(:plant_part).should be_a_new(PlantPart)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
PlantPart.any_instance.stub(:save).and_return(false)
post :create, {:plant_part => { "name" => "invalid value" }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested plant_part" do
plant_part = PlantPart.create! valid_attributes
# Assuming there are no other plant_parts in the database, this
# specifies that the PlantPart created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
PlantPart.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" })
put :update, {:id => plant_part.to_param, :plant_part => { "name" => "MyString" }}, valid_session
end
it "assigns the requested plant_part as @plant_part" do
plant_part = PlantPart.create! valid_attributes
put :update, {:id => plant_part.to_param, :plant_part => valid_attributes}, valid_session
assigns(:plant_part).should eq(plant_part)
end
it "redirects to the plant_part" do
plant_part = PlantPart.create! valid_attributes
put :update, {:id => plant_part.to_param, :plant_part => valid_attributes}, valid_session
response.should redirect_to(plant_part)
end
end
describe "with invalid params" do
it "assigns the plant_part as @plant_part" do
plant_part = PlantPart.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
PlantPart.any_instance.stub(:save).and_return(false)
put :update, {:id => plant_part.to_param, :plant_part => { "name" => "invalid value" }}, valid_session
assigns(:plant_part).should eq(plant_part)
end
it "re-renders the 'edit' template" do
plant_part = PlantPart.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
PlantPart.any_instance.stub(:save).and_return(false)
put :update, {:id => plant_part.to_param, :plant_part => { "name" => "invalid value" }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested plant_part" do
plant_part = PlantPart.create! valid_attributes
expect {
delete :destroy, {:id => plant_part.to_param}, valid_session
}.to change(PlantPart, :count).by(-1)
end
it "redirects to the plant_parts list" do
plant_part = PlantPart.create! valid_attributes
delete :destroy, {:id => plant_part.to_param}, valid_session
response.should redirect_to(plant_parts_url)
end
end
end

View File

@@ -3,6 +3,7 @@
FactoryGirl.define do
factory :harvest do
crop
plant_part
owner
harvested_at "2013-09-17"
quantity "3"

View File

@@ -0,0 +1,7 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :plant_part do
name "pollen"
end
end

View File

@@ -0,0 +1,15 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the PlantPartsHelper. For example:
#
# describe PlantPartsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe PlantPartsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -118,19 +118,4 @@ describe Harvest do
end
end
context "plant parts" do
it 'all valid plant parts should work' do
Harvest::PLANT_PARTS.push(nil, '').each do |p|
@harvest = FactoryGirl.build(:harvest, :plant_part => p)
@harvest.should be_valid
end
end
it 'should refuse invalid plant parts' do
@harvest = FactoryGirl.build(:harvest, :plant_part => 'leg')
@harvest.should_not be_valid
@harvest.errors[:plant_part].should include("leg is not a valid plant part")
end
end
end

View File

@@ -0,0 +1,5 @@
require 'spec_helper'
describe PlantPart do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,11 @@
require 'spec_helper'
describe "PlantParts" do
describe "GET /plant_parts" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get plant_parts_path
response.status.should be(200)
end
end
end

View File

@@ -0,0 +1,35 @@
require "spec_helper"
describe PlantPartsController do
describe "routing" do
it "routes to #index" do
get("/plant_parts").should route_to("plant_parts#index")
end
it "routes to #new" do
get("/plant_parts/new").should route_to("plant_parts#new")
end
it "routes to #show" do
get("/plant_parts/1").should route_to("plant_parts#show", :id => "1")
end
it "routes to #edit" do
get("/plant_parts/1/edit").should route_to("plant_parts#edit", :id => "1")
end
it "routes to #create" do
post("/plant_parts").should route_to("plant_parts#create")
end
it "routes to #update" do
put("/plant_parts/1").should route_to("plant_parts#update", :id => "1")
end
it "routes to #destroy" do
delete("/plant_parts/1").should route_to("plant_parts#destroy", :id => "1")
end
end
end

View File

@@ -9,7 +9,7 @@ describe "harvests/edit" do
it "renders new harvest form" do
assert_select "form", :action => harvests_path, :method => "post" do
assert_select "select#harvest_crop_id", :name => "harvest[crop_id]"
assert_select "select#harvest_plant_part", :name => "harvest[plant_part]"
assert_select "select#harvest_plant_part_id", :name => "harvest[plant_part_id]"
assert_select "input#harvest_quantity", :name => "harvest[quantity]"
assert_select "input#harvest_weight_quantity", :name => "harvest[quantity]"
assert_select "select#harvest_unit", :name => "harvest[unit]"

View File

@@ -9,7 +9,7 @@ describe "harvests/new" do
it "renders new harvest form" do
assert_select "form", :action => harvests_path, :method => "post" do
assert_select "select#harvest_crop_id", :name => "harvest[crop_id]"
assert_select "select#harvest_plant_part", :name => "harvest[plant_part]"
assert_select "select#harvest_plant_part_id", :name => "harvest[plant_part_id]"
assert_select "input#harvest_quantity", :name => "harvest[quantity]"
assert_select "input#harvest_weight_quantity", :name => "harvest[quantity]"
assert_select "select#harvest_unit", :name => "harvest[unit]"

View File

@@ -0,0 +1,18 @@
require 'spec_helper'
describe "plant_parts/edit" do
before(:each) do
@plant_part = assign(:plant_part, stub_model(PlantPart,
:name => "MyString"
))
end
it "renders the edit plant_part form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => plant_parts_path(@plant_part), :method => "post" do
assert_select "input#plant_part_name", :name => "plant_part[name]"
end
end
end

View File

@@ -0,0 +1,20 @@
require 'spec_helper'
describe "plant_parts/index" do
before(:each) do
assign(:plant_parts, [
stub_model(PlantPart,
:name => "Name"
),
stub_model(PlantPart,
:name => "Name"
)
])
end
it "renders a list of plant_parts" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Name".to_s, :count => 2
end
end

View File

@@ -0,0 +1,18 @@
require 'spec_helper'
describe "plant_parts/new" do
before(:each) do
assign(:plant_part, stub_model(PlantPart,
:name => "MyString"
).as_new_record)
end
it "renders new plant_part form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => plant_parts_path, :method => "post" do
assert_select "input#plant_part_name", :name => "plant_part[name]"
end
end
end

View File

@@ -0,0 +1,15 @@
require 'spec_helper'
describe "plant_parts/show" do
before(:each) do
@plant_part = assign(:plant_part, stub_model(PlantPart,
:name => "Name"
))
end
it "renders attributes in <p>" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/Name/)
end
end