Skip to content

Instantly share code, notes, and snippets.

require 'mime/types'
class AssetsController < ApplicationController
session :cookie_only => false, :only => :create
def create
@asset = Asset.new(params[:asset])
respond_to do |wants|
wants.html do
<% content_for :head do %>
<%= javascript_include_tag 'jquery.uploadify' %>
<% javascript_tag do %>
$(document).ready(function() {
$("#file_uploader").fileUpload({
'uploader': '/flash/uploader.swf',
'script': '<%= url %>',
'scriptData': { 'format': 'json', 'authenticity_token': encodeURIComponent('<%= form_authenticity_token if protect_against_forgery? %>'), '<%= Rails.configuration.action_controller.session[:session_key]%>': '<%= u session.session_id %>' },
'fileDataName': $('#file_uploader input:file')[0].name, // Extract correct name of upload field from form field
<%= render :partial => '/shared/upload_with_feedback', :locals => { :f => f,
:url => assets_path,
:dialog_file_description => 'Photos',
:allowed_extensions => [:jpg, :jpeg, :gif, :png],
:allow_multiple_files => true,
:max_size => 20.megabyte
} %>
def authenticate_or_request_admin
logged_in_as_admin || authenticate_or_request_with_http_basic { |username, password| login_as_admin(username, password) }
end
def logged_in_as_admin
session_enabled? && session[:is_admin?]
end
def login_as_admin(username, password)
authentic = username == ApplicationSettings::Administration::Authentication::USERNAME &&
module SiteExtension
def self.included(base)
base.class_eval {
extend ClassMethods
}
end
module ClassMethods
# We only have one site, and we want to use that whatever the url might be
task :deploy do
puts 'Deploying site to Heroku ...'
puts `git push heroku`
puts 'Running database migrations ...'
puts `heroku rake db:migrate`
release_name = "release-#{Time.now.utc.strftime("%Y%m%d%H%M%S")}"
puts "Tagging release as '#{release_name}'"
puts `git tag -a #{release_name} -m 'Tagged release'`
module BccAllMails
BCC_ALL_EMAILS_TO = "admin@example.com"
def self.included(base)
base.class_eval {
include InstanceMethods
alias_method_chain :deliver!, :bcc_all_mails
}
end
task :deploy => ['deploy:push', 'deploy:restart', 'deploy:tag']
namespace :deploy do
task :migrations => [:push, :off, :migrate, :restart, :on, :tag]
task :rollback => [:off, :push_previous, :restart, :on]
task :push do
puts 'Deploying site to Heroku ...'
puts `git push heroku`
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, [new_object], :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end
class ActionView::Helpers::FormBuilder
private
# PATCH: This is a patch of bug in Rails 3 beta 3, and should be removed when we move to Rails 3 final
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
options = args.extract_options!
association = args.shift
association = association.to_model if association.respond_to?(:to_model)