Question

I am developing a Rails 3 app in a largely tabless capacity. I am using savon_model and ActiveModel to generate similar behaviour to ActiveRecord equivalents. Below is my code:

class TestClass
  include Savon::Model
  include ActiveModel::Validations

  # Configuration
  endpoint "http://localhost:8080/app/TestService"
  namespace "http://wsns.test.com/"

  actions :getObjectById, :getAllObjects

  attr_accessor :id, :name

  def initialize(hash)
    @id = hash[:id]
    @name = hash[:name]
  end

  client do
    http.headers["Pragma"] = "no-cache"
  end

  def self.all
    h = getAllObjects(nil).to_array
    return convert_array_hash_to_obj(h, :get_all_objects_response)
  end

  def self.find(id)
    h = getObjectById(:arg0 => id).to_hash
    return convert_hash_to_obj(h, :get_object_by_id_response)
  end

private

  def self.convert_array_hash_to_obj(arrayhash, returnlabel)
    results = Array.new

    arrayhash.each do |hash|
      results << convert_hash_to_obj(hash, returnlabel)
    end

    return results

  end

  def self.convert_hash_to_obj(hash, returnlabel)
    return TestClass.new(hash[returnlabel][:return])
  end

end

OK, so everything works as expected; values are pulled from the web service and onto the page. Unfortunately, when I look at the html produced at the client side there are some issues. The Show links are along the following lines:

/testclasses/%23%3CTestClass:0xa814cb4%3E

instead of...

/testclasses/1

So, I did a print of the object (hash?) to the console to compare the outputs.

[#<System:0xa814cb4 @id="1", @name="CIS">]

instead of what I believe it should be...

[#<System id="1", name="CIS">]

I have three questions:
1: What is the hex suffix on my class name when it is printed out
2: How can I modify my class to match the desired output when printed to the console?
3: Why are the frontend links (Show, Edit, Delete) broken and is there an easy fix?

Thanks so much for your time and apologies for rubbish code / stupid questions. This is my first Ruby or Rails app!

Gareth

Was it helpful?

Solution

  1. The hex suffix is the object id of your instance of System
  2. You can manipulate the output on the console by implementing an inspect instance method
  3. The Rails url helpers use the to_param instance method to build these links. You should implement this if you are going to use your class as an ActiveRecord substitute.

Generally speaking, if you want to use all the Rails goodies with an own implementation of a model class, you should use ActiveModel:Lint::Test to verify which parts of the ActiveModel APIs are working as expected.

More information can be found here: http://api.rubyonrails.org/classes/ActiveModel/Lint/Tests.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top