rails g scaffold seed...

This commit is contained in:
Skud
2013-07-15 21:16:47 +10:00
parent 334ca315b2
commit 91ea5146ac
23 changed files with 375 additions and 5 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,88 @@
class SeedsController < ApplicationController
load_and_authorize_resource
# GET /seeds
# GET /seeds.json
def index
@seeds = Seed.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @seeds }
end
end
# GET /seeds/1
# GET /seeds/1.json
def show
@seed = Seed.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @seed }
end
end
# GET /seeds/new
# GET /seeds/new.json
def new
@seed = Seed.new
# using find_by_id here because it returns nil, unlike find
@crop = Crop.find_by_id(params[:crop_id]) || Crop.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @seed }
end
end
# GET /seeds/1/edit
def edit
@seed = Seed.find(params[:id])
end
# POST /seeds
# POST /seeds.json
def create
params[:seed][:owner_id] = current_member.id
@seed = Seed.new(params[:seed])
respond_to do |format|
if @seed.save
format.html { redirect_to @seed, notice: 'Seed was successfully created.' }
format.json { render json: @seed, status: :created, location: @seed }
else
format.html { render action: "new" }
format.json { render json: @seed.errors, status: :unprocessable_entity }
end
end
end
# PUT /seeds/1
# PUT /seeds/1.json
def update
@seed = Seed.find(params[:id])
respond_to do |format|
if @seed.update_attributes(params[:seed])
format.html { redirect_to @seed, notice: 'Seed was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @seed.errors, status: :unprocessable_entity }
end
end
end
# DELETE /seeds/1
# DELETE /seeds/1.json
def destroy
@seed = Seed.find(params[:id])
@seed.destroy
respond_to do |format|
format.html { redirect_to seeds_url }
format.json { head :no_content }
end
end
end

View File

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

View File

@@ -9,6 +9,8 @@ class Member < ActiveRecord::Base
has_many :gardens, :foreign_key => 'owner_id'
has_many :plantings, :through => :gardens
has_many :seeds
has_and_belongs_to_many :roles
has_many :notifications, :foreign_key => 'recipient_id'

5
app/models/seed.rb Normal file
View File

@@ -0,0 +1,5 @@
class Seed < ActiveRecord::Base
attr_accessible :owner_id, :crop_id, :description, :quantity, :use_by
belongs_to :crop
belongs_to :owner, :class_name => 'Member', :foreign_key => 'owner_id'
end

View File

@@ -0,0 +1,23 @@
= form_for(@seed, :html => {:class => "form-horizontal"}) do |f|
- if @seed.errors.any?
#error_explanation
%h2= "#{pluralize(@seed.errors.count, "error")} prohibited this seed from being saved:"
%ul
- @seed.errors.full_messages.each do |msg|
%li= msg
.control-group
= f.label 'Crop:', :class => 'control-label'
.controls= collection_select(:seed, :crop_id, Crop.all, :id, :system_name, :selected => @seed.crop_id || @crop.id)
.control-group
= f.label 'Description:', :class => 'control-label'
.controls= f.text_area :description, :rows => 6
.control-group
= f.label 'Quantity:', :class => 'control-label'
.controls
= f.number_field :quantity, :class => 'input-small'
.control-group
= f.label 'Use by:', :class => 'control-label'
.controls= f.text_field :use_by, :value => @seed.use_by ? @seed.use_by.to_s(:ymd) : '', :class => 'add-datepicker'
.form-actions
= f.submit 'Save', :class => 'btn btn-primary'

View File

@@ -0,0 +1,3 @@
- content_for :title, "Editing seeds"
= render 'form'

View File

@@ -0,0 +1,24 @@
- content_for :title, "Listing seeds"
%p= link_to 'Add seeds', new_seed_path, :class => 'btn btn-primary'
%table.table.table-striped
%tr
%th Owner
%th Crop
%th Description
%th Quantity
%th Use by
%th
- @seeds.each do |seed|
%tr
%td= link_to seed.owner.login_name, seed.owner
%td= link_to seed.crop.system_name, seed.crop
%td= seed.description
%td= seed.quantity
%td= seed.use_by
%td= link_to 'Details', seed, :class => 'btn btn-mini'
%br

View File

@@ -0,0 +1,3 @@
- content_for :title, "Add seeds"
= render 'form'

View File

@@ -0,0 +1,27 @@
- content_for :title, "#{@seed.owner}'s #{@seed.crop} seeds"
%p#notice= notice
.row
.span6
%p
%b Quantity:
= @seed.quantity.blank? ? "not specified" : @seed.quantity
%p
%b Use by:
= @seed.use_by.to_s
%p
%b Description:
:markdown
#{ @seed.description != "" ? @seed.description : "No description given." }
- if can? :edit, @seed or can? :destroy, @seed
%p
- if can? :edit, @seed
=link_to 'Edit', edit_seed_path(@seed), :class => 'btn btn-mini'
- if can? :destroy, @seed
=link_to 'Delete', @seed, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini'
.span6
= render :partial => "crops/index_card", :locals => { :crop => @seed.crop}

View File

@@ -1,9 +1,11 @@
Growstuff::Application.routes.draw do
resources :photos
devise_for :members, :controllers => { :registrations => "registrations" }
resources :members do
resources :seeds
end
resources :photos
resources :authentications
resources :plantings
@@ -11,7 +13,6 @@ Growstuff::Application.routes.draw do
resources :posts
resources :scientific_names
resources :crops
resources :members
resources :comments
resources :roles
resources :forums

View File

@@ -0,0 +1,13 @@
class CreateSeeds < ActiveRecord::Migration
def change
create_table :seeds do |t|
t.integer :owner_id, :null => false
t.integer :crop_id, :null => false
t.text :description
t.integer :quantity
t.date :use_by
t.timestamps
end
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 => 20130705104238) do
ActiveRecord::Schema.define(:version => 20130715110134) do
create_table "account_types", :force => true do |t|
t.string "name", :null => false
@@ -234,4 +234,14 @@ ActiveRecord::Schema.define(:version => 20130705104238) do
t.datetime "updated_at", :null => false
end
create_table "seeds", :force => true do |t|
t.integer "owner_id", :null => false
t.integer "crop_id", :null => false
t.text "description"
t.integer "quantity"
t.date "use_by"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end

View File

@@ -0,0 +1,5 @@
require 'spec_helper'
describe SeedsController do
pending "add some tests if/when we do anything interesting"
end

11
spec/factories/seeds.rb Normal file
View File

@@ -0,0 +1,11 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :seed do
owner
crop
description "MyText"
quantity 1
use_by "2013-07-15"
end
end

View File

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

13
spec/models/seed_spec.rb Normal file
View File

@@ -0,0 +1,13 @@
require 'spec_helper'
describe Seed do
before(:each) do
@seed = FactoryGirl.build(:seed)
end
it 'should save a basic seed' do
@seed.save.should be_true
end
end

View File

@@ -0,0 +1,11 @@
require 'spec_helper'
describe "Seeds" do
describe "GET /seeds" 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 seeds_path
response.status.should be(200)
end
end
end

View File

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

View File

@@ -0,0 +1,21 @@
require 'spec_helper'
describe "seeds/edit" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
@seed = FactoryGirl.create(:seed, :owner => @member)
end
it "renders the edit seed form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => seeds_path(@seed), :method => "post" do
assert_select "select#seed_crop_id", :name => "seed[crop_id]"
assert_select "textarea#seed_description", :name => "seed[description]"
assert_select "input#seed_quantity", :name => "seed[quantity]"
end
end
end

View File

@@ -0,0 +1,18 @@
require 'spec_helper'
describe "seeds/index" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
@seed1 = FactoryGirl.create(:seed, :owner => @member)
assign(:seeds, [@seed1, @seed1])
end
it "renders a list of seeds" do
render
assert_select "tr>td", :text => @seed1.crop.system_name, :count => 2
assert_select "tr>td", :text => @seed1.owner.login_name, :count => 2
assert_select "tr>td", :text => @seed1.quantity.to_s, :count => 2
end
end

View File

@@ -0,0 +1,21 @@
require 'spec_helper'
describe "seeds/new" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
@seed1 = FactoryGirl.create(:seed, :owner => @member)
assign(:seed, @seed1)
end
it "renders new seed form" do
render
assert_select "form", :action => seeds_path, :method => "post" do
assert_select "select#seed_crop_id", :name => "seed[crop_id]"
assert_select "textarea#seed_description", :name => "seed[description]"
assert_select "input#seed_quantity", :name => "seed[quantity]"
end
end
end

View File

@@ -0,0 +1,16 @@
require 'spec_helper'
describe "seeds/show" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
@seed1 = FactoryGirl.create(:seed, :owner => @member)
assign(:seed, @seed1)
end
it "renders attributes in <p>" do
render
rendered.should contain @seed1.crop.system_name
end
end