Getting Started with DRYer Rails "~>3.2"

Blog ยป Getting Started with DRYer Rails "~>3.2"

Posted on 07 Aug 2012 09:34

Following Edge Rails Guides gives you a demo blog. You can make a much DRYer version. This allows you quickly protype yet staying in the boundaries of standard "Rails". After creating new rails project Append this bit to the end of "config/application.rb":

#Reference: http://asciicasts.com/episodes/216-generators-in-rails-3
DemoBlog::Application.configure do
  config.generators do |g|
    g.scaffold_controller :inherited_resources_controller
    g.orm :hobo
  end
end

If prefer this way of appending custom generators instead of modifying the code block. So that I can use this in a simple script/template/builder.

And make requested gems available. Append to your "Gemfile":

gem 'inherited_resources'
gem 'has_scope'

git 'git://github.com/tablatom/hobo.git' do
  gem 'hobo_fields'
  #gem 'dryml'
  gem 'hobo_support'
  #gem 'hobo'
end

group :test do
  gem "shoulda"
  gem "factory_girl"
 #TODO: Introduce steak/capybara for integration test
end

As you see, I am able to use gems directly from github. (HoboFields for Rails 3.2 is only on GitHub at this date)

Now. Let's Finish It DRY!

Now you can scaffold a blog Post:

  rails g scaffold Post title:string text:text

And provide your DRY controller:

class PostsController < InheritedResources::Base
  respond_to :html, :json
end

Along with your DRYer model:

class Post < ActiveRecord::Base
  default_scope :order => 'created_at DESC'

  fields do
    title :string
    text  :text
    timestamps
  end

  validates :title, :presence => true,
                    :length => { :minimum => 5 }
end

This model allows automatic migratoins:

rails g hobo:migrations

Just rapid ruby on rails to the point with the help of 2 generators. Now you can focus your energy more to the user interface. You dont wanna code more, right?

To create the code mentioned here, you can use this template.

Moreover

This kind of application is ready to work on the cloud (Heroku). Just go and deploy your project to the world.

Rails contains a myriad of options. You should use RailsComposer to create new projects… And see more Rails examples

References

Note

I am not touching AJAX, Styling or whatever here intentionally. There are zillions of ways and preferences for that…

Another Note

Like all blog posts, this one will become out of date one day and it won't work any more. It would be nice if we could "unit test" and debug blog posts. Technically possible. However it would be nice if had a DSL that fits naturally to blogging. Does anybody have an inspiration?

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License