Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jackquack/BitPredictor
base: 200640aee1
...
head fork: jackquack/BitPredictor
compare: 22a58b4a2d
  • 2 commits
  • 16 files changed
  • 0 commit comments
  • 1 contributor
View
22 app/controllers/buys_controller.rb
@@ -4,4 +4,26 @@ def create
params[:buy][:user_id] = current_user.id
create!
end
+ def accept
+ @prediction = Prediction.find(params[:sell][:prediction_id])
+ @buy = Buy.find(params[:id])
+ @sell = Sell.create(
+ price: @buy.price,
+ number_of_shares: @buy.number_of_shares,
+ user_id: current_user.id,
+ prediction_id: @prediction.id
+ )
+ @transaction = Transaction.create(
+ buy_id: @buy.id,
+ sell_id: @sell.id,
+ number_of_shares: @buy.number_of_shares,
+ prediction_id: @prediction.id
+ )
+ @buy.transaction_id = @transaction.id
+ @buy.save
+ @sell.transaction_id = @transaction.id
+ @sell.save
+ flash[:notice] = "Transaction Executed saved!"
+ redirect_to prediction_path(@prediction.id)
+ end
end
View
22 app/controllers/sells_controller.rb
@@ -4,4 +4,26 @@ def create
params[:sell][:user_id] = current_user.id
create!
end
+ def accept
+ @prediction = Prediction.find(params[:sell][:prediction_id])
+ @sell = Sell.find(params[:id])
+ @buy = Buy.create(
+ price: @sell.price,
+ number_of_shares: @sell.number_of_shares,
+ user_id: current_user.id,
+ prediction_id: @prediction.id
+ )
+ @transaction = Transaction.create(
+ buy_id: @buy.id,
+ sell_id: @sell.id,
+ number_of_shares: @buy.number_of_shares,
+ prediction_id: @prediction.id
+ )
+ @buy.transaction_id = @transaction.id
+ @buy.save
+ @sell.transaction_id = @transaction.id
+ @sell.save
+ flash[:notice] = "Transaction Executed saved!"
+ redirect_to prediction_path(@prediction.id)
+ end
end
View
1  app/models/buy.rb
@@ -3,4 +3,5 @@ class Buy < ActiveRecord::Base
belongs_to :prediction
belongs_to :user
belongs_to :transaction
+ scope :incomplete, where(:transaction_id => nil)
end
View
1  app/models/prediction.rb
@@ -2,5 +2,6 @@ class Prediction < ActiveRecord::Base
attr_accessible :end_date, :name, :prediction, :source, :verified_by
has_many :sells
has_many :buys
+ has_many :transactions
belongs_to :user
end
View
1  app/models/sell.rb
@@ -3,4 +3,5 @@ class Sell < ActiveRecord::Base
belongs_to :prediction
belongs_to :user
belongs_to :transaction
+ scope :incomplete, where(:transaction_id => nil)
end
View
6 app/models/transaction.rb
@@ -1,6 +1,6 @@
class Transaction < ActiveRecord::Base
- attr_accessible :buyer_id, :prediction_id, :price, :quantity, :seller_id
+ attr_accessible :buy_id, :prediction_id, :price, :number_of_shares, :sell_id
belongs_to :prediction
- belongs_to :buyer, :class_name => "User", :foreign_key => "buyer_id"
- belongs_to :seller, :class_name => "User", :foreign_key => "seller_id"
+ belongs_to :buy
+ belongs_to :sell
end
View
49 app/views/predictions/show.html.haml
@@ -10,8 +10,24 @@
%b Created By:
= @prediction.user.name
+%h1 All Transactions
+
+%table
+ %tr
+ %th Price
+ %th Quantity
+ %th
+ %th
+ %th
+
+ - @prediction.transactions.each do |transaction|
+ %tr
+ %td= transaction.price
+ %td= transaction.number_of_shares
+ %td= link_to 'Show', transaction
+
%h1 Place Your Bits
-%h2 Listing sells
+%h2 All Sells
%table
%tr
@@ -21,18 +37,24 @@
%th
%th
- - @prediction.sells.each do |sell|
+ - @prediction.sells.incomplete.each do |sell|
%tr
%td= sell.number_of_shares
- %td= sell.user.name
+ %td= sell.price
%td= link_to 'Show', sell
- %td= link_to 'Edit', edit_sell_path(sell)
- %td= link_to 'Destroy', sell, :method => :delete, :data => { :confirm => 'Are you sure?' }
+ %td
+ = form_for sell, :url => accept_sell_path(sell), :method => :put do |ff|
+ = ff.hidden_field :sell_id, :value => sell.id
+ = ff.hidden_field :prediction_id, :value => @prediction.id
+ = ff.submit 'Accept'
+ - if current_user.id == sell.user_id
+ %td= link_to 'Edit', edit_sell_path(sell)
+ %td= link_to 'Destroy', sell, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Sell', new_prediction_sell_path(@prediction.id)
-%h2 Listing buys
+%h2 All Buys
%table
%tr
@@ -43,14 +65,19 @@
%th
%th
- - @prediction.buys.each do |buy|
+ - @prediction.buys.incomplete.each do |buy|
%tr
%td= buy.number_of_shares
- %td= buy.user.name
- %td= buy.prediction_id
+ %td= buy.price
%td= link_to 'Show', buy
- %td= link_to 'Edit', edit_buy_path(buy)
- %td= link_to 'Destroy', buy, :method => :delete, :data => { :confirm => 'Are you sure?' }
+ %td
+ = form_for buy, :url => accept_buy_path(buy), :method => :put do |ff|
+ = ff.hidden_field :buy_id, :value => buy.id
+ = ff.hidden_field :prediction_id, :value => @prediction.id
+ = ff.submit 'Accept'
+ - if current_user.id == buy.user_id
+ %td= link_to 'Edit', edit_buy_path(buy)
+ %td= link_to 'Destroy', buy, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Buy', new_prediction_buy_path(@prediction.id)
View
4 app/views/transactions/_form.html.haml
@@ -16,8 +16,8 @@
= f.label :price
= f.number_field :price
.field
- = f.label :quantity
- = f.number_field :quantity
+ = f.label :number_of_shares
+ = f.number_field :number_of_shares
.field
= f.label :prediction_id
= f.number_field :prediction_id
View
2  app/views/transactions/index.html.haml
@@ -16,7 +16,7 @@
%td= transaction.buyer_id
%td= transaction.seller_id
%td= transaction.price
- %td= transaction.quantity
+ %td= transaction.number_of_shares
%td= transaction.prediction_id
%td= link_to 'Show', transaction
%td= link_to 'Edit', edit_transaction_path(transaction)
View
6 app/views/transactions/show.html.haml
@@ -2,16 +2,16 @@
%p
%b Buyer:
- = @transaction.buyer_id
+ = @transaction.buy_id
%p
%b Seller:
- = @transaction.seller_id
+ = @transaction.sell_id
%p
%b Price:
= @transaction.price
%p
%b Quantity:
- = @transaction.quantity
+ = @transaction.number_of_shares
%p
%b Prediction:
= @transaction.prediction_id
View
12 config/routes.rb
@@ -2,8 +2,16 @@
resources :transactions
- resources :buys
- resources :sells
+ resources :buys do
+ member do
+ put 'accept'
+ end
+ end
+ resources :sells do
+ member do
+ put 'accept'
+ end
+ end
resources :predictions do
resources :buys
resources :sells
View
11 db/migrate/20130323175509_rename_transaction_buyer_and_seller_to_buy_and_sell.rb
@@ -0,0 +1,11 @@
+class RenameTransactionBuyerAndSellerToBuyAndSell < ActiveRecord::Migration
+ def up
+ rename_column :transactions, :buyer_id, :buy_id
+ rename_column :transactions, :seller_id, :sell_id
+ end
+
+ def down
+ rename_column :transactions, :buy_id, :buyer_id
+ rename_column :transactions, :sell_id, :seller_id
+ end
+end
View
9 db/migrate/20130323180246_rename_transaction_quantity_to_number_of_shares.rb
@@ -0,0 +1,9 @@
+class RenameTransactionQuantityToNumberOfShares < ActiveRecord::Migration
+ def up
+ rename_column :transactions, :quantity, :number_of_shares
+ end
+
+ def down
+ rename_column :transactions, :number_of_shares, :quantity
+ end
+end
View
12 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130323173942) do
+ActiveRecord::Schema.define(:version => 20130323180246) do
create_table "buys", :force => true do |t|
t.decimal "number_of_shares", :precision => 10, :scale => 0
@@ -45,13 +45,13 @@
end
create_table "transactions", :force => true do |t|
- t.integer "buyer_id"
- t.integer "seller_id"
+ t.integer "buy_id"
+ t.integer "sell_id"
t.integer "price"
- t.integer "quantity"
+ t.integer "number_of_shares"
t.integer "prediction_id"
- 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 "users", :force => true do |t|
View
4 test/fixtures/transactions.yml
@@ -4,12 +4,12 @@ one:
buyer_id: 1
seller_id: 1
price: 1
- quantity: 1
+ number_of_shares: 1
prediction_id: 1
two:
buyer_id: 1
seller_id: 1
price: 1
- quantity: 1
+ number_of_shares: 1
prediction_id: 1
View
4 test/functional/transactions_controller_test.rb
@@ -18,7 +18,7 @@ class TransactionsControllerTest < ActionController::TestCase
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 }
+ post :create, transaction: { buyer_id: @transaction.buyer_id, prediction_id: @transaction.prediction_id, price: @transaction.price, number_of_shares: @transaction.number_of_shares, seller_id: @transaction.seller_id }
end
assert_redirected_to transaction_path(assigns(:transaction))
@@ -35,7 +35,7 @@ class TransactionsControllerTest < ActionController::TestCase
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 }
+ put :update, id: @transaction, transaction: { buyer_id: @transaction.buyer_id, prediction_id: @transaction.prediction_id, price: @transaction.price, number_of_shares: @transaction.number_of_shares, seller_id: @transaction.seller_id }
assert_redirected_to transaction_path(assigns(:transaction))
end

No commit comments for this range

Something went wrong with that request. Please try again.