Class: Nozzle::Adapter::Base
- Inherits:
-
Object
- Object
- Nozzle::Adapter::Base
- Includes:
- Outlet
- Defined in:
- lib/nozzle/adapter/base.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#access_path ⇒ Object
Returns intermediate path to the tempfile if the record is not yet saved and file is not yet stored at path.
-
#adapter_folder ⇒ Object
Returns folder name of the adapter relative to application public.
-
#adapter_path ⇒ Object
Returns filesystem folder path of the adapter relative to adapter root.
- #as_json(*args) ⇒ Object
-
#content_type ⇒ Object
Returns file content_type stored in avatar_content_type column of the record.
-
#default_url ⇒ Object
Returns nil.
-
#delete ⇒ Object
Sets adapter to delete stored file on #adapеr_after_save.
-
#dump(value) ⇒ Object
Fills internal structure of the adapter with new file’s path.
-
#filename ⇒ Object
Returns stored filename.
-
#initialize(record, column, filename, options = {}) ⇒ Base
constructor
Initializes internal structure of new adapter.
-
#load(value) ⇒ Object
Returns adapter instance.
-
#path ⇒ Object
Constructs a filesustem path which absolutely points to stored file.
-
#public_path ⇒ Object
Returns folder path relative to public folder.
-
#relative_folder ⇒ Object
Returns file’s folder relative to #adapter_path.
-
#root ⇒ Object
Returns root path of application’s static assets.
-
#settings ⇒ Object
Sets or gets settings provided by options.
-
#size ⇒ Object
Returns file size stored in avatar_size column of the record.
-
#store! ⇒ Object
Stores temporary filename by the constructed path.
-
#system_path ⇒ Object
Returns filesystem folder path relative to adapter root.
-
#to_s ⇒ Object
Inspects class name and url of the object.
-
#unlink!(target = path) ⇒ Object
Deletes file by path.
-
#url ⇒ Object
Constructs an URL which relatively points to the file.
-
#url? ⇒ Boolean
Gets url and adds a cache busting timestamp to it.
Methods included from Outlet
#cleanup!, included, #outlets, #prepare, #prepare!
Constructor Details
#initialize(record, column, filename, options = {}) ⇒ Base
Initializes internal structure of new adapter.
outlet_class.new( instance, :avatar, 'image.jpg', :fake => true )
12 13 14 15 16 17 18 |
# File 'lib/nozzle/adapter/base.rb', line 12 def initialize( record, column, filename, = {} ) @record = record @model = record.class @column = column.to_sym @filename = filename settings.merge! end |
Instance Method Details
#access_path ⇒ Object
Returns intermediate path to the tempfile if the record is not yet saved and file is not yet stored at path.
68 69 70 |
# File 'lib/nozzle/adapter/base.rb', line 68 def access_path @tempfile_path || path end |
#adapter_folder ⇒ Object
Returns folder name of the adapter relative to application public.
instance.avatar.adapter_folder # => 'uploads'
This MAY be overridden to specify where all the files should be stored.
107 108 109 |
# File 'lib/nozzle/adapter/base.rb', line 107 def adapter_folder 'uploads' end |
#adapter_path ⇒ Object
Returns filesystem folder path of the adapter relative to adapter root.
instance.avatar.adapter_path # => 'public/uploads'
It is constructed from #root, ‘public’ and #adapter_folder.
114 115 116 |
# File 'lib/nozzle/adapter/base.rb', line 114 def adapter_path File.join root, adapter_folder end |
#as_json(*args) ⇒ Object
202 203 204 |
# File 'lib/nozzle/adapter/base.rb', line 202 def as_json(*args) { :url => url } end |
#content_type ⇒ Object
Returns file content_type stored in avatar_content_type column of the record.
74 75 76 |
# File 'lib/nozzle/adapter/base.rb', line 74 def content_type @record.send( :"#{@column}_content_type" ) rescue '' end |
#default_url ⇒ Object
Returns nil. This SHOULD be overridden by subclasses of Nozzle::Adapter::Base.
instance.avatar.default_url # => nil
92 93 94 |
# File 'lib/nozzle/adapter/base.rb', line 92 def default_url nil end |
#delete ⇒ Object
Sets adapter to delete stored file on #adapеr_after_save.
instance.avatar.delete # => nil
148 149 150 |
# File 'lib/nozzle/adapter/base.rb', line 148 def delete @record.send(:"#{@column}=", nil) end |
#dump(value) ⇒ Object
Fills internal structure of the adapter with new file’s path. It’s used in Nozzle::Adapter#avatar= before sending filename to the object.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/nozzle/adapter/base.rb', line 161 def dump( value ) reset @original_path = path return nil unless value new_path = value raise Errno::ENOENT, "'#{new_path}'" unless File.exists?(new_path) @tempfile_path = File.(new_path) detect_properties @filename end |
#filename ⇒ Object
Returns stored filename.
instance.avatar.filename # => 'image.jpg'
85 86 87 |
# File 'lib/nozzle/adapter/base.rb', line 85 def filename @filename end |
#load(value) ⇒ Object
Returns adapter instance. It’s used in Nozzle::Adapter#avatar after retrieving filename from the object.
154 155 156 157 |
# File 'lib/nozzle/adapter/base.rb', line 154 def load( value ) @filename = value self end |
#path ⇒ Object
Constructs a filesustem path which absolutely points to stored file.
instance.avatar.path # => 'public/uploads/Model/avatar/image.jpg'
How it’s constructed:
"/#{system_path}/#{filename}"
"/#{adapter_path}/#{relative_folder}/#{filename}"
"#{root}/#{adapter_folder}/#{@model}/#{@column}/#{filename}"
"public/uploads/#{@model}/#{@column}/#{filename}"
"public/uploads/Model/avatar/image.jpg"
Note: if filename is not yet stored, nil is returned.
60 61 62 63 64 |
# File 'lib/nozzle/adapter/base.rb', line 60 def path File.join system_path, filename rescue TypeError nil end |
#public_path ⇒ Object
Returns folder path relative to public folder.
instance.avatar.public_path # => 'uploads/Model/avatar'
It is constructed from #adapter_folder and #relative_folder
136 137 138 |
# File 'lib/nozzle/adapter/base.rb', line 136 def public_path File.join adapter_folder, relative_folder end |
#relative_folder ⇒ Object
Returns file’s folder relative to #adapter_path.
instance.avatar.relative_folder # => 'Model/avatar'
It is constructed from object’s class name and column name. This MAY be overridden to place files somwhere other than ‘Model/avatar’.
122 123 124 |
# File 'lib/nozzle/adapter/base.rb', line 122 def relative_folder File.join @model.to_s, @column.to_s end |
#root ⇒ Object
Returns root path of application’s static assets.
instance.avatar.root # => 'public'
This MAY be overridden to return an application root different from the current folder.
100 101 102 |
# File 'lib/nozzle/adapter/base.rb', line 100 def root 'public' end |
#settings ⇒ Object
Sets or gets settings provided by options.
if instance.avatar.settings[:fake]
instance.avatar.settings[:fake] = false
end
24 25 26 |
# File 'lib/nozzle/adapter/base.rb', line 24 def settings @settings ||= {} end |
#size ⇒ Object
Returns file size stored in avatar_size column of the record.
79 80 81 |
# File 'lib/nozzle/adapter/base.rb', line 79 def size @record.send( :"#{@column}_size" ) rescue -1 end |
#store! ⇒ Object
Stores temporary filename by the constructed path. Deletes old file. Note: the file is moved if it’s path contains /tmp/ or /temp/, copied otherwise.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/nozzle/adapter/base.rb', line 177 def store! unlink! @original_path return nil unless @tempfile_path new_path = path FileUtils.mkdir_p File.dirname(new_path) result = if @tempfile_path =~ /\/te?mp\// FileUtils.move @tempfile_path, new_path else FileUtils.copy @tempfile_path, new_path end File.chmod 0644, new_path reset result end |
#system_path ⇒ Object
Returns filesystem folder path relative to adapter root.
instance.avatar.system_path # => 'public/uploads/Model/avatar'
It is constructed from #adapter_path and #relative_folder
129 130 131 |
# File 'lib/nozzle/adapter/base.rb', line 129 def system_path File.join adapter_path, relative_folder end |
#to_s ⇒ Object
Inspects class name and url of the object.
instance.avatar.to_s # => 'Model#url: /uploads/Model/avatar/image.jpg'
142 143 144 |
# File 'lib/nozzle/adapter/base.rb', line 142 def to_s "#{self.class}#url: #{url}" end |
#unlink!(target = path) ⇒ Object
Deletes file by path. Do not use, it will break adapter’s integrity. It’s called in #avatar_after_destroy after the object is destroyed.
unlink! # deletes path
unlink! @original_path # deletes @original_path
unlink! nil # deletes nothing
198 199 200 |
# File 'lib/nozzle/adapter/base.rb', line 198 def unlink!( target = path ) delete_file_and_folder! target if target end |
#url ⇒ Object
Constructs an URL which relatively points to the file.
instance.avatar.url # => '/uploads/Model/avatar/image.jpg'
How it’s constructed:
"#{public_path}/#{filename}"
"/#{adapter_folder}/#{relative_folder}/#{filename}"
"/uploads/#{@model}/#{@column}/#{filename}"
"/uploads/Model/avatar/image.jpg"
Note: if filename is not yet stored, default_url is called.
36 37 38 39 40 |
# File 'lib/nozzle/adapter/base.rb', line 36 def url File.join '', public_path, filename rescue TypeError default_url end |
#url? ⇒ Boolean
Gets url and adds a cache busting timestamp to it.
instance.avatar.url? # => '/uploads/Model/avatar/image.jpg?1373369401'
44 45 46 47 48 49 |
# File 'lib/nozzle/adapter/base.rb', line 44 def url? result = url result && "#{result}?#{File.mtime(path).to_i}" rescue Errno::ENOENT result end |