12d36dd93362054c38e8cb7e71f58802

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.

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 !

A8d3f35baafdaea851914b17dae9e1fc

Adam

May 19, 2009, May 19, 2009 17:19, permalink

1 rating. Login to rate!
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
D41d8cd98f00b204e9800998ecf8427e

mainej

May 21, 2009, May 21, 2009 02:11, permalink

No rating. Login to rate!
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
D41d8cd98f00b204e9800998ecf8427e

mainej

May 21, 2009, May 21, 2009 02:12, permalink

1 rating. Login to rate!

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
12d36dd93362054c38e8cb7e71f58802

ehayes.myopenid.com

June 7, 2009, June 07, 2009 06:09, permalink

No rating. Login to rate!

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.

4004fe18a4bcd5f43224779154856808

loofUnopy

February 17, 2011, February 17, 2011 20:54, permalink

No rating. Login to rate!

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>

78846c2dc00c63486d5fe4a1d32c4ed3

TictackGraics

December 18, 2011, December 18, 2011 17:04, permalink

No rating. Login to rate!

Here are some more links on the topic, programs for visceral fat loss

http://ufotoysonline.com/img/

Your refactoring





Format Copy from initial code

or Cancel