New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shops and shop_inventory_items #6
Open
mlapeter
wants to merge
39
commits into
master
Choose a base branch
from
add-shops
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
0aa6ca5
add shops
mlapeter 93ad07b
added ShopInventoryItems
mlapeter 9e08d44
adding missing files
mlapeter 773ab6e
schema
492ca39
Merge branch 'add-shops' of https://github.com/OpenSourcePortland/obe…
bc36af2
added good model and beginning to write wrapper methods for inventory…
d0c519a
finish adding wrapper methods to shop
mlapeter df4c7ff
add buy and sell actions to store
mlapeter c1bf794
fixing schema issue
943952c
added character_inventory_item table and added tests
5f694db
added character_inventory_item table and added tests
6a32f30
fixing merge conflict
93b198f
adding reminder to finish shop#sell method
cdf8ca6
completed buy and sell methods for Shop
mlapeter 020506f
mark added Qaurtermaster model and transactionable
mlapeter 25fd746
working on tests for quartermaster and transactionable
mlapeter 57af5d3
finishing refactoring of quartermaster and transactionable
bethtabler c055762
created polymorphic possessions model and owning module
54a79a9
created polymorphic possessions model and owning module
b229986
Merge branch 'add-shops' of https://github.com/OpenSourcePortland/obe…
15774a6
fix test failure due to same name method in owning and transactionable
mlapeter 1a14e6c
fix merge conflict between master and add-shops
mlapeter 2c70efb
in progress cleaning up character shop specs
mlapeter 916f138
adding support specs
mlapeter 7631c78
getting owning specs running as shared examples for shops and characters
a2a3d52
cleaning up shop and character inventory_items
e77651e
removing unused files
636d2e9
adding polymorphic wares table
ccf0c1d
adding vending module and tests
b31a804
adding vending module and tests
eb3299b
Merge branch 'add-shops' of https://github.com/OpenSourcePortland/obe…
c27a589
Merge branch 'add-shops' of https://github.com/OpenSourcePortland/obe…
1767d45
Merge branch 'add-shops' of https://github.com/OpenSourcePortland/obe…
8e7430f
Move nil-fixing code from migration to separate maintenance rake task
marktabler 5f8c855
mayday mayday mayday
mlapeter 2233956
converting owning and vending into transactionable
5a95e9f
transactionable and quartermaster working
mlapeter da858e1
adding tests
mlapeter 3e61695
done with transactionable and quartermaster and full tests
mlapeter File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the Goods controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the ShopInventoryItems controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the Shops controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class GoodsController < ApplicationController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
class ShopsController < ApplicationController | ||
|
||
def index | ||
@shops = Shop.all | ||
|
||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.json { render json: @shops } | ||
end | ||
end | ||
|
||
def show | ||
@shop = Shop.find(params[:id]) | ||
|
||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.json { render json: @shop } | ||
end | ||
end | ||
|
||
def new | ||
@shop = Shop.new | ||
|
||
respond_to do |format| | ||
format.html # new.html.erb | ||
format.json { render json: @shop } | ||
end | ||
end | ||
|
||
def edit | ||
@shop = Shop.find(params[:id]) | ||
end | ||
|
||
def create | ||
@shop = Shop.new(params[:shop]) | ||
|
||
respond_to do |format| | ||
if @shop.save | ||
format.html { redirect_to @shop, notice: 'Shop was successfully created.' } | ||
format.json { render json: @shop, status: :created, location: @shop } | ||
else | ||
format.html { render action: "new" } | ||
format.json { render json: @shop.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
|
||
def update | ||
@shop = Shop.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @shop.update_attributes(params[:shop]) | ||
format.html { redirect_to @shop, notice: 'Shop was successfully updated.' } | ||
format.json { head :no_content } | ||
else | ||
format.html { render action: "edit" } | ||
format.json { render json: @shop.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
|
||
def destroy | ||
@shop = Shop.find(params[:id]) | ||
@shop.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to shops_url } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
def sell_to_character | ||
shop = Shop.find(params[:id]) | ||
character = Character.first | ||
item = ShopInventoryItem.find(params[:item_id]) | ||
quantity = params[:quantity].to_i | ||
respond_to do |format| | ||
if shop.sell(character, item, quantity) | ||
format.html { redirect_to shop, notice: 'Item sold to character!' } | ||
else | ||
format.html { redirect_to shop, notice: 'Item not sold!' } | ||
end | ||
end | ||
end | ||
|
||
def buy_from_character | ||
shop = Shop.find(params[:id]) | ||
character = Character.first | ||
item = ShopInventoryItem.find(params[:item_id]) | ||
quantity = params[:quantity].to_i | ||
respond_to do |format| | ||
if shop.buy(character, item, quantity) | ||
format.html { redirect_to shop, notice: 'Item purchased from character!' } | ||
else | ||
format.html { redirect_to shop, notice: 'Item not purchased!' } | ||
end | ||
end | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class TransactionsController < ApplicationController | ||
|
||
def create | ||
find_resources | ||
Quartermaster.transact(@buyer, @seller, @good, params[:quantity]) | ||
end | ||
|
||
|
||
private | ||
|
||
def find_resources | ||
find_buyer | ||
find_seller | ||
find_good | ||
end | ||
|
||
def find_buyer | ||
if params[:buyer_type] == "character" | ||
@buyer = Character.find(params[:buyer_id]) | ||
else | ||
@buyer = Shop.find(params[:buyer_id]) | ||
end | ||
end | ||
|
||
def find_seller | ||
if params[:seller_type] == "character" | ||
@seller = Character.find(params[:seller_id]) | ||
else | ||
@seller = Shop.find(params[:seller_id]) | ||
end | ||
end | ||
|
||
def find_good | ||
@good = Good.find(params[:good_id]) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module GoodsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ShopsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Good < ActiveRecord::Base | ||
attr_accessible :name | ||
|
||
has_many :shops, :through => :shop_inventory_items | ||
has_many :shop_inventory_items | ||
has_many :character_inventory_items | ||
has_many :characters, :through => :character_inventory_items | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Possession < ActiveRecord::Base | ||
attr_accessible :good_id, :ownable_id, :ownable_type, :quantity | ||
|
||
belongs_to :ownable, polymorphic: true | ||
|
||
def should_be_destroyed? | ||
quantity <= 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
class Quartermaster | ||
|
||
#Quartermaster.transact(Character.first, Shop.first, Good.first, 20) | ||
|
||
def self.transact(buyer, seller, good, quantity) | ||
self.new.transact(buyer, seller, good, quantity) | ||
end | ||
|
||
def transact(buyer, seller, good, quantity) | ||
@buyer = buyer | ||
@seller = seller | ||
@good = good | ||
@quantity = quantity | ||
perform_transaction | ||
end | ||
|
||
def perform_transaction | ||
if transaction_valid? | ||
@buyer.add_good(@good, @quantity) | ||
@seller.reduce_good(@good, @quantity) | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
def transaction_valid? | ||
validate_stock && validate_quantity && validate_can_afford && validate_will_buy | ||
end | ||
|
||
def validate_shop_trades_in | ||
@buyer.trades_in?(@good) | ||
end | ||
|
||
def validate_stock | ||
@seller.has_enough?(@good, @quantity) | ||
end | ||
|
||
def validate_quantity | ||
@quantity >= 0 | ||
end | ||
|
||
def validate_can_afford | ||
true | ||
# Pending implementation of currency | ||
end | ||
|
||
def validate_will_buy | ||
@buyer.will_buy?(@good) | ||
end | ||
|
||
end | ||
|
||
|
||
|
||
# def character.buy_good(character, good_receiving, quantity_receiving, quanity_giving) | ||
# trade(giver, getter, giving, quantity_giving, getting, getting_quantity) | ||
# end | ||
# | ||
# | ||
# | ||
# | ||
# 50 gold | ||
# | ||
# def buy_good(character, good_receiving, quantity_receiving, quanity_giving) #from store | ||
# trade(giver, getter, gold, quantity_giving, getting, getting_quantity) | ||
# end | ||
# | ||
# def sell_good() #to store | ||
# trade(giver, getter, giving, quantity_giving, gold, getting_quantity) | ||
# end | ||
# | ||
# def transact(giver, getter, giving, quantity_giving, getting, getting_quantity) | ||
# #transact | ||
# end | ||
# | ||
# def offer_trade(offering, offering_quantity, getting, getting_quantity) | ||
# | ||
# end | ||
# | ||
# # store buys from character joe (buy_price) | ||
# # character joe sells_to_store #store dictates (buy_price) | ||
# joe.reduce_good(good, 10) | ||
# store.add_good(good, 10) | ||
# joe.add_good(gold, store.buy_price_of(good)) | ||
# store.reduce_good(gold, store.buy_price_of(good)) | ||
# | ||
# # character buys from store #store dictates (sell_price) | ||
# joe.add_good(good, 10) | ||
# store.reduce_good(good, 10) | ||
# store.add_good(gold, store.sell_price_of(good)) | ||
# joe.reduce_good(gold, store.sell_price_of(good)) | ||
# | ||
# # mary buys from joe | ||
# # character joe sells_to_character mary # selling character joe dictates (sell_price) | ||
# joe.reduce_good(good, 10) | ||
# mary.add_good(good, 10) | ||
# joe.add_good(gold, joe.sell_price_of(good)) | ||
# mary.reduce_good(gold, joe.sell_price_of(good)) | ||
# | ||
# # character joe buys from character mary # selling character mary dictates (sell_price) | ||
# joe.add_good(good, 10) | ||
# mary.reduce_good(good, 10) | ||
# joe.reduce_good(gold, mary.sell_price_of(good)) | ||
# mary.increase_good(gold, mary.sell_price_of(good)) | ||
# |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope nope nope nope. :)
This is too much logic for a controller. We will definitely need an endpoint to handle this kind of thing, but it might not even live in the Shops controller. Here are a couple of possibilities:
.buy
method on the buyer or as a.sell
method on the seller, but it's only one and it's the same every time)The important pattern is the "delegates the logic" bit. A controller should only support the channel over which a conversation is happening.
Have you ever had a relay call? It's where a person using a TDD is calling a person using a regular telephone. A TDD is text-based, of course, so there needs to be an adapter of some kind in the middle. That adapter is a Relay Operator, whose job is to read the text coming from the TDD out loud to the person on the phone, then transcribe the audio from the phone as text to the person using the TDD.
The Relay Operator is kind of like a controller. They can't actually participate in the conversation - they can only facilitate it. They are limited to helping with the actual channel of the conversation - you can ask them to re-read the last sentence, and you can ask them to spell a word as it was typed, but nothing else. I remember asking once whether a relay service as billable per minute, per call, or free, because it made a difference as to how I was going to handle the call; the operator duly typed the question to the customer to let them explain (it's a free, nationally-provided service).
So, in this buy/sell situation, our Controller should pass along the message between the right parties, but only that - incoming parameters, outgoing data. When asked questions like "What does buying a thing actually mean" or "how many parties are involved," the controller shouldn't know. It should know "Shop#buy: I send a good, a buyer, and a quantity to the correct shop. If successful, I send this message back; if not successful I send this other message back."