inossidabile / wash_out

Dead simple Rails 4/5 SOAP server library
Other
342 stars 241 forks source link

Defining custom SOAP object fields for ActiveRecord models #237

Open xlts opened 7 years ago

xlts commented 7 years ago

I'm unable to change SOAP ComplexType fields for ActiveRecord models and I'm always stuck with the model's DB columns including created_at and possibly other unwanted fields, like in this WSDL excerpt:

<xsd:complexType name="my_model">
    <xsd:sequence>
      <xsd:element name="id" type="xsd:int" nillable="true"/>
      <xsd:element name="name" type="xsd:string" nillable="true"/>
      <xsd:element name="created_at" type="xsd:dateTime" nillable="true"/>
      <xsd:element name="updated_at" type="xsd:dateTime" nillable="true"/>
  </xsd:sequence>
</xsd:complexType>

What I want to do is to define new fields and/or get rid of others. Obviously I tried

class MyModel < ActiveRecord::Base
  map name: :string, other_field: :double
end

but it results in undefined method 'map' for #<Class:0x00000005bada60>. Of course, making MyModel extend from WashOut::Type works fine but I want to keep instances of MyModel ActiveRecord objects. Is there any way to deal with it other than writing another class (extending from WashOut::Type) which would serve as a container around MyModel? Am I missing a module which I can include into an ActiveRecord class?

jdsampayo commented 7 years ago

I think you need to create a WashOut::Type class, this is how I map mine:

class Soap::Account < WashOut::Type
  map name: :string,
    balance: :double,
    minimum_balance: :double,
    currency: :string
end
xlts commented 7 years ago

@jdsampayo as I said, creating a container-like class such as the one you proposed works fine. However I'd prefer to be able to configure an existing ActiveRecord model rather than duplicate code by having to define separate classes for all models.