mardi 4 août 2015

How can i generate json response from rabl gem and money gem with the database table i have ?

This is the json i want to build from rabl gem

M-0 {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
M-1 {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
M-2 {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
M-3 {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
M-4 {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
M-5 {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"}

This is my database table

create_table "merchant_monthly_stats", force: true do |t|
    t.integer  "created_count",        default: 0
    t.integer  "ready_count",          default: 0
    t.integer  "paid_count",           default: 0
    t.integer  "partially_paid_count", default: 0
    t.integer  "cancelled_count",      default: 0
    t.integer  "failed_count",         default: 0
    t.integer  "processed_count",      default: 0
    t.integer  "expired_count",        default: 0
    t.integer  "merchant_id",                          null: false
    t.integer  "year",                                 null: false
    t.integer  "month",                                null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "overdue_count",        default: 0
    t.integer  "fee_centimes",         default: 0,     null: false
    t.string   "fee_currency",         default: "XAF", null: false
    t.integer  "revenue_centimes",     default: 0,     null: false
    t.string   "revenue_currency",     default: "XAF", null: false
  end

This is my controller method

def history
    current_month = Time.now.month
    current_year = Time.now.year
    list_of_last_six_months = [current_month]
    5.times do |i|
      list_of_last_six_months << (current_month - i )
    end 

    month_array =[]
    list_of_last_six_months.each { |month|
      record = MerchantMonthlyStat.find_by merchant_id: current_api_user.id, year: current_year, month: month
      m = { 
        :revenue => Money.new(record.revenue_centimes , record.revenue_currency).format,
        :fees => Money.new(record.fee_centimes,record.fee_currency).format, 
        :created => record.created_count , 
        :paid => record.paid_count , 
        :partially_paid => record.partially_paid_count }
      month_array.push(m)
    }
    respond_with(month_array, status: :ok, template: 'api/merchant/mobiles/history')
  end

My question is how do i write the rabl template 'api/merchant/mobiles/history' to get the above json response ?

Note : I am using rails money gem. Thats why there is Money.new etc

Aucun commentaire:

Enregistrer un commentaire