appoxy / simple_record

An ActiveRecord like interface for SimpleDB. Can be used as a drop in replacement for ActiveRecord in rails.
http://www.appoxy.com
MIT License
129 stars 29 forks source link

as_json chokes on invalid data #51

Open pr1001 opened 12 years ago

pr1001 commented 12 years ago

While that doesn't exactly sound like a bad thing – it is being presented invalid data, after all! – I found the best way for one bad attribute not to take down everything was to monkeypatch the Json module:

module SimpleRecord::Json
  def as_json(options={})
    result = {
        'id' => self.id
    }
    result['json_class'] = self.class.name unless options && options[:exclude_json_class]
    defined_attributes_local.each_pair do |name, val|
      if val.type == :belongs_to
        result[name.to_s + "_id"] = get_attribute_sdb(name) rescue nil
      else
        result[name] = get_attribute(name) rescue nil
      end
    end
    result
  end
end

Perhaps this could be an option or just mentioned in the README if nothing else?