Netbeans Color Scheme
I recently switched from Aptana to Netbeans for me ruby editing.
If you are undecided in the IDE wars, you can download the 22Mb ruby version from here: http://download.netbeans.org/netbeans/6.0/final/
I was looking for a nice color scheme that doesn’t bombard me with to many rays. I found this scheme that claims to be a textmate styled scheme for netbeans. It works for me.
To install as a plugin follow the instructions in the post. To change schemes: Tools >> Options >> Fonts and Colors (button at the top) and choose a new profile. If you followed the instructions, the Aloha option should be in the list.
Enjoy.
Bending a django database to play with rails
I’m currently porting my band’s website from python/django to ruby/rails so here is the first installment. I don’t fancy recapturing to data or writing sql export and import scripts, so I’m just going to re mold the database to rails conventions in-place seeing as I’m discontinuing the old version. I have this table in the current database that stores news posts for the front page of the website:
table_name: blog_post
id : integer (primary key)
subject : varchar (200)
text : longtext
tags : varchar (200)
created : datetime
modified : datetime
author_id : integer (foreign key, user table)
The Django model
class Post(models.Model):
subject = models.CharField(maxlength=200)
text = models.TextField(help_text="""
*italics*
**bold**
- list item
http://www.codevader.com
""")
tags = models.CharField(maxlength=200)
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
author = models.ForeignKey(User);
monthstr = ''
class Meta:
app_label = "blog"
ordering = ['-created']
def __str__(self):
return self.subject
class Admin:
list_display = ('subject', 'created')
and here is my new rails version:
#Migration
class CreatePosts < ActiveRecord::Migration
def self.up
rename_table('blog_post','posts')
rename_column(:posts, 'created','created_at')
rename_column(:posts,'modified','updated_at')
add_index(:posts, 'author_id')
end
def self.down
rename_column(:posts, 'created_at','created')
rename_column(:posts, 'updated_at','modified')
rename_table('posts', 'blog_post')
end
end
#Model
class Post < ActiveRecord::Base
belongs_to :user, :foreign_key => :author_id
validates_presence_of :subject, :text
end
#Controller
class PostController < ApplicationController
layout 'admin'
before_filter :login_required
active_scaffold :posts do |config|
list.columns = [:subject, :user, :tags, :created_at]
end
end
Ok, I’ve used some cheap tricks here, I’ve already done the authentication using acts as authenticated and that’s where the before_filter method comes from. I’ve also installed active scaffold to save me creating the admin CRUD pages(which only the band get to see anyway). One of the things that turned me on to django initially was it’s slick admin interface that comes baked in. Now that I’ve been working in rails for a little while I’ve come to appreciate the plugin architecture and I try to avoid coding unique solutions to common problems like admin CRUD.
I initially tried to keep the table as is and bend rails around it(in fact, the original title of this entry was ‘how to bend rails to play with a django database’). Our old django site won’t work against the migrated database (which is fine for me in this particular project). If you need to keep the database intact the files would something look like this, although this approach is not recommended and it is untested, I ran into issues using active scaffold without an updated_at column, I don’t even want to bother paddling up stream:
I ran into a few issues when using active_scaffold on a model looking something like this:
#Model
class Post < ActiveRecord::Base
set_table_name "blog_post"
before_create :run_before_create
before_save :run_before_save
belongs_to :user, :foreign_key => :author_id
validates_presence_of :subject, :text
def updated_at
return modified
end
def run_before_create
created = Time.now
end
def run_before_save
modified = Time.now
end
end
Thanks to Raaum’s rails reader for showing me some of the unconventional ActiveRecord manipulation, as well as encouraging me to stick to the ActiveRecord conventions.
until next time…
REST and BDD
One of the challenges facing any developer is keeping current with modern methodologies. This is especially so with Ruby on Rails, whose practitioners are probably more open-minded than most when it comes to adopting new ideas that work.
Two practices which have now entered the Rails mainstream are REST and BDD.
REST, which stands for Representational State Transfer, is the use of URLs to represent unique conceptual resources, which are accessible as physical representations through content type negotiation (from http://safari.oreilly.com/9780321501745). That might sound like a mouthful, but the net result is a simplification of how to interact with applications.
BDD is Behaviour Driven Development. BDD is a logical next step from TDD (Test Driven Development). BDD switches the paradigm from one of testing the code you’ve written (or are about to write), to a language of description and specification. Instead of writing tests, we write specifications. The benefit of this is firstly that specs are a more logical thing do be doing before you start coding, and secondly that the language used to describe a specification is far more readable and understandable than the language used in tests.
Unfortunately with both concepts being relatively new, there’s not a lot out there describing the practice of using these techniques. REST seems to get at most a chapter or two in the crop of current books. BDD hasn’t even made it to a printed book yet.
So these are the resources we’ve found to get ourselves up-to-speed…
BDD:
- http://dannorth.net/introducing-bdd
- http://behaviour-driven.org/
- http://video.google.com/videoplay?docid=8135690990081075324
- http://www.oreillynet.com/pub/a/ruby/2007/08/09/…
- http://peepcode.com/products/rspec-basics
REST:
- http://peepcode.com/products/restful-rails
- http://www.b-simple.de/documents
- http://safari.oreilly.com/9780321501745
I hope some of those are useful for anyone trying to learn this stuff.