attached 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,6 +5,7 @@ Attached is a Ruby on Rails file attachment tool that lets users upload to the c
5
5
  == Installation
6
6
 
7
7
  gem install attached
8
+ gem install rmagick
8
9
 
9
10
  == Examples
10
11
 
@@ -43,7 +44,6 @@ Form:
43
44
 
44
45
  <%= form_for @video, :html => { :multipart => true } do |form| %>
45
46
  <%= form.file_field :encoding %>
46
- <p class="errors"><%= @video.errors[:encoding] %></p>
47
47
  <% end %>
48
48
 
49
49
  View:
@@ -57,12 +57,7 @@ View:
57
57
 
58
58
  === Storage
59
59
 
60
- has_attached :avatar, :provider => :amazon, :credentials => {
61
- :secret_access_key => "*****",
62
- :access_key_id => "*****",
63
- }
64
-
65
- has_attached :avatar, :provider => :google, :credentials => {
60
+ has_attached :avatar, :provider => :aws, :credentials => {
66
61
  :secret_access_key => "*****",
67
62
  :access_key_id => "*****",
68
63
  }
@@ -74,10 +69,14 @@ View:
74
69
  :large => { :size => "400x400#" }
75
70
  }
76
71
 
77
- has_attached :avatar, :processors => [:image], :styles => {
78
- :small => { :size => "200x200#" }
79
- :large => { :size => "400x400#" }
80
- }
72
+ === Alias
73
+
74
+ Attached::Attachment.options[:aliases] = %w(
75
+ d1kf5um3mxa8kv.cloudfront.net
76
+ d3d5wf186mp1rv.cloudfront.net
77
+ d2wmfkw6rydl1g.cloudfront.net
78
+ d1gqpdc40l7s16.cloudfront.net
79
+ )
81
80
 
82
81
  == Copyright
83
82
 
@@ -12,7 +12,6 @@ module Attached
12
12
  attr_reader :file
13
13
  attr_reader :name
14
14
  attr_reader :instance
15
- attr_reader :options
16
15
  attr_reader :queue
17
16
  attr_reader :path
18
17
  attr_reader :styles
@@ -21,6 +20,10 @@ module Attached
21
20
  attr_reader :credentials
22
21
  attr_reader :processors
23
22
  attr_reader :processor
23
+ attr_reader :aliases
24
+ attr_reader :alias
25
+ attr_reader :storage
26
+ attr_reader :host
24
27
 
25
28
 
26
29
  # A default set of options that can be extended to customize the path, storage or credentials.
@@ -31,10 +34,12 @@ module Attached
31
34
 
32
35
  def self.options
33
36
  @options ||= {
34
- :path => "/:name/:style/:identifier:extension",
35
- :default => :original,
36
- :styles => {},
37
- :processors => [],
37
+ :path => "/:name/:style/:identifier:extension",
38
+ :default => :original,
39
+ :credentials => {},
40
+ :styles => {},
41
+ :processors => [],
42
+ :aliases => [],
38
43
  }
39
44
  end
40
45
 
@@ -54,21 +59,29 @@ module Attached
54
59
  # * :styles - A hash containing optional parameters including extension and identifier
55
60
 
56
61
  def initialize(name, instance, options = {})
62
+ options = self.class.options.merge(options)
63
+
57
64
  @name = name
58
65
  @instance = instance
59
- @options = self.class.options.merge(options)
60
66
 
61
67
  @queue = {}
62
68
 
63
- @path = @options[:path]
64
- @styles = @options[:styles]
65
- @default = @options[:default]
66
- @medium = @options[:medium]
67
- @credentials = @options[:credentials]
68
- @processors = @options[:processors]
69
- @processor = @options[:processor]
69
+ @path = options[:path]
70
+ @styles = options[:styles]
71
+ @default = options[:default]
72
+ @medium = options[:medium]
73
+ @credentials = options[:credentials]
74
+ @processors = options[:processors]
75
+ @processor = options[:processor]
76
+ @aliases = options[:aliases]
77
+ @alias = options[:alias]
78
+
79
+ @processors = self.processors + [self.processor] if self.processor
80
+ @aliases = self.aliases + [self.alias] if self.alias
70
81
 
71
- @processors = @processors + [@processor] if @processor
82
+ @storage = Attached::Storage.storage(self.medium, self.credentials)
83
+
84
+ @host = self.storage.host
72
85
  end
73
86
 
74
87
 
@@ -82,6 +95,10 @@ module Attached
82
95
  instance.changed.include? "#{name}_identifier"
83
96
  end
84
97
 
98
+ def file?
99
+ not identifier.blank?
100
+ end
101
+
85
102
 
86
103
  # Assign an attachment to a file.
87
104
  #
@@ -111,8 +128,6 @@ module Attached
111
128
  # @object.avatar.save
112
129
 
113
130
  def save
114
- @storage ||= Attached::Storage.storage(self.medium, self.credentials)
115
-
116
131
  @queue.each do |style, file|
117
132
  @storage.save(file, self.path(style)) if file and self.path(style)
118
133
  end
@@ -128,8 +143,6 @@ module Attached
128
143
  # @object.avatar.destroy
129
144
 
130
145
  def destroy
131
- @storage ||= Attached::Storage.storage(self.medium, self.credentials)
132
-
133
146
  @storage.destroy(self.path) if self.path
134
147
  end
135
148
 
@@ -143,9 +156,12 @@ module Attached
143
156
  # @object.avatar.url(:large)
144
157
 
145
158
  def url(style = self.default)
146
- @storage ||= Attached::Storage.storage(self.medium, self.credentials)
159
+ path = self.path(style)
160
+
161
+ host = self.host
162
+ host = self.aliases[path.hash % self.aliases.count] unless self.aliases.empty?
147
163
 
148
- return "#{@storage.host}#{path(style)}"
164
+ return "#{host}#{path}"
149
165
  end
150
166
 
151
167
 
@@ -1,4 +1,4 @@
1
- require 'attached/storage/s3'
1
+ require 'attached/storage/aws'
2
2
 
3
3
  module Attached
4
4
  module Storage
@@ -7,12 +7,12 @@ module Attached
7
7
  #
8
8
  # Usage:
9
9
  #
10
- # Attached::Storage.medium(s3)
10
+ # Attached::Storage.medium(aws)
11
11
 
12
- def self.storage(medium = :s3, credentials = nil)
12
+ def self.storage(medium = :aws, credentials = nil)
13
13
 
14
14
  case medium
15
- when :s3 then return Attached::Storage::S3.new(credentials)
15
+ when :aws then return Attached::Storage::AWS.new(credentials)
16
16
  end
17
17
 
18
18
  end
@@ -5,7 +5,7 @@ require 'aws/s3'
5
5
 
6
6
  module Attached
7
7
  module Storage
8
- class S3 < Base
8
+ class AWS < Base
9
9
 
10
10
 
11
11
  attr_reader :bucket
@@ -50,9 +50,9 @@ module Attached
50
50
  def save(file, path)
51
51
  connect()
52
52
  begin
53
- AWS::S3::S3Object.store(path, file, bucket, :access => :public_read)
53
+ ::AWS::S3::S3Object.store(path, file, bucket, :access => :public_read)
54
54
  rescue AWS::S3::NoSuchBucket => e
55
- AWS::S3::Bucket.create(bucket)
55
+ ::AWS::S3::Bucket.create(bucket)
56
56
  retry
57
57
  end
58
58
  end
@@ -67,9 +67,9 @@ module Attached
67
67
  def destroy(path)
68
68
  connect()
69
69
  begin
70
- AWS::S3::S3Object.delete(path, bucket, :access => :authenticated_read)
70
+ ::AWS::S3::S3Object.delete(path, bucket, :access => :authenticated_read)
71
71
  rescue AWS::S3::NoSuchBucket => e
72
- AWS::S3::Bucket.create(bucket)
72
+ ::AWS::S3::Bucket.create(bucket)
73
73
  retry
74
74
  end
75
75
  end
@@ -81,7 +81,7 @@ module Attached
81
81
  # Connect to an AWS S3 server.
82
82
 
83
83
  def connect()
84
- @connection ||= AWS::S3::Base.establish_connection!(
84
+ @connection ||= ::AWS::S3::Base.establish_connection!(
85
85
  :access_key_id => access_key_id, :secret_access_key => secret_access_key
86
86
  )
87
87
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kevin Sylvestre
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-15 00:00:00 -05:00
17
+ date: 2010-12-17 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -57,8 +57,8 @@ files:
57
57
  - lib/attached/image.rb
58
58
  - lib/attached/processor.rb
59
59
  - lib/attached/railtie.rb
60
+ - lib/attached/storage/aws.rb
60
61
  - lib/attached/storage/base.rb
61
- - lib/attached/storage/s3.rb
62
62
  - lib/attached/storage.rb
63
63
  - lib/attached.rb
64
64
  - README.rdoc