added account_types, fixed a bunch of tests

This commit is contained in:
Skud
2013-05-17 16:07:46 +10:00
parent d49ba175e9
commit c595b2181d
30 changed files with 522 additions and 36 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 AccountTypesController < ApplicationController
# GET /account_types
# GET /account_types.json
def index
@account_types = AccountType.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @account_types }
end
end
# GET /account_types/1
# GET /account_types/1.json
def show
@account_type = AccountType.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @account_type }
end
end
# GET /account_types/new
# GET /account_types/new.json
def new
@account_type = AccountType.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @account_type }
end
end
# GET /account_types/1/edit
def edit
@account_type = AccountType.find(params[:id])
end
# POST /account_types
# POST /account_types.json
def create
@account_type = AccountType.new(params[:account_type])
respond_to do |format|
if @account_type.save
format.html { redirect_to @account_type, notice: 'Account type was successfully created.' }
format.json { render json: @account_type, status: :created, location: @account_type }
else
format.html { render action: "new" }
format.json { render json: @account_type.errors, status: :unprocessable_entity }
end
end
end
# PUT /account_types/1
# PUT /account_types/1.json
def update
@account_type = AccountType.find(params[:id])
respond_to do |format|
if @account_type.update_attributes(params[:account_type])
format.html { redirect_to @account_type, notice: 'Account type was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @account_type.errors, status: :unprocessable_entity }
end
end
end
# DELETE /account_types/1
# DELETE /account_types/1.json
def destroy
@account_type = AccountType.find(params[:id])
@account_type.destroy
respond_to do |format|
format.html { redirect_to account_types_url }
format.json { head :no_content }
end
end
end

View File

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

View File

@@ -1,4 +1,5 @@
class AccountDetail < ActiveRecord::Base
attr_accessible :account_type, :member_id, :paid_until
attr_accessible :account_type_id, :member_id, :paid_until
belongs_to :member
belongs_to :account_type
end

View File

@@ -0,0 +1,3 @@
class AccountType < ActiveRecord::Base
attr_accessible :is_paid, :is_permanent_paid, :name
end

View File

@@ -5,7 +5,7 @@
= @account_detail.member_id
%p
%b Account type:
= @account_detail.account_type
= @account_detail.account_type.name
%p
%b Paid until:
= @account_detail.paid_until

View File

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

View File

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

View File

@@ -0,0 +1,23 @@
%h1 Listing account_types
%table
%tr
%th Name
%th Is paid
%th Is permanent paid
%th
%th
%th
- @account_types.each do |account_type|
%tr
%td= account_type.name
%td= account_type.is_paid
%td= account_type.is_permanent_paid
%td= link_to 'Show', account_type
%td= link_to 'Edit', edit_account_type_path(account_type)
%td= link_to 'Destroy', account_type, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Account type', new_account_type_path

View File

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

View File

@@ -0,0 +1,15 @@
%p#notice= notice
%p
%b Name:
= @account_type.name
%p
%b Is paid:
= @account_type.is_paid
%p
%b Is permanent paid:
= @account_type.is_permanent_paid
= link_to 'Edit', edit_account_type_path(@account_type)
\|
= link_to 'Back', account_types_path

View File

@@ -1,5 +1,8 @@
Growstuff::Application.routes.draw do
resources :account_types
resources :account_details

View File

@@ -2,7 +2,7 @@ class CreateAccountDetails < ActiveRecord::Migration
def change
create_table :account_details do |t|
t.integer :member_id, :null => false
t.integer :account_type
t.integer :account_type_id
t.datetime :paid_until
t.timestamps

View File

@@ -0,0 +1,11 @@
class CreateAccountTypes < ActiveRecord::Migration
def change
create_table :account_types do |t|
t.string :name
t.boolean :is_paid
t.boolean :is_permanent_paid
t.timestamps
end
end
end

View File

@@ -11,14 +11,22 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130517015920) do
ActiveRecord::Schema.define(:version => 20130517051922) do
create_table "account_details", :force => true do |t|
t.integer "member_id", :null => false
t.string "account_type", :default => "free", :null => false
t.integer "member_id", :null => false
t.integer "account_type_id"
t.datetime "paid_until"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "account_types", :force => true do |t|
t.string "name"
t.boolean "is_paid"
t.boolean "is_permanent_paid"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "authentications", :force => true do |t|

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 AccountTypesController do
# This should return the minimal set of attributes required to create a valid
# AccountType. As you add validations to AccountType, 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
# AccountTypesController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all account_types as @account_types" do
account_type = AccountType.create! valid_attributes
get :index, {}, valid_session
assigns(:account_types).should eq([account_type])
end
end
describe "GET show" do
it "assigns the requested account_type as @account_type" do
account_type = AccountType.create! valid_attributes
get :show, {:id => account_type.to_param}, valid_session
assigns(:account_type).should eq(account_type)
end
end
describe "GET new" do
it "assigns a new account_type as @account_type" do
get :new, {}, valid_session
assigns(:account_type).should be_a_new(AccountType)
end
end
describe "GET edit" do
it "assigns the requested account_type as @account_type" do
account_type = AccountType.create! valid_attributes
get :edit, {:id => account_type.to_param}, valid_session
assigns(:account_type).should eq(account_type)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new AccountType" do
expect {
post :create, {:account_type => valid_attributes}, valid_session
}.to change(AccountType, :count).by(1)
end
it "assigns a newly created account_type as @account_type" do
post :create, {:account_type => valid_attributes}, valid_session
assigns(:account_type).should be_a(AccountType)
assigns(:account_type).should be_persisted
end
it "redirects to the created account_type" do
post :create, {:account_type => valid_attributes}, valid_session
response.should redirect_to(AccountType.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved account_type as @account_type" do
# Trigger the behavior that occurs when invalid params are submitted
AccountType.any_instance.stub(:save).and_return(false)
post :create, {:account_type => { "name" => "invalid value" }}, valid_session
assigns(:account_type).should be_a_new(AccountType)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
AccountType.any_instance.stub(:save).and_return(false)
post :create, {:account_type => { "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 account_type" do
account_type = AccountType.create! valid_attributes
# Assuming there are no other account_types in the database, this
# specifies that the AccountType created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
AccountType.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" })
put :update, {:id => account_type.to_param, :account_type => { "name" => "MyString" }}, valid_session
end
it "assigns the requested account_type as @account_type" do
account_type = AccountType.create! valid_attributes
put :update, {:id => account_type.to_param, :account_type => valid_attributes}, valid_session
assigns(:account_type).should eq(account_type)
end
it "redirects to the account_type" do
account_type = AccountType.create! valid_attributes
put :update, {:id => account_type.to_param, :account_type => valid_attributes}, valid_session
response.should redirect_to(account_type)
end
end
describe "with invalid params" do
it "assigns the account_type as @account_type" do
account_type = AccountType.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
AccountType.any_instance.stub(:save).and_return(false)
put :update, {:id => account_type.to_param, :account_type => { "name" => "invalid value" }}, valid_session
assigns(:account_type).should eq(account_type)
end
it "re-renders the 'edit' template" do
account_type = AccountType.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
AccountType.any_instance.stub(:save).and_return(false)
put :update, {:id => account_type.to_param, :account_type => { "name" => "invalid value" }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested account_type" do
account_type = AccountType.create! valid_attributes
expect {
delete :destroy, {:id => account_type.to_param}, valid_session
}.to change(AccountType, :count).by(-1)
end
it "redirects to the account_types list" do
account_type = AccountType.create! valid_attributes
delete :destroy, {:id => account_type.to_param}, valid_session
response.should redirect_to(account_types_url)
end
end
end

View File

@@ -2,8 +2,8 @@
FactoryGirl.define do
factory :account_detail do
member_id 1
account_type 1
member
account_type
paid_until "2013-05-17 11:59:20"
end
end

View File

@@ -0,0 +1,9 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :account_type do
name "MyString"
is_paid false
is_permanent_paid false
end
end

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,10 +2,9 @@ require 'spec_helper'
describe "account_details/edit" do
before(:each) do
@account_detail = assign(:account_detail, stub_model(AccountDetail,
:member_id => 1,
:account_type => 1
))
@account_detail = assign(:account_detail,
FactoryGirl.create(:account_detail)
)
end
it "renders the edit account_detail form" do

View File

@@ -2,22 +2,13 @@ require 'spec_helper'
describe "account_details/index" do
before(:each) do
assign(:account_details, [
stub_model(AccountDetail,
:member_id => 1,
:account_type => 1
),
stub_model(AccountDetail,
:member_id => 1,
:account_type => 1
)
])
@account_detail = FactoryGirl.create(:account_detail)
assign(:account_details, [@account_detail, @account_detail])
end
it "renders a list of account_details" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => 1.to_s, :count => 2
assert_select "tr>td", :text => "Account Type".to_s, :count => 2
assert_select "tr>td", :text => @account_detail.member_id.to_s, :count => 2
end
end

View File

@@ -2,10 +2,7 @@ require 'spec_helper'
describe "account_details/new" do
before(:each) do
assign(:account_detail, stub_model(AccountDetail,
:member_id => 1,
:account_type => 1
).as_new_record)
assign(:account_detail, FactoryGirl.create(:account_detail))
end
it "renders new account_detail form" do

View File

@@ -2,16 +2,16 @@ require 'spec_helper'
describe "account_details/show" do
before(:each) do
@account_detail = assign(:account_detail, stub_model(AccountDetail,
:member_id => 1,
:account_type => 1
))
@account_detail = assign(:account_detail,
FactoryGirl.create(:account_detail)
)
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(/1/)
rendered.should match(/Account Type/)
rendered.should contain @account_detail.member_id.to_s
rendered.should contain @account_detail.account_type.name
rendered.should contain @account_detail.paid_until.to_s
end
end

View File

@@ -0,0 +1,22 @@
require 'spec_helper'
describe "account_types/edit" do
before(:each) do
@account_type = assign(:account_type, stub_model(AccountType,
:name => "MyString",
:is_paid => false,
:is_permanent_paid => false
))
end
it "renders the edit account_type form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => account_types_path(@account_type), :method => "post" do
assert_select "input#account_type_name", :name => "account_type[name]"
assert_select "input#account_type_is_paid", :name => "account_type[is_paid]"
assert_select "input#account_type_is_permanent_paid", :name => "account_type[is_permanent_paid]"
end
end
end

View File

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

View File

@@ -0,0 +1,22 @@
require 'spec_helper'
describe "account_types/new" do
before(:each) do
assign(:account_type, stub_model(AccountType,
:name => "MyString",
:is_paid => false,
:is_permanent_paid => false
).as_new_record)
end
it "renders new account_type form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => account_types_path, :method => "post" do
assert_select "input#account_type_name", :name => "account_type[name]"
assert_select "input#account_type_is_paid", :name => "account_type[is_paid]"
assert_select "input#account_type_is_permanent_paid", :name => "account_type[is_permanent_paid]"
end
end
end

View File

@@ -0,0 +1,19 @@
require 'spec_helper'
describe "account_types/show" do
before(:each) do
@account_type = assign(:account_type, stub_model(AccountType,
:name => "Name",
:is_paid => false,
:is_permanent_paid => false
))
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/)
rendered.should match(/false/)
rendered.should match(/false/)
end
end