Skip to content

Instantly share code, notes, and snippets.

@Agowan
Agowan / album_form.rb
Last active April 5, 2016 20:50
A simple way to use has_many between form objects using Virtus.The problem I had was to get validations working with fields_for in the view, but still have the flexibility and full control using virtus instead of active record models.And I added a way of checking for sanitised args in rails 4.
# encoding: utf-8
class AlbumForm < BaseForm
has_many :songs, class_name: 'SongForm'
validates :songs, form_collection: true
end
@whylom
whylom / sync.rake
Last active March 22, 2016 19:44
Rake task to copy assets between S3 buckets. Requires the aws-s3 gem (http://amazon.rubyforge.org/)
# add a new "put_copy" method to the Amazon client's S3Object class
# to enable copying an object from 1 bucket to another
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
class AWS::S3::S3Object
def self.put_copy(source_key, target_key, source_bucket, target_bucket, options = {})
original = open(url_for(source_key, source_bucket))
default_options = { :content_type => original.content_type }
store(target_key, original, target_bucket, default_options.merge(options))
acl(target_key, target_bucket, acl(source_key, source_bucket))
end
@plentz
plentz / nginx.conf
Last active July 2, 2024 13:20
Best nginx configuration for improved security(and performance)
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@j-mcnally
j-mcnally / gist:6206935
Last active December 20, 2015 22:39
Compile assets locally when working in low-memory environments
namespace :assets do
desc "compile assets locally and upload before finalize_update"
task :deploy do
%x[RAILS_ENV=production bundle exec rake assets:clean && RAILS_ENV=production bundle exec rake assets:precompile]
run "sudo rm -rf #{release_path}/public/assets"
ENV['COMMAND'] = " mkdir '#{release_path}/public/assets'"
invoke
path = File.expand_path("../../public/assets", __FILE__)
puts path
upload path, "#{release_path}/public/assets", {:recursive => true}
@j-mcnally
j-mcnally / gist:6084073
Created July 25, 2013 21:47
Change scss to sass
for F in $(find ./app/assets -name '*.scss'); do export FILENAME=${F##*/} && sass-convert $F ${F%$FILENAME}`basename "${F}" ".scss"`.sass && rm $F; done
@bensie
bensie / base.rb
Created December 6, 2012 17:53
Sinatra API Helpers
require "sinatra/base"
require "sinatra/namespace"
require "multi_json"
require "api/authentication"
require "api/error_handling"
require "api/pagination"
module Api
class Base < ::Sinatra::Base
@shmatov
shmatov / deploy.rake
Created November 15, 2012 13:17
rails + mina + unicorn
# lib/tasks/deploy.rake
namespace :deploy do
desc 'Deploy to staging environment'
task :staging do
exec 'mina deploy -f config/deploy/staging.rb'
end
end
@trey
trey / rails_bootstrap_delete_confirmation_modal.md
Created February 8, 2012 05:12
A nice delete confirmation modal in Rails courtesy of Bootstrap

Here's what you get.

Some JavaScript

// Delete confirmation modals
$('#delete-confirm').on('show', function() {
  var $submit = $(this).find('.btn-danger'),
      href = $submit.attr('href');
  $submit.attr('href', href.replace('pony', $(this).data('id')));
@uhlenbrock
uhlenbrock / deploy.rb
Created December 14, 2011 17:36
Precompile assets locally for Capistrano deploy
load 'deploy/assets'
namespace :deploy do
namespace :assets do
desc 'Run the precompile task locally and rsync with shared'
task :precompile, :roles => :web, :except => { :no_release => true } do
%x{bundle exec rake assets:precompile}
%x{rsync --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{host}:#{shared_path}}
%x{bundle exec rake assets:clean}
end