(5,393 views)
Vaporbase ported to Rails 3 and Heroku
It's been a while since I posted here, not because I haven't been busy. But rather I didn't feel like I had alot to say, or at least I haven't been motivated to share what I've been learning.
But more about that later, I'll write a separate post about "what i did over my summer vacation".
But I spent time the past couple weeks porting my Vaporbase blog from my legacy php-based Xaraya-CMS framework to Rails 3 and hosted on Heroku (for free).
Here's some notes about the process.
Rails Project Setup
I decided the new site would use haml and compass and blueprint for html and css templating. Using simple_form for semantic forms. And authlogic for authentication. I also started with the nifty-generators, because I like them.
Roughly (not exactly) the setup commands boiled down to:
$ rails new vapor
$ cd vapor
edit the Gemfile
$ bundle install
$ rake db:create
$ rm public/index.html
$ rails g formtastic:install
$ compass init rails . --using blueprint/semantic
$ rails g nifty:layout --haml
$ rails g nifty:authentication --haml --authlogic
Create an initial admin user, add to seeds.rb
User.create(:username => "admin", :password => "secret", :password_confirmation => "secret", :email => "me@gmail.com")
Add these lines to the head of your layouts/application.html.haml:
%head
= stylesheet_link_tag 'compiled/screen.scss', :media => 'screen, projection'
= stylesheet_link_tag 'compiled/print.scss', :media => 'print'
= stylesheet_link_tag 'compiled/ie.scss', :media => 'screen, projection'
Setup grid in app/stylesheets/partials/_base.scss, remove scaffold from _page.scss, and in _page.scss remove body.bp, put includes at top level.
In routes.rb
root :to => "posts#index"
Import Posting from Old CMS
Boy this was fun. Digging back into php code, and digging back into where it was buried in the back of my mind. I wrote an export_postings.php and export_comments.php scripts which used the Xaraya api to pull the data (including dynamic data, categories etc) and output it as CSV files. If you want to see the code for this, write me an email and beg gently :)
Then, create the Posting model in the Rails app
$ rails g nifty:scaffold posting title:string summary:text status:string body:text counter:integer user_id:integer --haml --rspec
$ rake db:migrate
wrote a Posting#import method and added it to a rake task
$ rake vapor:import
for categories,
$ rails g nifty:scaffold category name:string --haml
setup for habtm in models, and
in migration
create_table :categories_posts, :id => false do |t|
t.integer :category_id
t.integer :post_id
end
Next steps in rough notes:
pagination
ref: http://github.com/mislav/will_paginate/wiki
Gemfile
gem "will_paginate", "~> 3.0.pre2"
posting.rb
cattr_reader :per_page
@@per_page = 10
controller
@postings = Posting.paginate :page => params[:page], :order => 'created_at DESC'
view
= will_paginate @postings
status
add published scope to posting model, and use it in controller
category filter
add :category param to controller, and pass it in link_to in aside nav
preserve xar urls
change the export_postings.php to generate the xar url for us
Gemfile stringex
add a url column to posting
posting acts_as_url (wont overwrite imported urls but will generate new ones), and def to_param
controller uses find_by_url
import needs to remove dots from url
show posting view
html5 strcture
body_id for different banner image from frontpage
ref http://www.themomorohoax.com/2009/02/21/on-css-5-tips-for-rails-developer-sanity
authentication
gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'
logged_in filters, views
views counter (non-admin)
@posting.increment!(:counter) unless logged_in?
exception handling
ref http://guides.rubyonrails.org/action_controller_overview.html#the-default-500-and-404-templates
be sure all finds use ! to raise exceptions (Posting.find_by_url!)
create/edit postings
tinymce
http://github.com/kete/tiny_mce
Gemfile
gem 'tiny_mce'
postings_controller
uses_tiny_mce
form body field
:class => "mceEditor"
application layout
= include_tiny_mce_if_needed
simple_form
Gemfile
gem 'simple_form'
styles
rails generate simple_form:install
minor fixes to _form.html.haml
about
contact
$ rails g mailer notifier
config/application.rb
...
$ rails g resource contact_form
rm the migration
in mailer/notifier.rb
def contact_form(sender)...
also can set defaults
create views (contact_form.html.haml, contact_form.text.haml)
in routes.rb
resources :contact_forms, :only => [:new, :create]
in contact_forms_controller.rb
def new
def create (calls @contact_form.save)
in models/contact_form.rb
validations...
def initialize...
def read_attribute_for_validation...
def persisted?...
# convenience accessors
def to
def from
# use save to send, so the validations get called etc
def save
if valid?
Notifier.contact_form(self).deliver
else
return false
end
end
debugger
gem "ruby-debug"
$ rails s --debugger
comments
export comments from xar vaporbase
$ rails g nifty:scaffold comment title:string body:text author:string status:string anonymous:boolean post_id:integer --haml
add model associations
import
rake
model function
routes.rb
show views
user create comments
admin comments
delete
edit
markdown
Gemfile
gem 'rdiscount'
application_helper.rb
def sanitized_markdown(content)
RDiscount.new(h content).to_html.html_safe
end
comments/_comment.html.haml
...
= sanitized_markdown comment.body
search
add params[:search] option in postings_controller
and search form on sidebar
git
$ git init
$ git add .
$ git commit -m "first commit"
deploy
signup heroku
$ gem install heroku
$ heroku keys:add
$ heroku create
$ git push heroku master
build the database
$ heroku rake db:migrate
$ heroku rake vapor:import
$ heroku rake vapor:comments
s3 account
sign up
https://console.aws.amazon.com/s3/home
create a bucket: vaporbase
upload files
write uploads_on_s3 helper to change links in content (could be a model filter but decided to use a helper)
def uploads_on_s3(content)
content.gsub('https://s3.amazonaws.com/vaporbase/uploads', 'https://s3.amazonaws.com/vaporbase/uploads').gsub('https://s3.amazonaws.com/vaporbase/uploads', 'https://s3.amazonaws.com/vaporbase/uploads')
end
routes.rb
match "/postingshttps://s3.amazonaws.com/vaporbase/uploads/:dir/:file.:ext" => redirect("https://s3.amazonaws.com/vaporbase/uploads/%{dir}/%{file}.%{ext}")
custom domain on heroku
add custom domain addon
change dns records per heroku docs
www CNAME proxy.heroku.com. 1 hour
@ A 75.101.163.44
@ A 75.101.145.87
@ A 174.129.212.2
@ MX verio.vaporbase.com.
$ heroku domains:add www.vaporbase.com
check it:
$ host www.vaporbase.com
TaaDaa!!
Comments
New Comment