paperclip processors and after_post_process

A few things to note when trying to process an uploaded file with paperclip:

  • The only way a post-processor will run is if a style is defined

class MyModel < ActiveRecord::Base

has_attached_file :mp3, :url => '/:class/:id/:style.:extension', :path => ':rails_root/assets/:class/:id_partition/:style.:extension', :whiny => true, :styles => { :text => { :do_something => true } }, :processors => [:my_processor]

# ...

end

  • To access the file after processing but before paperclip is finished, use the :after_process callback -- model instance attributes can be modified and will be saved automatically as part of the paperclip process

class MyModel < ActiveRecord::Base

# ...

after_post_process :do_one_more_thing

# ...

private

def do_one_more_thing

logger.info "MyModel [paperclip] after_post_process"

logger.info "MyModel [paperclip] file #{mp3.queued_for_write[:original].path}"

# Do more with the file

# Make model attribute changes as needed

# self.my_attribute = "x"

end

end

rails log rotation

No need to restart by following this post: http://www.hostingrails.com/401/What-is-the-best-practice-for-rails-log-rotat... #!/bin/sh mkdir /mnt/app/releases/mysite.com/log/archive -p tar czf "/mnt/app/releases/mysite.com/log/archive/`date +%Y%m%d`_production.tar.gz" -C / mnt/app/releases/mysite.com/log/production.log cat /dev/null > /mnt/app/releases/mysite.com/log/production.log find /mnt/app/releases/mysite.com/log/archive/* -mtime +180 -exec rm {} \;

Happy with...

Nothing earth-shattering in this post...just wanted to note my good experiences with:
  • formtastic: I had looked into the air_budd_form_builder and RubyPond's semantic_form_builder but moved on to formtastic after liking its concise approach to building forms.
  • suspenders: Not too heavy for a Rails starter app with lots of nice development plugins from the thoughtbot folks.
  • paperclip: Really easy file attachments for ActiveRecord.

Ruby on Rails Plugin-based Creator and Updater Model Relationships

Leonhart was nice enough to post about how easy it is to create a Ruby on Rails plugin. The post described how to create a plugin to help with user creator and updater relationships. One of my colleagues decided to try it. A few days later, he noticed issues with RSpec tests that were doing simple things like user.projects and sent me an email to see if I could spot anything. After spending some time this morning wondering why the RSpec tests were acting really strangely, I tracked down the issue to this section of code in a lib module: [sourcecode="ruby"] belongs_to :creator, :foreign_key => "created_by", :class_name => "User" belongs_to :updator, :foreign_key => "updated_by", :class_name => "User" User.class_eval "has_many: #{self.to_s.tableize}, :foreign_key => :created_by" [/sourcecode]One quick note before you use this in the future: Make sure you have Creator and Updater models/classes that extend User, and use them to establish the relationship with the target model. Otherwise, you'll be wondering why method calls like user.projects returns the projects that a user created instead of the user's projects! [sourcecode="ruby"] belongs_to :creator, :foreign_key => "created_by" belongs_to :updater, :foreign_key => "updated_by" Creator.class_eval "has_many: #{self.to_s.tableize}, :foreign_key => :created_by" Updater.class_eval "has_many: #{self.to_s.tableize}, :foreign_key => :updated_by" [/sourcecode]