From 8bd5668d572aaea8181f32c84913bb757dfbd7fb Mon Sep 17 00:00:00 2001 From: Espen Antonsen Date: Wed, 3 Jun 2009 21:30:09 +0200 Subject: [PATCH] tagging works --- app/models/album.rb | 9 ++++--- app/models/photo.rb | 12 ++++----- app/models/tag.rb | 10 ++++++- app/views/albums/_form.html.erb | 8 +++++- app/views/albums/test/upload.html.erb | 32 ++++++++++++++++++++++ app/views/albums/upload.html.erb | 4 +-- app/views/photos/_form.html.erb | 2 +- public/javascripts/application.js | 39 +++------------------------ 8 files changed, 65 insertions(+), 51 deletions(-) diff --git a/app/models/album.rb b/app/models/album.rb index 9ab2ac5..30cd789 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -24,7 +24,8 @@ class Album < ActiveRecord::Base end def tag_list - # should maybe cache this to databse? + # should maybe cache this to database? + # should maybe return array instead? tags = Array.new self.photos.map{ |photo| @@ -49,7 +50,7 @@ class Album < ActiveRecord::Base def tag_list=(tags) return if tags.split(" ").sort.join(" ") == self.tag_list - current_tags = self.tag_list.split(" ") + current_tags = ( self.tag_list.nil? ? [] : self.tag_list.split(" ") ) tags = tags.split(" ") # find tags that should be removed from this album - thus remove from all photos in album @@ -81,7 +82,7 @@ class Album < ActiveRecord::Base def destroy_directory #puts "DELETE DIRECTORY " + APP_CONFIG[:photos_path] + self.path - #Dir.delete( APP_CONFIG[:photos_path] + self.path + "/" ) if File.exists?( APP_CONFIG[:photos_path] + self.path ) - #Dir.delete( APP_CONFIG[:thumbs_path] + self.path ) if File.exists?( APP_CONFIG[:thumbs_path] + self.path ) + Dir.delete( APP_CONFIG[:thumbs_path] + self.path ) if File.exists?( APP_CONFIG[:thumbs_path] + self.path ) + Dir.delete( APP_CONFIG[:photos_path] + self.path + "/" ) if File.exists?( APP_CONFIG[:photos_path] + self.path ) end end diff --git a/app/models/photo.rb b/app/models/photo.rb index 6bffd46..3a639b8 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -54,7 +54,7 @@ class Photo < ActiveRecord::Base def tag_list=(tags) ts = Array.new tags.split(" ").each do |tag| - ts.push( Tag.find_or_create_by_title( :title => tag) ) + ts.push( Tag.find_or_create_by_title( :title => tag.downcase) ) end self.tags = ts end @@ -118,7 +118,7 @@ class Photo < ActiveRecord::Base self.latitude = photo.GPSLatitude if self.latitude.nil? self.title = photo.DocumentName if self.title.nil? self.description = photo.ImageDescription if self.description.nil? - self.tag_list = photo.Keywords.map { |tag| tag.gsub(" ", "_") }.join(" ") if self.tags.empty? && !photo.Keywords.nil? + self.tag_list = (self.tags.empty? ? "" : self.album.tag_list) + " " + (photo.Keywords.map { |tag| tag.gsub(" ", "_") }.join(" ") if !photo.Keywords.nil?) end def exif_write @@ -130,12 +130,12 @@ class Photo < ActiveRecord::Base photo.Keywords = self.tags photo.save end - + def destroy_file #puts "DELETE THUMBS OF " + APP_CONFIG[:photos_path] + self.path - #File.delete( APP_CONFIG[:photos_path] + self.path ) if File.exists?( APP_CONFIG[:photos_path] + self.path ) - File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_thumb" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_small" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) - File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_album" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_small" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) + File.delete( APP_CONFIG[:photos_path] + self.path ) if File.exists?( APP_CONFIG[:photos_path] + self.path ) + File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_thumb" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_thumb" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) + File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_album" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_album" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_large" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_large" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) end end diff --git a/app/models/tag.rb b/app/models/tag.rb index c1a3044..9b719ed 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -3,7 +3,9 @@ class Tag < ActiveRecord::Base has_many :photos, :through => :photo_tags validates_uniqueness_of :title - + + before_validation :downcase_title + def self.tag_list return self.find(:all).map { |tag| tag.title }.join('\',\'') end @@ -15,4 +17,10 @@ class Tag < ActiveRecord::Base self.title end + protected + + def downcase_title + self.title.downcase! if attribute_present?("title") + end + end diff --git a/app/views/albums/_form.html.erb b/app/views/albums/_form.html.erb index 73150c0..726d75d 100644 --- a/app/views/albums/_form.html.erb +++ b/app/views/albums/_form.html.erb @@ -1,3 +1,9 @@ +<% content_for :javascript do %> + +<% end %> + +<%= hidden_field_tag :all_tags, "'#{Tag.find(:all).map { |tag| tag.title }.join('\',\'')}'" %> + <%= form.label :title, :Title, {:class => 'big'} %>
<%= form.text_field :title, {:class => 'big'} %>
<%= form.label :description %>
@@ -10,7 +16,7 @@ <%= form.text_area :note %>
<%= form.label :tag_list %>
-<%= form.text_field :tag_list, { :autocomplete => "off"} %>
+<%= form.text_field :tag_list, { :autocomplete => "off", :class => 'tag_list'} %>

<% if @album.path? %> diff --git a/app/views/albums/test/upload.html.erb b/app/views/albums/test/upload.html.erb index e0285b9..4c8de40 100644 --- a/app/views/albums/test/upload.html.erb +++ b/app/views/albums/test/upload.html.erb @@ -1,3 +1,35 @@ +if ( $('ul.gallery').length ) { + $('ul.gallery').galleria( { + clickNext : true, + insert: "#photo_large", + onImage: function ( image, caption, thumb ) { + image.css('display','none').fadeIn() + + thumb.parents('li').siblings().children('img.selected').fadeTo(500,0.3) + thumb.fadeTo('fast',1).addClass('selected') + $( '#photo_metadata' ).html( 'Update photo details' ) + + var scrollable = $("#thumbstrip").scrollable() + scrollable.seekTo( thumb.parents('ul').children().index( thumb.parents('li') ) ) + }, + onThumb: function ( thumb) { + thumb.css({display:'none',opacity: (thumb.parents('li').is('.active') ? '1' : '0.3') }).fadeIn(1500) + } + }) + } + + if ( $('#thumbstrip').length ) { + $('#thumbstrip').scrollable( { + items : '#thumbs', + clickable: true, + keyboard : false + }) + if ( $('#thumbs li.active').length == 0 ){ + //$('div.scrollable').scrollable().click(0) + $('#thumbs li:first').addClass('active') + } + } + <% content_for :javascript do %> <% end %> diff --git a/app/views/albums/upload.html.erb b/app/views/albums/upload.html.erb index 6f6fbee..18208e6 100644 --- a/app/views/albums/upload.html.erb +++ b/app/views/albums/upload.html.erb @@ -7,8 +7,8 @@ $(document).ready(function() { uploader:'/javascripts/jquery.uploadify-v1.6.2.mit/uploader.swf', script:'<%= photos_path %>', scriptData: { - '<%= get_session_key %>' : '<%= u cookies[get_session_key] %>', - '<%= request_forgery_protection_token %>' : '<%= u form_authenticity_token %>', + '<%= get_session_key %>' : encodeURIComponent('<%= u cookies[get_session_key] %>'), + '<%= request_forgery_protection_token %>' : encodeURIComponent('<%= u form_authenticity_token %>'), 'photo[album_id]' : "<%= @album.id %>" }, cancelImg:'/javascripts/jquery.uploadify-v1.6.2.mit/cancel.png', diff --git a/app/views/photos/_form.html.erb b/app/views/photos/_form.html.erb index 9288519..31d4b7f 100644 --- a/app/views/photos/_form.html.erb +++ b/app/views/photos/_form.html.erb @@ -6,6 +6,6 @@ <%= form.text_field :title %>

<%= form.label :tag_list %>
-<%= form.text_field :tag_list, { :autocomplete => "off"} %>
+<%= form.text_field :tag_list, { :autocomplete => "off", :class => 'tag_list'} %>

On disk: ~/<%= @photo.path %>

\ No newline at end of file diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 81cee80..bb923df 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1,43 +1,10 @@ jQuery(function($) { - if ( $('ul.gallery').length ) { - $('ul.gallery').galleria( { - clickNext : true, - insert: "#photo_large", - onImage: function ( image, caption, thumb ) { - image.css('display','none').fadeIn() - - thumb.parents('li').siblings().children('img.selected').fadeTo(500,0.3) - thumb.fadeTo('fast',1).addClass('selected') - $( '#photo_metadata' ).html( 'Update photo details' ) - - var scrollable = $("#thumbstrip").scrollable() - scrollable.seekTo( thumb.parents('ul').children().index( thumb.parents('li') ) ) - }, - onThumb: function ( thumb) { - thumb.css({display:'none',opacity: (thumb.parents('li').is('.active') ? '1' : '0.3') }).fadeIn(1500) - } - }) - } - - if ( $('#thumbstrip').length ) { - $('#thumbstrip').scrollable( { - items : '#thumbs', - clickable: true, - keyboard : false - }) - if ( $('#thumbs li.active').length == 0 ){ - //$('div.scrollable').scrollable().click(0) - $('#thumbs li:first').addClass('active') - } - } - if ( $('#photo_tag_list').length ) { - $('#photo_tag_list').tagSuggest({ + + if ( $('.tag_list').length ) { + $('.tag_list').tagSuggest({ tags: $('#all_tags').val().split('\'') }) } - - if ( $('FORM #upload').length ) { - } //$('div.scrollable').scrollable().click( $('#gallery ul').children().index( $('#gallery li.active') ) ) }); \ No newline at end of file