Skip to content

Instantly share code, notes, and snippets.

@Sunnyztj
Created January 29, 2014 05:21
Show Gist options
  • Save Sunnyztj/8682269 to your computer and use it in GitHub Desktop.
Save Sunnyztj/8682269 to your computer and use it in GitHub Desktop.
perfect code in ROR
One. coupon_id => coupon.business => business.stores
1.{
get '/:id', rabl: 'coupon' do
authenticate!
@coupon = Coupon.find params[:id]
end
object @coupon
attributes :id, :name, :category, :business_name, :expired_at
node(:expired_on) {|coupon| time_ago_in_words(coupon.expired_at) }
node(:business) {|coupon| partial('business', object: @coupon.business) }
}
2.{
get '/:id', rabl: 'business' do
authenticate!
@business = Business.find params[:id]
end
object @business
attributes :id, :website, :logo, :description
node(:stores) { |business| partial('_store', object: business.stores) }
attributes :id, :telephone, :address, :lat, :lng, :state
}
Two.
def available_coupons
coupons = Coupon.where("expired_at >= ?", Time.now)
coupons = coupons.where("id not in (?)", coupon_ids) if coupon_ids.count > 0
coupons
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment