Skip to content

Commit

Permalink
implement payments 🎉 closes #29 closes #35
Browse files Browse the repository at this point in the history
* fix broken specs
* DRY up the responses for payments
* remove payments from the GET /api/bills/id call
* add has_many relation for bill -> payments on the frontend
* refactor logic around payments and bills
  • Loading branch information
Mohnish Thallavajhula committed Feb 25, 2015
1 parent fefa4f9 commit 19cc370
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 20 deletions.
4 changes: 0 additions & 4 deletions app/assets/javascripts/money/collections/payments.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

M.Payments = Backbone.Collection.extend({
url: function() {
return '/api/bills/' + this.billId + '/payments';
},

model: M.Payment,

initialize: function() {
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/money/models/bill.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ M.Bill = Backbone.Model.extend({
},

initialize: function() {
this.payments = new M.Payments();
this.payments.url = '/api/bills/' + this.get('id') + '/payments';

// this is to avoid https://github.com/jashkenas/backbone/issues/1845
// we basically propagate the error from the model to the
// collection and let the collection view handle the error
Expand Down
7 changes: 3 additions & 4 deletions app/assets/javascripts/money/models/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
M.Payment = Backbone.Model.extend({
defaults: {
amount: '',
bill_id: '',
payment_source_id: ''
payment_source: '',
bill: ''
},

initialize: function() {
Expand All @@ -19,8 +19,7 @@ M.Payment = Backbone.Model.extend({
var errors = [];

if(_.isEmpty(attrs.amount)) errors.push('payment amount is invalid');
if(_.isEmpty(attrs.bill_id)) errors.push('please select a bill');
if(_.isEmpty(attrs.payment_source_id)) errors.push('please select a valid card');
if(_.isEmpty(attrs.payment_source) && _.isEmpty(attrs.payment_source_id)) errors.push('please select a valid card');

if (!_.isEmpty(errors)) return errors;
}
Expand Down
2 changes: 0 additions & 2 deletions app/assets/javascripts/money/templates/payments/index.jst.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
</div>
</div>

<input class="form-control" type="hidden" id="bill-id" name="bill_id" value="<%= billId %>" />

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-primary" type="submit">
Expand Down
4 changes: 1 addition & 3 deletions app/assets/javascripts/money/views/bill_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ M.BillView = M.BaseView.extend({
this.setPageTitle(this.model.get('name'));
this.$el.html(this.template(this.model.toJSON()));
$('#money').html(this.el);
var payments = new M.Payments();
payments.billId = this.model.get('id');
var paymentsView = new M.PaymentsView({ collection: payments });
new M.PaymentsView({ collection: this.model.payments });
return this;
},

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def update

@payment ||= Payment.new

if @payment.persisted? && @payment.valid?
if @payment.valid? && @payment.persisted?
status = :ok
else
status = :unprocessable_entity
Expand Down
4 changes: 2 additions & 2 deletions app/views/api/v1/payments/_show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
json.extract! payment, :id, :amount, :bill_id, :payment_source_id
json.extract! payment, :id, :amount

json.payment_date payment.created_at

json.bill do
json.extract! payment.bill, :id, :name
json.extract! payment.bill, :id, :name, :amount
end

json.payment_source do
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v1/payments/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
json.extract! @payment, :errors
json.partial! 'api/v1/payments/show', payment: @payment
json.partial! 'api/v1/payments/show', payment: @payment if @payment.persisted?
2 changes: 1 addition & 1 deletion app/views/api/v1/payments/update.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
json.extract! @payment, :errors
json.partial! 'api/v1/payments/show', payment: @payment
json.partial! 'api/v1/payments/show', payment: @payment if @payment.persisted?
1 change: 0 additions & 1 deletion spec/controllers/api/v1/bills_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
expect(response).to have_http_status(:success)
result = JSON.parse(response.body)
expect(result['id']).to eql(bill.id)
expect(result['payments'].size).to eql(1)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/payments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
patch :update, params
expect(response).to have_http_status(:unprocessable_entity)
result = JSON.parse(response.body)
expect(result['errors']).to be_blank
expect(result['errors']).not_to be_blank
end
end
end
Expand Down

0 comments on commit 19cc370

Please sign in to comment.