This blog is now closed. I have moved my blog to www.bendangelo.me
So I want my web app on heroku to create a file in ruby and save it using paperclip. The issue with this is heroku doesn’t allow file saves anywhere except the /tmp folder. So I have to use the Tempfile.open command instead of the File.open command.
This works fine except the Tempfile will append a random script to make it unique. This is annoying and looks something like this.
ach_1.png20111229-3340-ajrqxf
In order to fix this and save it again as its normal file name. I had to use a paperclip interpolation like so.
Paperclip.interpolates :image_name do |attachment, style|
attachment.instance.data_file_name = attachment.instance.data_file_name.match(/^\w+.(png)/i)[0] rescue ‘untitled.png’
end
Put this inside your model and add this to the path/url
:path=>”:rails_root/public/system/attachments/#{Rails.env}/:image_name”
Problem solved. Just make sure you remove the old broken tempfile or they will conflict when these changes are made!
I was just using paperclip and found that all my files were being saved in the Root.root directory and not in the directory I wanted.
After some investigation I found that
model.file = File.open(‘new.js’, ‘w+’){|f| write.(‘hey’); }
model.save
Would save the file to the directory and would end up missing. I simply switched this to
file = File.new(‘new.js’, ‘r+’)
file.print(‘hey’)
model.file = file
model.save
And all was well again.
I just had an issue using the image preprocessor in paperclip. Firstly I didn’t have imagemagick installed, so I installed it and that fixed the “sh: identify not found” error.
After that the preprocessor wasn’t working. The images weren’t being turned into thumbnails after upload. I figured this was a windows issue and installed cygwin. This fixed all the issues. Cygwin is a linux polyfill for windows and will fix a lot of gem issues.
If you’re on windows and are a ruby on rails developer. I recommend installing cygwin right away.
There is an issue in kaminari where pagiating on an embedded document will not display the paginate html. This is because the num_pages value is always set to 1. I came up with a quick fix for anyone having the same issue.
Put this inside a helper…
def paginate_comments(scope, total, options={})
pages = (@game.comments.count.to_f / @comments.limit_value.to_f).ceil
paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
paginator.to_s
end
:)
I just started to play around with jasmine and it couldn’t find any JS/Coffee files in my app! This was really annoying. Anyway I came up with a solution, its down below.
src_files: - "vendor/**/*.{js,coffee}" - "lib/**/*.{js,coffee}" - "app/**/*.{js,coffee}" stylesheets: - "vendor/**/*.css" - "lib/**/*.css" - "app/**/*.css" helpers: - "helpers/**/*.{js,coffee}" spec_files: - "**/*[Ss]pec.{js,coffee}" src_dir: spec_dir: spec/javascripts
Came from here: https://github.com/searls/jasmine-rails
So I wanted to generate random but valid data for testing out my rails application in the web browser. I found a really cool and easy to use gem called RandomData.
I figured the easiest way was to create a rake task like “app:test_seed” and run that to generate random data in the database. I also figured it would be best to use factorygirl to generate the valid models for me.
So first install the random_data gem. In your Gemfile add. Remember to run bundle install.
gem ‘random_data’
Now in your factories.rb include the random_data.rb
require ‘random_data’
Here an example user factory. Emails should be unique in my app so I used a sequence to always have a unique email. I also included factory_girl for my rake task
require ‘factory_girl’
require ‘random_data’
FactoryGirl.define do
factory :user do
first_name Random.firstname
sequence(:email) {|n| “#{n}_#{Random.email}”}
end
end
Now create a new rake in lib/tasks called app.rake. This will generate 100 unique users upon execution.
namespace :app do
task :test_seed => :environment do
require File.dirname(__FILE__) + ‘/../../spec/factories’
puts ‘seeding users’100.times do
Factory :user
print ‘.’
end
end
end
Run rake app:test_seed and it should fill your database with test users.
Enjoy!
I been looking around lately for a good rails windows editor. I looked at a couple but I settled on Redcar. Its just like textmate on Mac and even has all the same shortcuts. I recommend you check it out if you need a rails editor on windows.
So I been testing with rspec lately and I have always had this annoying deprecation warning message pop up every other test. Today I had enough and finally solved the issue.
It seems to be the Mongiod Token gem that is causing the ruckus. A simple fix is to pop this into your spec_helper.rb
ActiveSupport::Deprecation.silenced = true
That will disable warning messages in tests.
I just setup amazon s3 and a bucket named pixel.throwthegame.com. Now I want all assets showing up from paperclip to display from this domain, not from s3.amazon.com. It took me a while to figure this out.
Simply add..
:path=>”/:id_partition/:style/:basename.:extension”,
:url=>”:s3_alias_url”,
:s3_host_alias=>”pixel.throwthegame.com”
And thats it. As a bonus :id_partition automatically sorts your directories into a spread out manner for large amounts of files.
One more tip, to easily switch between s3 and local development you can use a simple class method here. http://stackoverflow.com/questions/2562249/how-can-i-set-paperclips-storage-mechanism-based-on-the-current-rails-environme