2

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

Update:

If i have following :

# app/app.rb
get "/posts", :provides => [:json, :xml] do
  @user = current_user
  @posts = Post.order("id DESC")
  render "posts/index"
end

Rabl docs have written following rabl for it(which does not make sense to me) :

# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

But i dont know how to apply this to get my month_array in rabl and in the format i want ? Anybody wanting to solve this ? I am stuck.

syam
  • 892
  • 8
  • 19
  • 1
    But what you provided doesn't look like JSON. What are these `M-0`, `M-1`, etc. labels? – EugZol Aug 04 '15 at 12:29
  • 1
    I don't see any efforts. Your question is _write code for me_. – Roman Kiselenko Aug 04 '15 at 12:32
  • So what do you think who wrote rest of the things above? They are not efforts ? I just dont know lot in rails. I am learning since a week and thats the progress i could do. Thats all. – syam Aug 04 '15 at 12:36
  • @EugZol i have corrected the json in question – syam Aug 04 '15 at 12:39
  • @Зелёный i am asking to teach me how to do it. And not write code. Don't write it. Tell me how its done. Whats the process ? – syam Aug 04 '15 at 12:42
  • @samo if you do not know much in Rails, I suggest following the Rails Guides "Getting Started" chapter, or doing Michael Hartl's Rails Tutorial. If you do not know much rails, it is difficult to understand how to use other gems with your app. – onebree Aug 04 '15 at 12:42
  • 1
    @samo [rabl gem](https://github.com/nesquena/rabl) has a lot of documentation, wonderful wiki pages and many examples in web. So this can be done if you try. – Roman Kiselenko Aug 04 '15 at 12:43
  • @samo SO is about teaching, but given a specific question. This is too broad. I would suggest asking this on a traditional forum (Google Groups? Reddit?) Rather than Q&A like SO. – onebree Aug 04 '15 at 12:44
  • @ Hunter Stevens so what do i do with current task? I need to achieve it. See if you guys dont know how to do it, please keep quite or if you want to help me, and you need more information, ask me. – syam Aug 04 '15 at 12:46
  • All i am asking is tell me some advice on syntax of rabl for generating a json arrray with values that i have in month_array. How can it be broad ? Think up guys, i think you can do it if you know rabl json api syntax ,given an array of information and a template to write it. Though i can not score a goal because i have never used rabl json api. – syam Aug 04 '15 at 12:52
  • I think i got lot of down votes for posting a good question that can be useful to others who have data but dont know rabl syntax. This is so rude. – syam Aug 04 '15 at 12:55
  • Thanks to those who actually see the problem. – syam Aug 04 '15 at 13:30

1 Answers1

1

Just use render :json

render json: month_array

No need to mess with templating, you already have built your array in controller.

Update from comments: Make month_array instance vairable – change my_array to @my_array. And then in your rabl template just write collection @my_array, object_root: false

EugZol
  • 6,476
  • 22
  • 41
  • Actually i proposed this to my senior already since i can easily do it. But he said we are using rabl, and i need to learn it. Otherwise i would have marked you as answer. :( – syam Aug 04 '15 at 13:06
  • 1
    Well, you can make month_array instance vairable – change `my_array` to `@my_array`. And then in your rabl template just write `collection @my_array, object_root: false` – EugZol Aug 04 '15 at 13:45
  • 1
    You are welcome. If your senior will ask you to refactor code to move more things from your controller to Rabl, feel free to ask another question, we'll figure something out :) Also, consider upvoting my answer if you like it. – EugZol Aug 04 '15 at 13:48
  • But i already marked it as an answer. Since that was most helpful. Could you please update your answer ? – syam Aug 04 '15 at 13:51
  • Thanks Eugzol. http://teohm.com/blog/2011/05/31/using-rabl-in-rails-json-web-api/ this also helped me to learn rabl syntax, in case someone wants to understand better – syam Aug 04 '15 at 14:12