Created
March 30, 2016 21:08
-
-
Save cnk/e183143754fc982757c9ed3221216875 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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