Skip to content

Instantly share code, notes, and snippets.

@cnk
Created March 30, 2016 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnk/e183143754fc982757c9ed3221216875 to your computer and use it in GitHub Desktop.
Save cnk/e183143754fc982757c9ed3221216875 to your computer and use it in GitHub Desktop.
require 'RMagick'
class BackgroundImage < ActiveRecord::Base
attr_accessible :parent_id, :uploaded_data, :filename
has_one :child, :class_name => "BackgroundImage", :foreign_key => :parent_id
# You may want to pass other options to has_attachment.
# See the attachment_fu README.
has_attachment :content_type => :image,
:min_size => 1.kilobyte,
:max_size => 4.megabyte,
:storage => :db_file,
:resize_to => '1600x',
# Process thumbnail to:
:thumbnails => {:thumbnail => '400x'}
validates_as_attachment
# Load seeds - in here so I can call it from db:seeds and capistrano
def self.load_seed_data
fileglob = "#{RAILS_ROOT}/public/backgrounds/*.jpg"
Dir.glob(fileglob).each {|file|
if self.find_by_filename(file.split("/")[-1])
# Do nothing
else
@bg_image = self.new(:uploaded_data => ActionController::TestUploadedFile.new(file, "image/jpeg"))
@bg_image.save
end
}
end
end
namespace :empcms do
desc "Load background images from the file system into the admin interface - and then set one as the current backgrou\
nd"
task :seed_background_images => :environment do
# load new files
BackgroundImage.load_seed_data
# Pick a random image for this site's background
if SitePreference.instance.background_image_path.blank?
background_images = BackgroundImage.originals
bg_image_path = background_images[Kernel.rand(background_images.length)].public_filename
SitePreference.instance.update_attributes(:background_image_path => bg_image_path)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment