anaconda 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +29 -1
- data/lib/anaconda.rb +5 -0
- data/lib/anaconda/anaconda.rb +8 -0
- data/lib/anaconda/anaconda_for.rb +47 -1
- data/lib/anaconda/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48af1f9503f065b652298a02de0fe6f0f22ead90
|
4
|
+
data.tar.gz: 91ba09bdb12f881ccd3b8612d482fe7681f41b82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96ba09c1ba5ec0d753ebdeeecdad087ce928219729d1f8b568b40eee54714474ae1d82161dbe2d5a03e7113a4d0a96574f1fef542573c837f2145c9bb0597ec4
|
7
|
+
data.tar.gz: 3a14cc1ba75147fb24a87315e6f13a69596106afb0bc8c9d4d19d0b341a3be6deda93f915c5ade422a8ae3600442c317d763ca873742f6319e2c4602aa833b2d
|
data/README.markdown
CHANGED
@@ -76,6 +76,27 @@ We highly recommend the `figaro` gem [https://github.com/laserlemon/figaro](http
|
|
76
76
|
* Controller changes
|
77
77
|
|
78
78
|
You must add these parameters to your permitted parameters. In Rails 4 this is done via strong parameters in the controller. In Rails 3 this is done in the model via attr_accessible.
|
79
|
+
|
80
|
+
We add two Class methods on the model that return an array of columns used by each anaconda_for column. One is scoped per anaconda field, and the other returns all of the columns anaconda needs for the entire model. This is useful if you have multiple anaconda columns in your model.
|
81
|
+
|
82
|
+
You can use these methods in your strong parameter list directly. Ex:
|
83
|
+
|
84
|
+
PostMediasController < Application Controller
|
85
|
+
...
|
86
|
+
def post_media_params
|
87
|
+
params.require(:post_media).permit(
|
88
|
+
:name,
|
89
|
+
:foobar,
|
90
|
+
PostMedia.anaconda_fields_for( :asset )
|
91
|
+
)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
This keeps your strong parameter list clean and dry. If you have multiple anaconda models in your model and you wish for all of the params for all of the models to be permitted, you may use `PostMedia.anaconda_fields_for_all_columns` instead.
|
96
|
+
|
97
|
+
We have not tested if you can use `Model.anaconda_fields_for( column )` in the rails 3 attr_accessible list.
|
98
|
+
|
99
|
+
If you prefer to do this manually the fields this permit are listed below.
|
79
100
|
|
80
101
|
For each `anaconda_for` (assuming `anaconda_for :asset`):
|
81
102
|
|
@@ -117,7 +138,8 @@ We highly recommend the `figaro` gem [https://github.com/laserlemon/figaro](http
|
|
117
138
|
* `allowed_file_types` default: _all_
|
118
139
|
* `host` String. If specified, this will be used to access publically stored objects instead of the S3 bucket. Useful for CloudFront integration. Note: At this time privately stored objects will still be requested via S3. Default: _false_
|
119
140
|
* `protocol` `https`, `http`, or `:auto`. If `:auto`, `//` will be used as the protocol. Note: At this time, all privately stored objects are requested over https. Default: `http`
|
120
|
-
|
141
|
+
* `remove_previous_s3_files_on_change` Boolean. If true, files will be removed from S3 when a new file is uploaded. Default: `true`
|
142
|
+
* `remove_previous_s3_files_on_destroy` Boolean. If true, files will be removed from S3 when a record is destroyed. Default: `true`
|
121
143
|
|
122
144
|
* Form setup
|
123
145
|
|
@@ -148,6 +170,7 @@ We highly recommend the `figaro` gem [https://github.com/laserlemon/figaro](http
|
|
148
170
|
* :asset_stored_privately
|
149
171
|
* :asset_type
|
150
172
|
* :asset_url
|
173
|
+
* :asset_download_url
|
151
174
|
|
152
175
|
The magic methods are asset_url and asset_download_url.
|
153
176
|
|
@@ -156,6 +179,11 @@ We highly recommend the `figaro` gem [https://github.com/laserlemon/figaro](http
|
|
156
179
|
`asset_download_url` will return a signed S3 URL with content-disposition set to attachment so the file will be downloaded instead of opened in the browser.
|
157
180
|
|
158
181
|
## Changelog
|
182
|
+
* 0.12.0
|
183
|
+
* Delete files from S3 when a new one us uploaded, or the record is deleted.
|
184
|
+
* Add options to disable deleting files from S3 when a new one is uploaded (`remove_previous_s3_files_on_change` and `remove_previous_s3_files_on_destroy`). These default to `true`
|
185
|
+
* Add `Model.anaconda_fields_for_all_columns` and `Model.anaconda_fields_for(column_name)` methods to make strong parameters cleaner
|
186
|
+
|
159
187
|
* 0.11.0
|
160
188
|
* Change aws URLs to use path style URLs
|
161
189
|
|
data/lib/anaconda.rb
CHANGED
@@ -49,4 +49,9 @@ module Anaconda
|
|
49
49
|
end
|
50
50
|
return js_file_types
|
51
51
|
end
|
52
|
+
|
53
|
+
def self.remove_s3_object_in_bucket_with_file_path(bucket, file_path)
|
54
|
+
aws = Fog::Storage.new({:provider => 'AWS', :aws_access_key_id => Anaconda.aws[:aws_access_key], :aws_secret_access_key => Anaconda.aws[:aws_secret_key], :path_style => true})
|
55
|
+
aws.delete_object(bucket, file_path)
|
56
|
+
end
|
52
57
|
end
|
data/lib/anaconda/anaconda.rb
CHANGED
@@ -38,9 +38,30 @@ module Anaconda
|
|
38
38
|
allowed_file_types: [],
|
39
39
|
base_key: "#{self.to_s.pluralize.downcase}/#{anaconda_column.to_s.pluralize}/#{(0...32).map{(65+rand(26)).chr}.join.downcase}",
|
40
40
|
host: false,
|
41
|
-
protocol: "http"
|
41
|
+
protocol: "http",
|
42
|
+
remove_previous_s3_files_on_change: true,
|
43
|
+
remove_previous_s3_files_on_destroy: true
|
42
44
|
)
|
45
|
+
|
46
|
+
self.after_commit :anaconda_remove_previous_s3_files_on_change_or_destroy
|
47
|
+
end
|
48
|
+
|
49
|
+
def anaconda_fields_for( anaconda_column )
|
50
|
+
if self.anaconda_columns.include? anaconda_column.to_sym
|
51
|
+
Anaconda::FieldSuffixes.collect do |suffix|
|
52
|
+
"#{anaconda_column}_#{suffix}".to_sym
|
53
|
+
end
|
54
|
+
else
|
55
|
+
raise "#{anaconda_column} not configured for anaconda. Misspelling or did you forget to add the anaconda_for call for this field?"
|
56
|
+
end
|
43
57
|
end
|
58
|
+
|
59
|
+
def anaconda_fields_for_all_columns
|
60
|
+
self.anaconda_columns.collect do |column|
|
61
|
+
anaconda_fields_for column
|
62
|
+
end.flatten
|
63
|
+
end
|
64
|
+
|
44
65
|
end
|
45
66
|
module InstanceMethods
|
46
67
|
def method_missing(method, *args, &block)
|
@@ -96,6 +117,31 @@ module Anaconda
|
|
96
117
|
"#{self.anaconda_options[column_name.to_sym][:protocol]}://"
|
97
118
|
end
|
98
119
|
end
|
120
|
+
|
121
|
+
def anaconda_remove_previous_s3_files_on_change_or_destroy
|
122
|
+
|
123
|
+
if self.destroyed?
|
124
|
+
self.class.anaconda_columns.each do |column_name|
|
125
|
+
next unless self.anaconda_options[column_name.to_sym][:remove_previous_s3_files_on_destroy]
|
126
|
+
if self.send("#{column_name}_file_path").present?
|
127
|
+
Anaconda.remove_s3_object_in_bucket_with_file_path(Anaconda.aws[:aws_bucket], self.send("#{column_name}_file_path"))
|
128
|
+
end
|
129
|
+
end
|
130
|
+
else
|
131
|
+
self.class.anaconda_columns.each do |column_name|
|
132
|
+
next unless self.anaconda_options[column_name.to_sym][:remove_previous_s3_files_on_change]
|
133
|
+
if self.previous_changes["#{column_name}_file_path"].present?
|
134
|
+
# Looks like this field was edited.
|
135
|
+
if self.previous_changes["#{column_name}_file_path"][0].present? &&
|
136
|
+
self.previous_changes["#{column_name}_file_path"][0] != self.previous_changes["#{column_name}_file_path"][1]
|
137
|
+
# It's not a new entry ([0] would be nil), and it really did change, wasn't just committed for no reason
|
138
|
+
# So let's delete the previous file from S3
|
139
|
+
Anaconda.remove_s3_object_in_bucket_with_file_path(Anaconda.aws[:aws_bucket], self.previous_changes["#{column_name}_file_path"][0])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
99
145
|
end
|
100
146
|
end
|
101
147
|
end
|
data/lib/anaconda/version.rb
CHANGED