Skip to content

Instantly share code, notes, and snippets.

View tors's full-sized avatar
:octocat:

Tors tors

:octocat:
  • Vim
View GitHub Profile
# How to use Paperclip for generic attachment types on models in Ruby on Rails
class User < ActiveRecord::Base
has_many :user_assets
end
# example of building a model which is a generic attachment asset
@user.build_user_asset(:asset_type => "podcast", :attached_file => some_uploaded_file_data)
# make a generic asset class, with instances initialized for specific asset
development: &global_settings
database: textual_development
host: 127.0.0.1
port: 27017
test:
database: textual_test
<<: *global_settings
production:
class Chapter < ActiveRecord::Base
validates :subdomain,
:presence => true,
:uniqueness => true,
:length => { :maximum => 63 }
validates_format_of :subdomain, :with => /^[a-z0-9-]+$/i, :message => 'can only contain letters, numbers, and dashes'
validates_format_of :subdomain, :without => /^-/, :message => 'can not start with a dash'
validates_format_of :subdomain, :without => /-$/, :message => 'can not end with a dash'
validates_format_of :subdomain, :without => /--/, :message => 'can not contain two consecutive dashes'
end
# mongo_template.rb
# remove unneeded defaults
run "rm public/index.html"
run "rm public/images/rails.png"
run "rm public/javascripts/controls.js"
run "rm public/javascripts/dragdrop.js"
run "rm public/javascripts/effects.js"
run "rm public/javascripts/prototype.js"
# mongo_template.rb
# fork of Ben Scofield's Rails MongoMapper Template (http://gist.github.com/181842)
#
# To use:
# rails project_name -m http://gist.github.com/270200.txt
# remove unneeded defaults
run "rm public/index.html"
run "rm public/images/rails.png"
# mongo_template.rb
# fork of Ben Scofield's Rails MongoMapper Template (http://gist.github.com/181842)
#
# To use:
# rails project_name -m http://gist.github.com/gists/219223.txt
# remove unneeded defaults
run "rm public/index.html"
run "rm public/images/rails.png"
run "rm public/javascripts/controls.js"
#----------------------------------------------------------------------------
# Git Setup
#----------------------------------------------------------------------------
file '.gitignore', <<-FILE
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
public/uploads/*
class AddAttachmentsDataToWrapper < ActiveRecord::Migration
def self.up
add_column :wrappers, :data_file_name, :string
add_column :wrappers, :data_content_type, :string
add_column :wrappers, :data_file_size, :integer
add_column :wrappers, :data_updated_at, :datetime
end
def self.down
remove_column :wrappers, :data_file_name
@tors
tors / Fix
Created September 11, 2010 03:40
I created an Attachment class. This is a paperclip bug. Fix is to rename your Attachment class.
@tors
tors / gist:575235
Created September 11, 2010 14:36 — forked from mudge/gist:263139
# A polymorphic has_many :through relationship in Rails 2.3
# based on Josh Susser's "The other side of polymorphic :through associations"
# http://blog.hasmanythrough.com/2006/4/3/polymorphic-through
class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :publication, :polymorphic => true
end
class Author < ActiveRecord::Base
has_many :authorships