Skip to content

Instantly share code, notes, and snippets.

Store file uploads in the database

This gem allows to store carrierwave uploads in the database, eg. on platforms with ephemeral filesystems like Heroku.

Installation

# In: Gemfile
gem 'carrierwave-blob', git: "https://gist.github.com/f1dc8c75d67b92b23a55.git"
@karmi
karmi / .gitignore
Last active August 29, 2015 14:02
Install an Elasticsearch/Redis/Nginx/Rails/Ruby/Sinatra stack
.DS_Store
get '/:post_id' do
last_modification_of_post = Blog::Post[ params[:post_id], {:only => ['id', 'updated_on']} ]
last_modified( last_modification_of_post.updated_on ) if last_modification_of_post
@post = Blog::Post[ params[:post_id] ]
throw :halt, [404, erb(not_found) ] unless @post
erb :post
end
@karmi
karmi / gibberish.rake
Created August 5, 2008 13:47
Rake task to extract texts from Ruby/ERb source in your application for the Gibberish plugin dictionary
# Rake task to extract texts from Ruby/ERb source in your application
# Scans patterns like "Hello World"[:hello_world] and dumps them into RAILS_ROOT/lang/new_language.yml
# TODO: Process incrementally, ie. dump newly added strings into existing localization files
require 'fileutils'
namespace :localize do
desc "Extract all texts prepared to be translated from Ruby source"
task :extract do
count, keys, out = 0, [], "# Localization dictionary for the 'Gibberish' plugin (#{RAILS_ROOT.split('/').last})\n\n"
# IN app/controllers/application.rb
require 'uri'
before_filter :set_language
# ...
# Set language by hostname (.com => 'en', .cz => 'cz')
def set_language
module ClassMethods
# Declare in ActiveRecord or ActiveResource class with localized attributes
# You must include the module "manually" in your own classes (ie. put <tt>include HasLocalizedAttributes</tt> in your class)
# See README for example
def has_localized_atributes
include InstanceMethods unless included_modules.include?(InstanceMethods)
end
end
module InstanceMethods
def test_proper_localization_for_czech
falsify_today
hostname 'www.application.cz'
get :index
assert_tag :tag => 'h1', :content => 'Program Květen/Červen 2008'
assert_select '#topMenu a', :text => 'English'
assert_select '#headMenu strong', :text => 'Program'
assert_select '#program_item_2008-05-30 .day a', :text => 'pátek 30/05'
end
def test_should_not_list_unpublished_projects
get :index
assert_equal 13, assigns(:projects).size
assert assigns(:projects).delete_if { |p| p.published? }.empty?
end
def test_should_show_404_when_trying_to_get_not_published_project
get :show, :id => 14
assert assigns(:project)
assert_response :not_found
# Dump of table translations
# ------------------------------------------------------------
CREATE TABLE `translations` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_czech_ci default NULL,
`description` text collate utf8_czech_ci,
`lang` varchar(255) collate utf8_czech_ci default NULL,
`reference_id` int(11) default NULL,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
# artist.rb
class Artist < ActiveRecord::Base
has_many :works
has_many :compositions, :through => :works
end
# composition.rb
class Composition < ActiveRecord::Base
has_many :works
has_many :artists, :through => :works