diff --git a/app/assets/javascripts/seeds.js.coffee b/app/assets/javascripts/seeds.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/seeds.js.coffee @@ -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/ diff --git a/app/controllers/seeds_controller.rb b/app/controllers/seeds_controller.rb new file mode 100644 index 000000000..8d13addb6 --- /dev/null +++ b/app/controllers/seeds_controller.rb @@ -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 diff --git a/app/helpers/seeds_helper.rb b/app/helpers/seeds_helper.rb new file mode 100644 index 000000000..46c697f02 --- /dev/null +++ b/app/helpers/seeds_helper.rb @@ -0,0 +1,2 @@ +module SeedsHelper +end diff --git a/app/models/member.rb b/app/models/member.rb index 506ce2551..0725e42f4 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -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' diff --git a/app/models/seed.rb b/app/models/seed.rb new file mode 100644 index 000000000..c271678b4 --- /dev/null +++ b/app/models/seed.rb @@ -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 diff --git a/app/views/seeds/_form.html.haml b/app/views/seeds/_form.html.haml new file mode 100644 index 000000000..e17719fd7 --- /dev/null +++ b/app/views/seeds/_form.html.haml @@ -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' diff --git a/app/views/seeds/edit.html.haml b/app/views/seeds/edit.html.haml new file mode 100644 index 000000000..13445fb6c --- /dev/null +++ b/app/views/seeds/edit.html.haml @@ -0,0 +1,3 @@ +- content_for :title, "Editing seeds" + += render 'form' diff --git a/app/views/seeds/index.html.haml b/app/views/seeds/index.html.haml new file mode 100644 index 000000000..3a215aab9 --- /dev/null +++ b/app/views/seeds/index.html.haml @@ -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 + diff --git a/app/views/seeds/new.html.haml b/app/views/seeds/new.html.haml new file mode 100644 index 000000000..834c958d8 --- /dev/null +++ b/app/views/seeds/new.html.haml @@ -0,0 +1,3 @@ +- content_for :title, "Add seeds" + += render 'form' diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml new file mode 100644 index 000000000..f9e5530f8 --- /dev/null +++ b/app/views/seeds/show.html.haml @@ -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} diff --git a/config/routes.rb b/config/routes.rb index c14a07e76..ed14e0042 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20130715110134_create_seeds.rb b/db/migrate/20130715110134_create_seeds.rb new file mode 100644 index 000000000..1d0d49225 --- /dev/null +++ b/db/migrate/20130715110134_create_seeds.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index bf9e10438..a805a5b48 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/controllers/seeds_controller_spec.rb b/spec/controllers/seeds_controller_spec.rb new file mode 100644 index 000000000..b5285946e --- /dev/null +++ b/spec/controllers/seeds_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe SeedsController do + pending "add some tests if/when we do anything interesting" +end diff --git a/spec/factories/seeds.rb b/spec/factories/seeds.rb new file mode 100644 index 000000000..aeb2abe0d --- /dev/null +++ b/spec/factories/seeds.rb @@ -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 diff --git a/spec/helpers/seeds_helper_spec.rb b/spec/helpers/seeds_helper_spec.rb new file mode 100644 index 000000000..ad9a2022a --- /dev/null +++ b/spec/helpers/seeds_helper_spec.rb @@ -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 diff --git a/spec/models/seed_spec.rb b/spec/models/seed_spec.rb new file mode 100644 index 000000000..46a4c5851 --- /dev/null +++ b/spec/models/seed_spec.rb @@ -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 diff --git a/spec/requests/seeds_spec.rb b/spec/requests/seeds_spec.rb new file mode 100644 index 000000000..ac3ee1076 --- /dev/null +++ b/spec/requests/seeds_spec.rb @@ -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 diff --git a/spec/routing/seeds_routing_spec.rb b/spec/routing/seeds_routing_spec.rb new file mode 100644 index 000000000..d4746d299 --- /dev/null +++ b/spec/routing/seeds_routing_spec.rb @@ -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 diff --git a/spec/views/seeds/edit.html.haml_spec.rb b/spec/views/seeds/edit.html.haml_spec.rb new file mode 100644 index 000000000..3d0ce0ab5 --- /dev/null +++ b/spec/views/seeds/edit.html.haml_spec.rb @@ -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 diff --git a/spec/views/seeds/index.html.haml_spec.rb b/spec/views/seeds/index.html.haml_spec.rb new file mode 100644 index 000000000..8979d0291 --- /dev/null +++ b/spec/views/seeds/index.html.haml_spec.rb @@ -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 diff --git a/spec/views/seeds/new.html.haml_spec.rb b/spec/views/seeds/new.html.haml_spec.rb new file mode 100644 index 000000000..c291a9956 --- /dev/null +++ b/spec/views/seeds/new.html.haml_spec.rb @@ -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 diff --git a/spec/views/seeds/show.html.haml_spec.rb b/spec/views/seeds/show.html.haml_spec.rb new file mode 100644 index 000000000..c31d14b79 --- /dev/null +++ b/spec/views/seeds/show.html.haml_spec.rb @@ -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

" do + render + rendered.should contain @seed1.crop.system_name + end +end