Skip to content

Commit

Permalink
Interpolate any method ofmodel into url and path
Browse files Browse the repository at this point in the history
  • Loading branch information
Adilson Chacon Cavalcanti committed Oct 1, 2011
1 parent e1406ca commit 7c16ab1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ If you are using the file system to store files, you can specify the directory/f

OBS: The default directory is ’/:rails_root/public/system/:attachment/:id/:style/:filename’


Also you can interpolate any method result of the model

class Foo
include MongoMapper::Document
plugin AttachIt
has_attachment :photo, { :url => '/:model.say_greet/users/:model.say_farewell/:id/:filename', :path => ':rails_root/public/:model.say_greet/:model.say_farewell/users/:id/:filename' }

def say_greet
'hello'
end

def say_farewell
'bye'
end
end

Where:
* :rails_root - is the root directory of your Rails application
* :environment - can be "production", "test" or "development"
Expand All @@ -80,6 +97,7 @@ Where:
* :style - if you specified the styles
* :filename - the name of the file
* :extension - the extension of the file
* :model.METHOD_NAME - where METHOD_NAME is any method of the model

=== View

Expand Down
18 changes: 18 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ If you are using the file system to store files, you can specify the directory/f

OBS: The default directory is ’/:rails_root/public/system/:attachment/:id/:style/:filename’


Also you can interpolate any method result of the model

class Foo
include MongoMapper::Document
plugin AttachIt
has_attachment :photo, { :url => '/:model.say_greet/users/:model.say_farewell/:id/:filename', :path => ':rails_root/public/:model.say_greet/:model.say_farewell/users/:id/:filename' }

def say_greet
'hello'
end

def say_farewell
'bye'
end
end

Where:
* :rails_root - is the root directory of your Rails application
* :environment - can be "production", "test" or "development"
Expand All @@ -80,6 +97,7 @@ Where:
* :style - if you specified the styles
* :filename - the name of the file
* :extension - the extension of the file
* :model.METHOD_NAME - where METHOD_NAME is any method of the model

=== View

Expand Down
6 changes: 4 additions & 2 deletions lib/attach_it/attach_it.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ def information_for(name = nil, options = nil)
end

def save_attachments
@attachment_options.keys.each do |name|
@attachment_options[name].save
unless @attachment_options.nil?
@attachment_options.keys.each do |name|
@attachment_options[name].save
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/attach_it/attachment_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def interpolate(source = nil, style_name = nil)
result.gsub!(/\:class/, @class_name)
result.gsub!(/\:attachment/, @attachment)
result.gsub!(/(\/){2,}/, '/')
while result.match(/:model\.([A-Za-z\_]+)/)
result.sub!(/:model\.[A-Za-z\_]+/, @model.send($1))
end
result
end

Expand Down
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ class UserTwelve
has_attachment :avatar, { :styles => { :small => '150x150>', :medium => '300x300>' }, :storage => 'gridfs' }
end

class UserThirteen
include MongoMapper::Document
plugin AttachIt
key :name, String
has_attachment :avatar, { :url => '/:model.say_greet/users/:model.say_farewell/:id/:filename', :path => ':rails_root/public/:model.say_greet/:model.say_farewell/users/:id/:filename' }

def say_greet
'hello'
end

def say_farewell
'bye'
end
end

class DocumentOne
include MongoMapper::Document
plugin AttachIt
Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_attach_it.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,20 @@ class TestAttachIt < Test::Unit::TestCase

end

context "Iterpolate model methods" do
setup do
@user = UserThirteen.new
@user.name = 'Myname'
@user.avatar = File.open(JpgImageFile, 'rb')
@user.save
end

should "show methods returns" do
assert_match('hello', @user.avatar.path)
assert_match('bye', @user.avatar.path)
assert_match('hello', @user.avatar.url)
assert_match('bye', @user.avatar.url)
end
end

end

0 comments on commit 7c16ab1

Please sign in to comment.