Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Almost ready to buy and sell

  • Loading branch information...
commit 767cd44a8ba6d7fa17bd8ca0ed057b8b4acd93d0 1 parent c6a9339
@jackquack authored
Showing with 243 additions and 8 deletions.
  1. +3 −0  app/assets/javascripts/transactions.js.coffee
  2. +3 −0  app/assets/stylesheets/transactions.css.scss
  3. +2 −0  app/controllers/transactions_controller.rb
  4. +2 −0  app/helpers/transactions_helper.rb
  5. +2 −1  app/models/buy.rb
  6. +2 −1  app/models/sell.rb
  7. +6 −0 app/models/transaction.rb
  8. +4 −1 app/views/buys/_form.html.haml
  9. +5 −2 app/views/buys/show.html.haml
  10. +4 −1 app/views/sells/_form.html.haml
  11. +7 −1 app/views/sells/show.html.haml
  12. +25 −0 app/views/transactions/_form.html.haml
  13. +7 −0 app/views/transactions/edit.html.haml
  14. +27 −0 app/views/transactions/index.html.haml
  15. +5 −0 app/views/transactions/new.html.haml
  16. +21 −0 app/views/transactions/show.html.haml
  17. +3 −0  config/routes.rb
  18. +13 −0 db/migrate/20130323173211_create_transactions.rb
  19. +6 −0 db/migrate/20130323173237_add_price_to_buys_and_sells.rb
  20. +6 −0 db/migrate/20130323173942_add_transaction_id_to_sells_and_buys.rb
  21. +15 −1 db/schema.rb
  22. +15 −0 test/fixtures/transactions.yml
  23. +49 −0 test/functional/transactions_controller_test.rb
  24. +4 −0 test/unit/helpers/transactions_helper_test.rb
  25. +7 −0 test/unit/transaction_test.rb
View
3  app/assets/javascripts/transactions.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/
View
3  app/assets/stylesheets/transactions.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the transactions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
2  app/controllers/transactions_controller.rb
@@ -0,0 +1,2 @@
+class TransactionsController < InheritedResources::Base
+end
View
2  app/helpers/transactions_helper.rb
@@ -0,0 +1,2 @@
+module TransactionsHelper
+end
View
3  app/models/buy.rb
@@ -1,5 +1,6 @@
class Buy < ActiveRecord::Base
- attr_accessible :prediction_id, :number_of_shares, :user_id
+ attr_accessible :prediction_id, :number_of_shares, :user_id, :price
belongs_to :prediction
belongs_to :user
+ belongs_to :transaction
end
View
3  app/models/sell.rb
@@ -1,5 +1,6 @@
class Sell < ActiveRecord::Base
- attr_accessible :number_of_shares, :user_id, :prediction_id
+ attr_accessible :number_of_shares, :user_id, :prediction_id, :price
belongs_to :prediction
belongs_to :user
+ belongs_to :transaction
end
View
6 app/models/transaction.rb
@@ -0,0 +1,6 @@
+class Transaction < ActiveRecord::Base
+ attr_accessible :buyer_id, :prediction_id, :price, :quantity, :seller_id
+ belongs_to :prediction
+ belongs_to :buyer, :class_name => "User", :foreign_key => "buyer_id"
+ belongs_to :seller, :class_name => "User", :foreign_key => "seller_id"
+end
View
5 app/views/buys/_form.html.haml
@@ -9,6 +9,9 @@
.field
= f.label :number_of_shares
= f.text_field :number_of_shares
- = f.hidden_field :prediction_id, :value => @prediction.id
+ .field
+ = f.label :price
+ = f.number_field :price
+ = f.hidden_field :prediction_id, :value => @prediction.id
.actions
= f.submit 'Save'
View
7 app/views/buys/show.html.haml
@@ -4,11 +4,14 @@
%b NumberOfShares:
= @buy.number_of_shares
%p
+ %b Price:
+ = @buy.price
+%p
%b User:
- = @buy.user_id
+ = @buy.user.name
%p
%b Prediction:
- = @buy.prediction_id
+ = @buy.prediction.name
= link_to 'Edit', edit_buy_path(@buy)
\|
View
5 app/views/sells/_form.html.haml
@@ -9,6 +9,9 @@
.field
= f.label :number_of_shares
= f.text_field :number_of_shares
- = f.hidden_field :prediction_id, :value => @prediction.id
+ .field
+ = f.label :price
+ = f.number_field :price
+ = f.hidden_field :prediction_id, :value => @prediction.id
.actions
= f.submit 'Save'
View
8 app/views/sells/show.html.haml
@@ -4,8 +4,14 @@
%b NumberOfShares:
= @sell.number_of_shares
%p
+ %b Price:
+ = @sell.price
+%p
%b User:
- = @sell.user_id
+ = @sell.user.name
+%p
+ %b Prediction:
+ = @sell.prediction.name
= link_to 'Edit', edit_sell_path(@sell)
\|
View
25 app/views/transactions/_form.html.haml
@@ -0,0 +1,25 @@
+= form_for @transaction do |f|
+ - if @transaction.errors.any?
+ #error_explanation
+ %h2= "#{pluralize(@transaction.errors.count, "error")} prohibited this transaction from being saved:"
+ %ul
+ - @transaction.errors.full_messages.each do |msg|
+ %li= msg
+
+ .field
+ = f.label :buyer_id
+ = f.number_field :buyer_id
+ .field
+ = f.label :seller_id
+ = f.number_field :seller_id
+ .field
+ = f.label :price
+ = f.number_field :price
+ .field
+ = f.label :quantity
+ = f.number_field :quantity
+ .field
+ = f.label :prediction_id
+ = f.number_field :prediction_id
+ .actions
+ = f.submit 'Save'
View
7 app/views/transactions/edit.html.haml
@@ -0,0 +1,7 @@
+%h1 Editing transaction
+
+= render 'form'
+
+= link_to 'Show', @transaction
+\|
+= link_to 'Back', transactions_path
View
27 app/views/transactions/index.html.haml
@@ -0,0 +1,27 @@
+%h1 Listing transactions
+
+%table
+ %tr
+ %th Buyer
+ %th Seller
+ %th Price
+ %th Quantity
+ %th Prediction
+ %th
+ %th
+ %th
+
+ - @transactions.each do |transaction|
+ %tr
+ %td= transaction.buyer_id
+ %td= transaction.seller_id
+ %td= transaction.price
+ %td= transaction.quantity
+ %td= transaction.prediction_id
+ %td= link_to 'Show', transaction
+ %td= link_to 'Edit', edit_transaction_path(transaction)
+ %td= link_to 'Destroy', transaction, :method => :delete, :data => { :confirm => 'Are you sure?' }
+
+%br
+
+= link_to 'New Transaction', new_transaction_path
View
5 app/views/transactions/new.html.haml
@@ -0,0 +1,5 @@
+%h1 New transaction
+
+= render 'form'
+
+= link_to 'Back', transactions_path
View
21 app/views/transactions/show.html.haml
@@ -0,0 +1,21 @@
+%p#notice= notice
+
+%p
+ %b Buyer:
+ = @transaction.buyer_id
+%p
+ %b Seller:
+ = @transaction.seller_id
+%p
+ %b Price:
+ = @transaction.price
+%p
+ %b Quantity:
+ = @transaction.quantity
+%p
+ %b Prediction:
+ = @transaction.prediction_id
+
+= link_to 'Edit', edit_transaction_path(@transaction)
+\|
+= link_to 'Back', transactions_path
View
3  config/routes.rb
@@ -1,4 +1,7 @@
BitPredictor::Application.routes.draw do
+ resources :transactions
+
+
resources :buys
resources :sells
resources :predictions do
View
13 db/migrate/20130323173211_create_transactions.rb
@@ -0,0 +1,13 @@
+class CreateTransactions < ActiveRecord::Migration
+ def change
+ create_table :transactions do |t|
+ t.integer :buyer_id
+ t.integer :seller_id
+ t.integer :price
+ t.integer :quantity
+ t.integer :prediction_id
+
+ t.timestamps
+ end
+ end
+end
View
6 db/migrate/20130323173237_add_price_to_buys_and_sells.rb
@@ -0,0 +1,6 @@
+class AddPriceToBuysAndSells < ActiveRecord::Migration
+ def change
+ add_column :buys, :price, :float
+ add_column :sells, :price, :float
+ end
+end
View
6 db/migrate/20130323173942_add_transaction_id_to_sells_and_buys.rb
@@ -0,0 +1,6 @@
+class AddTransactionIdToSellsAndBuys < ActiveRecord::Migration
+ def change
+ add_column :buys, :transaction_id, :integer
+ add_column :sells, :transaction_id, :integer
+ end
+end
View
16 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130323172552) do
+ActiveRecord::Schema.define(:version => 20130323173942) do
create_table "buys", :force => true do |t|
t.decimal "number_of_shares", :precision => 10, :scale => 0
@@ -19,6 +19,8 @@
t.integer "prediction_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.float "price"
+ t.integer "transaction_id"
end
create_table "predictions", :force => true do |t|
@@ -38,6 +40,18 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "prediction_id"
+ t.float "price"
+ t.integer "transaction_id"
+ end
+
+ create_table "transactions", :force => true do |t|
+ t.integer "buyer_id"
+ t.integer "seller_id"
+ t.integer "price"
+ t.integer "quantity"
+ t.integer "prediction_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
View
15 test/fixtures/transactions.yml
@@ -0,0 +1,15 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ buyer_id: 1
+ seller_id: 1
+ price: 1
+ quantity: 1
+ prediction_id: 1
+
+two:
+ buyer_id: 1
+ seller_id: 1
+ price: 1
+ quantity: 1
+ prediction_id: 1
View
49 test/functional/transactions_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class TransactionsControllerTest < ActionController::TestCase
+ setup do
+ @transaction = transactions(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:transactions)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create transaction" do
+ assert_difference('Transaction.count') do
+ post :create, transaction: { buyer_id: @transaction.buyer_id, prediction_id: @transaction.prediction_id, price: @transaction.price, quantity: @transaction.quantity, seller_id: @transaction.seller_id }
+ end
+
+ assert_redirected_to transaction_path(assigns(:transaction))
+ end
+
+ test "should show transaction" do
+ get :show, id: @transaction
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @transaction
+ assert_response :success
+ end
+
+ test "should update transaction" do
+ put :update, id: @transaction, transaction: { buyer_id: @transaction.buyer_id, prediction_id: @transaction.prediction_id, price: @transaction.price, quantity: @transaction.quantity, seller_id: @transaction.seller_id }
+ assert_redirected_to transaction_path(assigns(:transaction))
+ end
+
+ test "should destroy transaction" do
+ assert_difference('Transaction.count', -1) do
+ delete :destroy, id: @transaction
+ end
+
+ assert_redirected_to transactions_path
+ end
+end
View
4 test/unit/helpers/transactions_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class TransactionsHelperTest < ActionView::TestCase
+end
View
7 test/unit/transaction_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TransactionTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.