January 3, 2008 at 11:28 am
· Filed under csses, rails, ruby
Update: CSSes is now a gem! No need to install the plugin for each app.
I’ve updated CSSes to handle a bunch more Rails HTML helpers, so your forms and images and links should all get the proper treatment and their own nicely alphabetized entries in the generated application.css file.
See it in action!
Say you have this view (though let it be known that CSSes scours your helpers, too):
<h1 id="pomp">The Grand Demonstration</h1>
<p class="wing">Super Mario Bros. 3 reference</p>
<%= image_tag('joy.png', :class => 'happy') %>
<%= link_to('Read', {:action => :read, :id => 4}, {:id => 'four'}) %>
<!-- and it works for form tags too -->
Do this:
script/generate csses
And look at that sweet generated application.css:
a {}
a:visited {}
a:hover {}
a:active {}
a#four {}
a#four:visited {}
a#four:hover {}
a#four:active {}
h1 {}
h1#pomp {}
img {}
img.happy {}
p {}
p.wing {}
Get it for yourself:
sudo gem install csses
Permalink
December 31, 2007 at 12:40 am
· Filed under csses, rails, ruby
I try to follow some good guidelines when developing a website. I separate content from style with CSS, for example, by judiciously using class and id attributes for html elements. Of course, I also usually leave the styling until the content has been mostly completed. The problem with that, though, is I end up with many views and many helpers that reference class and id attributes for elements I haven’t styled yet.
So I made CSSes. It searches helpers and views (you’re not putting style information in your controllers or models are you?) for references to html entities, and creates a bare-bones CSS file.
It has a few shortcomings. It doesn’t yet handle link_to* or image_tag or any of the form building methods, just raw referenced html. It’s only Rails 2.0.x friendly so far. It makes just one file, public/stylesheets/application.css, and you can’t control the name. If you use the < character in html attribute values, it will probably make a little noise. It doesn’t have any unit tests.
But! It was sure nice to run after working on a site for a while and finding this waiting for me:
body {}
div {}
div#author_bio {}
div#author_url {}
div#chapbook_exceprt {}
...
div.chapbook_links {}
div.poem_content {}
div.poem_links {}
...
h1 {}
h1#author_name {}
h1#chapbooks_header {}
...
Get it:
sudo gem install csses
Run it:
script/generate css
Any help at all would be greatly appreciated; it’s pretty raw. But I plan on making it more robust and more fun.
Update!: We’ve got tests, automatic anchor tag pseudo-classes, and basic link_to* and image_tag support (figuring out how to handle class and id attributes in rails helpers is a little trickier than I guessed at first). AND: it’s a gem now.
Permalink
December 13, 2007 at 1:04 am
· Filed under rails, GuessMethod, irb, ruby
I was looking at the code for Giles Bowkett’s utility_belt gem, and came across something I wish I had known about ages ago. There’s a way to execute some code in an .irbrc after everything else when the irb session starts (technically, when the context changes). The problem with GuessMethod and Rails was that GuessMethod can’t load first. Problem solved.
So now my .irbrc has this line to include GuessMethod in a way that works with script/console:
IRB.conf[:IRB_RC] = Proc.new { require 'guessmethod' }
Of course, if you’re using that IRB.conf[:IRB_RC] for something else (like using utility_belt), something’s going to overwrite something.
Anyway, I’ve finally just given up and wrapped my entire .irbrc in a big unless:
unless $0 == 'script/server'
I’m confused as to why script/server loads .irbrc for every request, but at least this mitigates that issue a bit. (As an aside, can someone tell me why it does that?)
Permalink
November 8, 2007 at 7:16 pm
· Filed under rails, GuessMethod, ruby
It finally happened. I figured out how to get GuessMethod (particularly the GuessConstant half of things) and Rails to behave. There’s still a little kludginess to it, but they can work magic together:
mvb:~/rails/depot cms$ script/console
Loading development environment.
>> require 'guessmethod'
=> ["GuessMethod", "GuessConstant", "GuessMethodOptions", "GuessMethodGuesser", "GuessMethodOutputter"]
>> LineItm.find(:first)
no constant in threshold: for LineItm, sending to Object’s const_missing
NameError: uninitialized constant LineItm
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:266:in `load_missing_constant’
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:452:in `const_missing’
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:464:in `unguessed_const_missing’
from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.1.0/lib/guessmethod.rb:87:in `const_missing’
from (irb):2
>> LineItem
=> LineItem
>> LineItm.find(:first)
attention: replacing non-existant constant LineItm with LineItem for Object
=> #<LineItem:0×30c1338 @attributes={”order_id”=>”1″, “total_price”=>”3″, “quantity”=>”1″, “product_id”=>”8″, “id”=>”1″}>
Once Rails loads a constant GuessConstant can find it, and your poor typing stops holding you back.
There’s just one thing (there’s always just one thing). Things don’t go very well if you require GuessMethod in your irbrc. You have to require it manually in the console. It works fine when script/server gets a hold of it, but not for script/console. I suppose they load everything in a different order (but this isn’t something I’ve looked into yet). Rails has to show up to the dance first.
It was this old Dr. Nic post that got me over the hurdle solving this one. Thanks Dr. Nic.
sudo gem install guessmethod
Enjoy!
Permalink
August 14, 2007 at 10:39 am
· Filed under rails, ruby
I don’t quite understand why Mongrel and WEBrick insist on loading your .irbrc every single request. It’s especially bad when your .irbrc is full of stuff that kills Rails (GuessMethod, redefining String#match to handle Oniguruma regular expressions, etc.).
Thankfully, Peter Jones clued me in to this:
env IRBRC=/dev/null script/server
Besides not killing Rails anymore, every request doesn’t load 8 unneeded gems, complain that there’s no constant named IRB, and define methods only usable for irb sessions.
Permalink