I’m a couple of weeks late, but I just finished reviewing . While a very nice and “magical” feature, I’ve got to admit that I’m really not that crazy about how it works.
Let me be the first to admit that there’s no one right way to do things. In fact, I’m writing this post particularly because I have a few objections to how this functionality is ultimately exposed, and I’d like to hear arguments from those who disagree. In other words, let me acknowledge the possibility that I am the misguided one.
We are currently working on a project that involves Flex and active resource + will_paginate and we needed to be able to paginate the xml transactions easily. Unfortunately, will_paginate and to_xml don’t play nicely when it comes to adding the current_page, total_pages, and page attributes to the xml. After many failed attempts I went looking around and found in a few of that some people had solved this problem but, I didn’t want to install another version of the gem to risk breaking other apps on the server so I did it the rails way!
I started by creating a module that opens up the will_paginate class and includes ActiveResource and alias method chain the to_xml method to include these values. Example code below.
#enviroment.rb
...
require'to_xml_extensions'
#lib/to_xml_extensions.rbmodule WillPaginateHelpers
includeActiveSupport::CoreExtensions::Array::Conversions
def to_xml_with_collection_type(options = {})
serializeable_collection.to_xml_without_collection_type(options)do|xml|
xml.tag!(:current_page, {:type =>ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[current_page.class.name]}, current_page)
xml.tag!(:per_page, {:type =>ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[per_page.class.name]}, per_page)
xml.tag!(:total_entries, {:type =>ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[total_entries.class.name]}, total_entries)end.sub(%{type="array"}, %{type="collection"})end
alias_method_chain :to_xml, :collection_typedef serializeable_collection #:nodoc:# Ugly hack because to_xml will not yield the XML Builder object when empty?
empty? ? returning(self.clone){|c| c.instance_eval{|i|def empty?; false; end}} : selfendendWillPaginate::Collection.send(:include, WillPaginateHelpers)
This now gives me the proper xml when I call to_xml
If you need to process 1000 items independently, the simplest thing to do is to handle them one-by-one. But what if there is a large delay that is not caused by your application when you handle an item? What if you need to make a 100ms web request per item, only to do about 10ms of processing on that item? This would take 1000 x 110ms (100ms to wait, 10ms to process)!
ActiveRecord provides a powerful method to all its records called to_xml. Most web developers using RoR should be familiar with its usage and hopefully use it for their simple xml production needs. But what do you do when you need to produce xml that is selective, includes associations, or contains custom tags? There are many ways to manipulate to_xml’s output and I’ll explain a few below. Read the rest of this entry »
So the problem is that I have an ActiveRecord model that has a :has_many relationship to another model (we’ll call this one object), but when I am in the view context I didn’t want to have to loop through the object each time to determine which data was being displayed. Object has many attributes (approximately 30) and many are often null for a given instance. So I decided to add a method to my model to loop through all of objects and determine which data should be included. Pretty much the rule was that if there was no data for a particular attribute temporarily save it to a copy of the object and then return that. This is what I came up with.
However there was a flaw here. Every time I would view the page all of the data in the objects was getting overwritten with one copy of it. After banging my head on the desk it was realized that tmp[key] = o[key] was actually writing the changes to the database permanently rewriting all of the objects (which still seems counter intuitive to me, because it seems like only the first record should have been the one changing). But the solution was pretty simple. The working method is as follows.
I had been working on a project in which we wanted to utilize WebDAV (namely for editing Word & Excel Documents that were saved in our application). In order to do this we decided to use a plugin from that can be found . It was pretty easy to hook up after a little direction from a guy over at Benryan Inc [apologies I cannot find a link for them], but there was a major issue. When opening a document through the ActiveX controller for editing it was opening in Read-Only mode.
After a few starts and stops, many hours of reading through the , and browsing through the http traffic using – it was determined that locking was the issue.
Rails 2.1 introduced in the MySQL Adapter “smart integer columns.” The idea was to use the :limit option to determine whether a smallint, int, or bigint should be used. This is something that the Postgres adapter had already previously implemented. The relevant code in activerecord/lib/active_record/connection_adapters/mysql_adapter.rb is: