fabrik42/acts_as_api

[enhancement] Support dasherize: false

masterkain opened this issue · 9 comments

Hello,
currently I have found no way on Rails 3+ to use underscores in the generated XML attributes instead of dashes.

For a legacy API version I need this functionality enabled so I have to turn if on/off dinamically, I saw some options, but nothing really specific to this issue. I tried also with:

respond_with(@collection, api_template: :v1_public, dasherize: false)

but no dice.

did you try to overwrite:

ActsAsApi::Config.dasherize_for

by default it is

ActsAsApi::Config.dasherize_for = [:xml]

so you could try to set

ActsAsApi::Config.dasherize_for = []

e.g. somewhere in your initializers.

Please tell me if this works for you :)

Yes, but I need this setting to be different by api version..

Can it work as runtime setting? Best placed in a controller.

Il giorno 13/dic/2011, alle ore 18:30, Christian Bäuerlein ha scritto:

did you try to overwrite:

ActsAsApi::Config.dasherize_for

by default it is

ActsAsApi::Config.dasherize_for = [:xml]

so you could try to set

ActsAsApi::Config.dasherize_for = []

e.g. somewhere in your initializers.

Please tell me if this works for you :)


Reply to this email directly or view it on GitHub:
#52 (comment)

@fabrik42 I tried passing the empty array to dasherize_for in an initializer but it got no effect, xml is still being returned like

<duration-in-seconds type="integer">232</duration-in-seconds>

@fabrik42: I have the same enhancement requirement as Masterkain. We want to use underscores in the generated XML attributes instead of dashes. And ActsAsApi::Config.dasherize_for = [] does not work in my dev box.

Ok, I will look into it these days. :)

Ok, I tried to reproduce your problem but for me everything works like I would expect:

class UsersController < ApplicationController

  respond_to :json, :xml

  self.responder = ActsAsApi::Responder

  def index
    @users = User.where("").all
    respond_with @users, :api_template => :name_only, :dasherize => params[:dasherize] == "true"
  end

  def show
    @user = User.find(params[:id])
    respond_with @user, :api_template => :name_only, :dasherize => params[:dasherize] == "true"
  end

end

http://localhost:3000/users.xml?dasherize=false gives me

<?xml version="1.0" encoding="UTF-8"?>
<users type="array">
  <user>
    <first_name>Me</first_name>
    <duration_in_seconds type="integer">4</duration_in_seconds>
  </user>
  <user>
    <first_name>Me2</first_name>
    <duration_in_seconds type="integer">4</duration_in_seconds>
  </user>
</users>

http://localhost:3000/users.xml?dasherize=true gives me

<?xml version="1.0" encoding="UTF-8"?>
<users type="array">
  <user>
    <first-name>Me</first-name>
    <duration-in-seconds type="integer">4</duration-in-seconds>
  </user>
  <user>
    <first-name>Me2</first-name>
    <duration-in-seconds type="integer">4</duration-in-seconds>
  </user>
</users>

I tried this with Rails 3.1.3 and didn't change anything in ActsAsApi::Config.

Do you guys use the ActsAsApi::Responder?

Best,

Chris

Thanks very much. It works if I use :dasherize => false.

@hebing0625 You're welcome :)

@masterkain Do you maybe use another responder than the ActsAsApi::Responder?

@masterkain It worked for @hebing0625 and I didn't heard something from you, so I'm closing this issue right now.

If you still face the problems, just reopen it. :)

Cheers,

Chris