Posted by Lance Ivy
Thu, 05 Jul 2007 22:50:00 GMT
At RailsConf07 someone asked me how to simplify forms in ActiveScaffold. What they wanted was to blend attributes from an associated model in with the main model. The premise was that the user doesn’t need to know about the separate models, so why differentiate them on the form? Well we talked and came up with a solution where you proxy methods from one model to another, then add those methods to ActiveScaffold as virtual attributes.
People are still asking about this. I figure it’s time to abstract the behavior, right? Introducing ActsAsProxy, a very simple plugin that lets you do something like:
1
2
3
4
5
6
7
8
9
10
11
| class User < ActiveRecord::Base
has_one :address
proxies :address, :city, :state, :zip
end
u = User.new
u.address_city = 'Schenectady'
u.address_state = 'New York'
u.address_zip = 12345
assert_equal u.address_city, u.address.city
|
Posted in Ruby on Rails, Open Source, Releases | Tags ActsAsProxy | 2 comments
Posted by Lance Ivy
Wed, 04 Jul 2007 12:29:00 GMT
So you’re working on a large codebase and need to change something. But it’s all over the place. Why hunt it down when you can do an easy search-and-replace right from the shell? I wrote this shell script back in my PHP days when I had to move some database tables around. It’s not the prettiest (it is written in bash), but it’s functional and gets the job done.
To install, place this script in your $PATH (I like to put stuff like this in ~/bin/) and you’re good to go. The csed script operates recursively from your current directory.
Script is available after the jump.
Note: there’s a bug I haven’t bothered to fix related to the result set size. It’s really benign, don’t worry.
Read more...
Posted in Open Source | Tags csed, searchandreplace | 1 comment
Posted by Lance Ivy
Wed, 04 Jul 2007 12:13:00 GMT
I’ve been working on an issue with the SlimTimer API where a YAML response would end up with mismatched anchors and references. Turns out that the YAML emitter attempts to minimize the output by watching for references to an object it’s already seen and creating anchors (&id001) and references (*id001). It does this by keeping track of the object ids. Simple enough, right?
Yeah, simple until you have a large enough dataset filled with complex objects (like ActiveRecord::Base) that Ruby’s garbage collector kicks in. Then you can end up with reused object ids, wreaking havoc with your pretty YAML output. It’s always a bit frustrating to find a bug in core libraries that people have known about for a while but no one has fixed.
But hey, this is Ruby, right? It’s something of a DIY language, so I kept looking around until I discovered the GC (Garbage Collector) module. And from there I wound up with this little solution snippet:
class Object
def without_gc(&block)
GC.disable
result = yield
GC.enable
return result
end
end
Which lets you swap the problematic:
For a rawer but much more reliable:
without_gc {@records.to_yaml}
Posted in Ruby | Tags ruby, slimtimer, yaml | no comments