From 8b7f8faae6fa7f6daf9834726afc4feb710a3ac4 Mon Sep 17 00:00:00 2001 From: Skud Date: Sat, 18 May 2013 10:27:08 +1000 Subject: [PATCH] added account type and paid months columns to product --- app/models/account_type.rb | 1 + app/models/product.rb | 5 ++++- app/views/products/_form.html.haml | 8 ++++++++ app/views/products/index.html.haml | 4 ++++ app/views/products/show.html.haml | 6 ++++++ .../20130518000339_add_columns_to_product.rb | 6 ++++++ db/schema.rb | 14 ++++++++------ db/seeds.rb | 13 ++++++++----- spec/factories/products.rb | 2 ++ spec/requests/account_types_spec.rb | 11 ----------- spec/views/products/new.html.haml_spec.rb | 2 ++ 11 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20130518000339_add_columns_to_product.rb delete mode 100644 spec/requests/account_types_spec.rb diff --git a/app/models/account_type.rb b/app/models/account_type.rb index d9b0df081..8aaa4f752 100644 --- a/app/models/account_type.rb +++ b/app/models/account_type.rb @@ -1,3 +1,4 @@ class AccountType < ActiveRecord::Base attr_accessible :is_paid, :is_permanent_paid, :name + has_many :products end diff --git a/app/models/product.rb b/app/models/product.rb index 88ee157b0..00dd79106 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,6 +1,9 @@ class Product < ActiveRecord::Base - attr_accessible :description, :min_price, :name + attr_accessible :description, :min_price, :name, + :account_type_id, :paid_months + has_and_belongs_to_many :orders + belongs_to :account_type def to_s name diff --git a/app/views/products/_form.html.haml b/app/views/products/_form.html.haml index 8b2909af7..798aa7208 100644 --- a/app/views/products/_form.html.haml +++ b/app/views/products/_form.html.haml @@ -15,5 +15,13 @@ .field = f.label :min_price = f.text_field :min_price + .field + = f.label :account_type + = collection_select(:product, :account_type_id, AccountType.all, :id, + :name, :selected => @product.account_type_id ) + .field + = f.label :paid_months + = f.text_field :paid_months + .control-group .actions = f.submit 'Save' diff --git a/app/views/products/index.html.haml b/app/views/products/index.html.haml index caf508db7..6a0cadb50 100644 --- a/app/views/products/index.html.haml +++ b/app/views/products/index.html.haml @@ -5,6 +5,8 @@ %th Name %th Description %th Min price + %th Account type + %th Paid months %th %th %th @@ -14,6 +16,8 @@ %td= product.name %td= product.description %td= product.min_price + %td= product.account_type ? product.account_type.name : "" + %td= product.paid_months %td= link_to 'Show', product %td= link_to 'Edit', edit_product_path(product) %td= link_to 'Destroy', product, :method => :delete, :data => { :confirm => 'Are you sure?' } diff --git a/app/views/products/show.html.haml b/app/views/products/show.html.haml index 03b2e6e08..874169c81 100644 --- a/app/views/products/show.html.haml +++ b/app/views/products/show.html.haml @@ -9,6 +9,12 @@ %p %b Min price: = @product.min_price +%p + %b Account type: + = @product.account_type.name +%p + %b Paid months: + = @product.paid_months = link_to 'Edit', edit_product_path(@product) \| diff --git a/db/migrate/20130518000339_add_columns_to_product.rb b/db/migrate/20130518000339_add_columns_to_product.rb new file mode 100644 index 000000000..bb3bcf781 --- /dev/null +++ b/db/migrate/20130518000339_add_columns_to_product.rb @@ -0,0 +1,6 @@ +class AddColumnsToProduct < ActiveRecord::Migration + def change + add_column :products, :account_type_id, :integer + add_column :products, :paid_months, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index d70d92d58..e2447b6b6 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 => 20130517234458) do +ActiveRecord::Schema.define(:version => 20130518000339) do create_table "account_details", :force => true do |t| t.integer "member_id", :null => false @@ -185,11 +185,13 @@ ActiveRecord::Schema.define(:version => 20130517234458) do add_index "posts", ["slug"], :name => "index_updates_on_slug", :unique => true create_table "products", :force => true do |t| - t.string "name", :null => false - t.string "description", :null => false - t.integer "min_price", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.string "name", :null => false + t.string "description", :null => false + t.integer "min_price", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.integer "account_type_id" + t.integer "paid_months" end create_table "roles", :force => true do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 601811628..f307bb94a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -64,12 +64,12 @@ if Rails.env.development? or Rails.env.test? :is_paid => false, :is_permanent_paid => false ) - AccountType.create!( + @paid_account = AccountType.create!( :name => "Paid", :is_paid => true, :is_permanent_paid => false ) - AccountType.create!( + @seed_account = AccountType.create!( :name => "Seed", :is_paid => true, :is_permanent_paid => true @@ -83,13 +83,16 @@ if Rails.env.development? or Rails.env.test? puts "Adding products..." Product.create!( :name => "Annual subscription", - :description => "Paid account, renews yearly", - :min_price => 3000 + :description => "Paid account, 1 year", + :min_price => 3000, + :account_type_id => @paid_account.id, + :paid_months => 12 ) Product.create!( :name => "Seed account", :description => "Paid account, in perpetuity", - :min_price => 15000 + :min_price => 15000, + :account_type_id => @seed_account.id, ) end diff --git a/spec/factories/products.rb b/spec/factories/products.rb index 84ca3163f..037350ece 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -5,5 +5,7 @@ FactoryGirl.define do name "annual subscription" description "paid membership, renewing yearly" min_price "999" + account_type + paid_months 12 end end diff --git a/spec/requests/account_types_spec.rb b/spec/requests/account_types_spec.rb deleted file mode 100644 index c7d67ac6b..000000000 --- a/spec/requests/account_types_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -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 diff --git a/spec/views/products/new.html.haml_spec.rb b/spec/views/products/new.html.haml_spec.rb index c508a89c5..a01ad897d 100644 --- a/spec/views/products/new.html.haml_spec.rb +++ b/spec/views/products/new.html.haml_spec.rb @@ -17,6 +17,8 @@ describe "products/new" do assert_select "input#product_name", :name => "product[name]" assert_select "input#product_description", :name => "product[description]" assert_select "input#product_min_price", :name => "product[min_price]" + assert_select "select#product_account_type_id", :name => "product[account_type_id]" + assert_select "input#product_paid_months", :name => "product[paid_months]" end end end