Skip to content

Instantly share code, notes, and snippets.

@superlou
Created August 23, 2013 00:13
Show Gist options
  • Save superlou/6314251 to your computer and use it in GitHub Desktop.
Save superlou/6314251 to your computer and use it in GitHub Desktop.
Unpermitted parameters: book, patron
attr = Ember.attr
hasMany = Ember.hasMany
belongsTo = Ember.belongsTo
Library.Checkout = Ember.Model.extend
id: attr(Number)
book: belongsTo('Library.Book', {key: 'book', embedded: true})
patron: belongsTo('Library.Patron', {key: 'patron', embedded: true})
status: attr()
closed_at: attr(Date)
created_at: attr(Date)
isValid: (->
if @get('book.isLoaded') and @get('patron.isLoaded')
return true
return false
).property('book.isLoaded', 'patron.isLoaded')
isOpen: (->
if @get('status') == "out" then true else false
).property('status')
duration: (->
if @get('created_at') and @get('closed_at')
diff = moment.duration(moment(@get('closed_at')).valueOf() - moment(@get('created_at')).valueOf())
dur = moment.duration(diff)
return dur
else if @get('created_at')
diff = moment.duration(moment().valueOf() - moment(@get('created_at')).valueOf())
dur = moment.duration(diff)
return dur
else
return null
).property('created_at', 'closed_at')
Library.Checkout.reopenClass
findByInfo: (book_info, patron_info) ->
@find('book_info:' + book_info)
Library.Checkout.url = '/checkouts'
Library.Checkout.adapter = Library.CheckoutAdapter.create()
class CheckoutSerializer < ActiveModel::Serializer
attributes :id, :status, :created_at, :closed_at
has_one :book, embed: :object
has_one :patron, embed: :object
end
class CheckoutsController < ApplicationController
respond_to :json
def index
if params[:status] == 'out'
checkouts = Checkout.where(status: 'out')
else
checkouts = Checkout.all
end
respond_with checkouts
end
def show
respond_with Checkout.find(params[:id])
end
def create
respond_with Checkout.create(checkout_params)
end
def update
respond_with Checkout.update(params[:id], checkout_params)
end
def destroy
respond_with Checkout.destroy(params[:id])
end
private
def checkout_params
params.require(:checkout).permit(:patron, :book, :patron_id, :book_id, :status, :closed_at, :created_at)
end
end
{"id"=>nil, "status"=>"out", "closed_at"=>nil, "created_at"=>nil, "book"=>{"id"=>1, "code"=>"123", "volume"=>2, "stock"=>1, "title"=>"A Road Ago", "adult"=>true, "notes"=>nil, "checkout_ids"=>[22]}, "patron"=>{"id"=>1, "code"=>"1", "name"=>"Chen Kenichi", "gender"=>nil, "email"=>nil}, "checkout"=>{"book"=>{"id"=>1, "code"=>"123", "volume"=>2, "stock"=>1, "title"=>"A Road Ago", "adult"=>true, "notes"=>nil, "checkout_ids"=>[22]}, "patron"=>{"id"=>1, "code"=>"1", "name"=>"Chen Kenichi", "gender"=>nil, "email"=>nil}, "status"=>"out", "closed_at"=>nil, "created_at"=>nil}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment