file_column ãã©ã°ã¤ã³å é¨æ§é
file_column ã®å é¨æ§é ã«ã¤ãã¦ã¡ã¢ã
Rails ã«ããããã¡ã¤ã«ã¢ãããã¼ãã®ä»çµã¿ã«ã¤ãã¦ã
ã¾ã file_column ã使ããã«ã©ããã£ã¦ãã¡ã¤ã«ãã¢ãããã¼ãããã®ãã¨ããã¨ããããå§ããã
Entry ã¢ãã«ã« image ã¨ãããã¡ã¤ã«ç¨ã®ã«ã©ã ããã£ãã¨ãããã¢ãããã¼ãç¨ã®ãã©ã¼ã ã¯æ¬¡ã®ãããªæãã
<% form_tag({:action => 'create'}, :multipart => true) do %> <%= file_field 'entry', 'image' %></p> <% end %>
file_field() 㯠ã¨ãã HTML ãçæãããã«ãã¡ã½ããã§ãããform_tag() 㧠:multipart => true ã¨æå®ããã¦ãããã¨ã«æ³¨æãããããªãã¨ã¢ãããã¼ãã§ããªãã
ã¢ãããã¼ãããããã¡ã¤ã«ãåãåãã³ã³ããã¼ã©å´ã§ã¯ã»ã»ã»
def create @entry = Entry.new(params[:entry]) logger.debug "EntriesController#create @entry.image.class.name, @entry.image = #{@entry.image.class.name}, #{@entry.image.inspect}" # => EntriesController#create @entry.image.class.name, @entry.image = Tempfile, #<File:/tmp/CGI.3002.1> if @entry.save flash[:notice] = 'Entry was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end
ã¨ããæãã Rails ãããããè³¢ããã£ã¦ããã¦ã@entry.image ã«ã¯ã¢ãããã¼ãããããã¡ã¤ã«ã表ç¾ãã Tempfile ãªãã¸ã§ã¯ããå ¥ã£ã¦ãããTempfile ãå®éã«ãããã¹ããã¹ã«ä¿åããã¨ããæä½ãèªåã§æ¸ããªãã¨ãããªããããã代ããã«ãã£ã¦ãããã®ããfile_column ãã©ã°ã¤ã³ã§ããã
ã¢ãã«ã« file_column
file_column ãã©ã°ã¤ã³ã®ä½¿ãæ¹ã®åºæ¬ã¯ãã¢ãã«ã§ file_column() ã使ããã¨ã ãã¾ãä¾ã«ãã£ã¦ Entry#image ã¨ããã«ã©ã ãèããã
class Entry < ActiveRecord::Base file_column :image end
ããã§ãimage ã«ã©ã ããã¡ã¤ã«æ ¼ç´ç¨ã®ã«ã©ã ã«æå®ãããã¨ã«ãªããã§ã¯ãã® file_column ã¯ãã£ããä½è ãï¼ $RAILSAPP/vendor/plugins/file_column/lib/file_column.rb ã« file_column ã¯å®ç¾©ããã¦ããã
# $RAILSAPP/vendor/plugins/file_column/lib/file_column.rb ã« file_column ããæç² def file_column(attr, options={}) state_attr = "@#{attr}_state".to_sym state_method = "#{attr}_state".to_sym define_method state_method do #(A) result = instance_variable_get state_attr if result.nil? result = FileColumn::create_state(self, attr.to_s) instance_variable_set state_attr, result end result end define_method "#{attr}=" do |file| #(B) state = send(state_method).assign(file) #(C) instance_variable_set state_attr, state #(D) end end
æ¬çã«é¢ä¿ãªãé¨åã¯å¤§å¹ ã«å²æãããéè¦ãªã®ã¯ãfile_column :image 㯠Entry#image=() ã¨ããã¡ã½ãããå®ç¾©ãã¦ãããã¨ã ã(B) ä»£å ¥ãèµ·ãã£ãç¬éã« file_column ã¯ãã¡ã¤ã«ä¿åãè¡ã£ã¦ããã®ã ã
ä¸ã®ã³ã¼ã㧠attr = "image" ã§ãããã¨ãèæ ®ããã¨ãä¸ã®ã³ã¼ãã¯æ¬¡ã®ãããªã¡ã½ãã群ãå®ç¾©ãã¦ãããã¨ã«ãªãã
class Entry < ActiveRecord::Base def image_state result = @image_state if result.nil? result = FileColumn::create_state(self, "image") @image_state = result end end def image=(file) state = image_state.assign(file) @image_state = state end end
Entry#image ã« Tempfile ãä»£å ¥ãããã¨ããã¨ããã¾ãã¯ä¸ã§å®ç¾©ããã image=() ãå¼ã°ãããå é¨ã§ image_state() ãå¼ã³åºããããã®æ»ãå¤ã«å¯¾ã㦠assign(file) ãå¼ã³åºããããimage_state() ã¯å é¨ã§ FileColumn::create_state(self, "image")ãå¼ã³åºãã¦ãããFileColumn::create_state() ãè¦ã¦ã¿ãã
# $RAILSAPP/vendor/plugins/file_column/lib/file_column.rb def self.create_state(instance,attr) filename = instance[attr] if filename.nil? or filename.empty? NoUploadedFile.new(instance,attr) else PermanentUploadedFile.new(instance,attr) end end
instance[attr] ã¯ãã®å ´åãentry["image"] ã§ãããentry["image"] ã«ã¾ã å¤ãå
¥ã£ã¦ããªãã¨ããã¨ãNoUploadedFile ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¨ã«ãªãã
å®ã¯ NoUploadedFile 以å¤ã«ããããã¤ã XXXUploadedFile ã¨å¼ã°ããã¯ã©ã¹ãå®ç¾©ããã¦ããã
å ·è±¡ã¯ã©ã¹ã¯ã¢ãããã¼ããã¡ã¤ã«ãå¦çããã¨åæã«ããã®ç¶æ ããããããªãã¸ã§ã¯ãã«ããªã£ã¦ããã
NoUploadedFile | ãã¡ã¤ã«ã®ãªãç¶æ |
TempUploadedFile | ã¢ãããã¼ãããããã®ãä¸æãã¡ã¤ã«ã¨ãã¦åå¨ããç¶æ |
PermanentUploadedFile | ã¢ãããã¼ãããããã®ãæ°¸ç¶ãã¡ã¤ã«ã¨ãã¦åå¨ããç¶æ |
ã表ãã¦ãããã¡ã½ããå¼ã³åºãã«ãã£ã¦ç¶æ ãé·ç§»ããã
ä¸ã§è¦ãããã« create_state() ã«ãã£ã¦åæç¶æ ãä½ããããåãã¦ãã¡ã¤ã«ãã¢ãããã¼ãããã¨ãã«ã¯ãNo(UploadedFile) ç¶æ ã¨ãªããimage=() ã® state = image_state.assign(file) ã§ã¯ã次㫠assign() ãå¼ã°ãããassign() ã¯ã½ã¼ã¹ãè¦ãã¨ããããå®è³ª upload() ã¨åçã§ãããããTemp(UploadedFile) ãªãã¸ã§ã¯ããè¿ã£ã¦ããï¼=Temp ç¶æ ã«é·ç§»ããï¼ãããã¦ãEntry ãªãã¸ã§ã¯ããä¿åãããã¨ããafter_save ã³ã¼ã«ããã¯ãå¼ã³åºããããã㧠Permanent(UploadedFile) ç¶æ ã¸ããã«é·ç§»ããããã®ã¨ããã¢ãããã¼ãããããã¡ã¤ã«ã¯ãä¸æãã©ã«ãããæ°¸ç¶ãã©ã«ãã¸ç§»åãããã(after_save ã³ã¼ã«ããã¯ã¨ TempUploadedFile#after_save ã¨ã®ç´ä»ã㯠FileColumn.file_column ã§è¡ããã¦ãããã½ã¼ã¹åç §ï¼
ã¾ã¨ã
ã½ã¼ã¹ã³ã¼ããéè¡è§£èª¬ããããã¨æã£ããããã¾ãã«é¢åãªã®ã§ããããã½ã¼ã¹ã³ã¼ããªãã¦èª°ãèªã¿ãããªãã¨æãããä¸ã®å³ã®æ¹ãéè¦ãè¦ããã«ç¶æ æ©æ¢°ã¨ãã¦ã¯ã©ã¹ç¾¤ãå®ç¾©ãã¦ããã®ããããã°ãã½ã¼ã¹ã¯ãªãã¨ãèªããã ããã