desc "Load files from upload directory"
task :load_galleries => :environment do
dir = ENV["UPLOAD_DIR"]
event = Event.find(ENV["EVENT_ID"].to_i)
Dir.chdir(RAILS_ROOT + '/uploads/' + dir)
Dir.glob('**').each do |d|
Dir.chdir(d)
g = event.galleries.create(:title => d.titleize)
Dir.glob('*.jpg').each_with_index do |p, i|
g.prints.create(:photo => File.open(p), :title => "#{g.title} ##{i + 1}" )
end
g.photo = g.prints.first.photo
g.save
Dir.chdir('..')
end
end
Refactorings
No refactoring yet !
Adam
May 19, 2009, May 19, 2009 17:19, permalink
desc "Load files from upload directory"
task :load_galleries => :environment do
Dir.glob(File.join(Rails.root, 'uploads', ENV['UPLOAD_DIR'], '*.jpg')).each_with_index do |image_path,index|
File.open(image_path) do |file|
gallery = Event.find(ENV['EVENT_ID']).galleries.find_or_create_by_title(File.basename(File.dirname(image_path)).titleize)
gallery.prints.create(:photo => file, :title => "#{gallery.title} ##{index.succ}")
end
end
end
mainej
May 21, 2009, May 21, 2009 02:11, permalink
desc "Load files from upload directory" task :load_galleries => :environment do Event.find(ENV["EVENT_ID"]).load_galleries_from(ENV["UPLOAD_DIR"]) end
class Event < ActiveRecord::Base
has_many :galleries
def load_galleries_from(folder)
Dir.glob(RAILS_ROOT + '/uploads/' + folder + '/**').each do |subfolder|
galleries.create_from_folder(subfolder)
end
end
end
class Gallery < ActiveRecord::Base
belongs_to :event
has_many :prints
has_attached_file :photo
def self.create_from_folder(folder)
gallery = create(:title => folder.titleize)
gallery.create_prints_of_jpgs_in(folder)
gallery.photo = prints.first.photo
gallery.save
gallery
end
def create_prints_of_jpgs_in(path)
Dir.glob(path + '/*.jpg').each_with_index do |photo, i|
prints.create(:photo => File.open(photo), :title => "#{title} ##{i + 1}" )
end
end
end
class Print < ActiveRecord::Base belongs_to :gallery has_attached_file :photo
desc "Load files from upload directory" task :load_galleries => :environment do Event.find(ENV["EVENT_ID"]).load_galleries_from(ENV["UPLOAD_DIR"]) end
class Event < ActiveRecord::Base
has_many :galleries
def load_galleries_from(folder)
Dir.glob(RAILS_ROOT + '/uploads/' + folder + '/**').each do |subfolder|
galleries.create_from_folder(subfolder)
end
end
end
class Gallery < ActiveRecord::Base
belongs_to :event
has_many :prints
has_attached_file :photo
def self.create_from_folder(folder)
gallery = create(:title => folder.titleize)
gallery.create_prints_of_jpgs_in(folder)
gallery.photo = prints.first.photo
gallery.save
gallery
end
def create_prints_of_jpgs_in(path)
Dir.glob(path + '/*.jpg').each_with_index do |photo, i|
prints.create(:photo => File.open(photo), :title => "#{title} ##{i + 1}" )
end
end
end
class Print < ActiveRecord::Base belongs_to :gallery has_attached_file :photo end
mainej
May 21, 2009, May 21, 2009 02:12, permalink
Less noise
desc "Load files from upload directory" task :load_galleries => :environment do Event.find(ENV["EVENT_ID"]).load_galleries_from(ENV["UPLOAD_DIR"]) end
class Event < ActiveRecord::Base
has_many :galleries
def load_galleries_from(folder)
Dir.glob(RAILS_ROOT + '/uploads/' + folder + '/**').each do |subfolder|
galleries.create_from_folder(subfolder)
end
end
end
class Gallery < ActiveRecord::Base
belongs_to :event
has_many :prints
has_attached_file :photo
def self.create_from_folder(folder)
gallery = create(:title => folder.titleize)
gallery.create_prints_of_jpgs_in(folder)
gallery.photo = prints.first.photo
gallery.save
gallery
end
def create_prints_of_jpgs_in(path)
Dir.glob(path + '/*.jpg').each_with_index do |photo, i|
prints.create(:photo => File.open(photo), :title => "#{title} ##{i + 1}" )
end
end
end
class Print < ActiveRecord::Base belongs_to :gallery has_attached_file :photo end
ehayes.myopenid.com
June 7, 2009, June 07, 2009 06:09, permalink
Thanks! I like Adam's because it really cleans up what I've got. But, now that I look at mainej's, I'm wondering if I should move more functionality into the models.
loofUnopy
February 17, 2011, February 17, 2011 20:54, permalink
Sometimes pain can be made worse by social or work-related events that cause you stress – loss of a job. That's what i want to say here.
___________________________________
<a href=http://cecs02.cecs.uci.edu/forum/index.php?action=profile;u=9968>profile</a>
TictackGraics
December 18, 2011, December 18, 2011 17:04, permalink
Here are some more links on the topic, programs for visceral fat loss
http://ufotoysonline.com/img/
I'm using the following Rake task to traverse a directory tree and load images into a custom Rails photo gallery. Models are thus: Events have many Galleries which have many Prints (individual images). The admin user would create an Event, then choose which folder within the "uploads" folder to load images from. For each sub-folder in that folder, we create a new gallery and create a new print for each jpg in that sub-folder. These jpgs are attached to the new Print with Paperclip. After Prints are added to the new Gallery, we make the first Print's photo the main Gallery image and save it. Finally, we back up a directory and start on a new Gallery.