transloadit-rails 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -51,9 +51,101 @@ Refer to the templates with the `transloadify` helper. This requires jQuery,
|
|
51
51
|
and loads the [Transloadit jQuery plugin](https://github.com/transloadit/jquery-sdk).
|
52
52
|
It also uses JavaScript to ensure your form is encoded as `multipart/form-data`.
|
53
53
|
|
54
|
-
<%= form_for :
|
54
|
+
<%= form_for :upload, :html => { :id => 'upload' } do |form| %>
|
55
55
|
<%= form.transloadit :s3_store %>
|
56
|
-
<%= form.
|
56
|
+
<%= form.label :file, 'File to upload' %>
|
57
|
+
<%= form.file_field :file %>
|
58
|
+
<%= form.submit %>
|
57
59
|
<% end %>
|
58
60
|
|
59
61
|
<%= transloadit_jquerify :upload %>
|
62
|
+
|
63
|
+
## Tutorial
|
64
|
+
|
65
|
+
In this tutorial, you are going to learn how to use transloadit in a freshly
|
66
|
+
setup rails project.
|
67
|
+
|
68
|
+
If you haven't already done so, go ahead an install rails as well as the
|
69
|
+
transloadit gem:
|
70
|
+
|
71
|
+
$ gem install rails
|
72
|
+
$ gem install transloadit
|
73
|
+
|
74
|
+
With rails installed, let's create a new app called 'my_app':
|
75
|
+
|
76
|
+
$ rails new my_app
|
77
|
+
$ cd my_app
|
78
|
+
|
79
|
+
In order to use transloadit in this app, we need to add the gem to our Gemfile
|
80
|
+
and bundle things up:
|
81
|
+
|
82
|
+
$ echo "gem 'transloadit-rails'" >> Gemfile
|
83
|
+
$ bundle install
|
84
|
+
|
85
|
+
With that in place, it's time to generate our transloadit configuration, as
|
86
|
+
well as a basic UploadsController:
|
87
|
+
|
88
|
+
$ rails generate transloadit:install
|
89
|
+
$ rails generate controller uploads new create
|
90
|
+
|
91
|
+
The controller generator we just executed has probably put two GET routes into
|
92
|
+
your `config/routes.rb`. We don't want those, so lets go ahead an overwrite
|
93
|
+
them with this:
|
94
|
+
|
95
|
+
MyApp::Application.routes.draw do
|
96
|
+
resources :uploads
|
97
|
+
end
|
98
|
+
|
99
|
+
Next we need to configure our `config/transloadit.yml` file. For this tutorial,
|
100
|
+
just put in your credentials, and define an image resize step as indicated
|
101
|
+
below:
|
102
|
+
|
103
|
+
auth:
|
104
|
+
key : '4d2e...'
|
105
|
+
secret: '8ad1...'
|
106
|
+
|
107
|
+
templates:
|
108
|
+
image_resize:
|
109
|
+
steps:
|
110
|
+
resize:
|
111
|
+
robot : '/image/resize'
|
112
|
+
format: 'jpg'
|
113
|
+
width : 320
|
114
|
+
height: 200
|
115
|
+
|
116
|
+
Alright, time to create our upload form. In order to do that, please open
|
117
|
+
`app/views/uploads/new.html.erb`, and put the following code in:
|
118
|
+
|
119
|
+
<%= javascript_include_tag '//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js' %>
|
120
|
+
|
121
|
+
<h1>Upload an image</h1>
|
122
|
+
<%= form_for :upload, :html => { :id => 'upload' } do |form| %>
|
123
|
+
<%= form.transloadit :image_resize %>
|
124
|
+
<%= form.label :file, 'File to upload' %>
|
125
|
+
<%= form.file_field :file %>
|
126
|
+
<%= form.submit %>
|
127
|
+
<% end %>
|
128
|
+
|
129
|
+
<%= transloadit_jquerify :upload, :wait => true %>
|
130
|
+
|
131
|
+
Now let's open up our `uploads_controller.rb` in order to modify our `create`
|
132
|
+
action to parse the response JSON we get from the jQuery plugin:
|
133
|
+
|
134
|
+
def create
|
135
|
+
@upload = ActiveSupport::JSON.decode(params[:transloadit])
|
136
|
+
end
|
137
|
+
|
138
|
+
With this in place, we can modify the `create.html.erb` view to render the
|
139
|
+
uploaded and resized image:
|
140
|
+
|
141
|
+
<h1>Resized upload image</h1>
|
142
|
+
<%= image_tag @upload['results']['resize'].first['url'] %>
|
143
|
+
|
144
|
+
That's it. If you've followed the steps closely, you should now be able to
|
145
|
+
try your first upload. Don't forget do start your rails server first:
|
146
|
+
|
147
|
+
$ rails server
|
148
|
+
|
149
|
+
Then go to http://localhost:3000/uploads/new, and upload an image. If you did
|
150
|
+
everything right, you should see the uploaded and resized file as soon as the
|
151
|
+
upload finishes.
|
@@ -6,7 +6,7 @@ module TransloaditHelper
|
|
6
6
|
def transloadit_jquerify(id, options = {})
|
7
7
|
javascript_tag %{
|
8
8
|
$(document).ready(function() {
|
9
|
-
var script = '
|
9
|
+
var script = '//assets.transloadit.com/js/jquery.transloadit2.js';
|
10
10
|
|
11
11
|
$.getScript(script, function() {
|
12
12
|
$('##{id}').attr('enctype', 'multipart/form-data');
|
@@ -4,14 +4,9 @@ require 'rails/generators'
|
|
4
4
|
class Transloadit::Generators::InstallGenerator < Rails::Generators::Base
|
5
5
|
source_root File.expand_path('../../../templates/transloadit', __FILE__)
|
6
6
|
|
7
|
-
desc %{Installs
|
7
|
+
desc %{Installs an empty Transloadit config file}
|
8
8
|
|
9
9
|
def copy_configuration
|
10
10
|
template 'transloadit.yml', 'config/transloadit.yml'
|
11
11
|
end
|
12
|
-
|
13
|
-
def copy_jquery_plugin
|
14
|
-
get 'http://assets.transloadit.com/js/jquery.transloadit2.js',
|
15
|
-
'public/javascripts/jquery.transloadit2.js'
|
16
|
-
end
|
17
12
|
end
|
data/lib/transloadit/rails.rb
CHANGED
@@ -3,6 +3,8 @@ require 'transloadit/generators'
|
|
3
3
|
require 'rails'
|
4
4
|
|
5
5
|
class Transloadit::Rails < Rails::Engine
|
6
|
+
CONFIG_PATH = 'config/transloadit.yml'
|
7
|
+
|
6
8
|
autoload :FormHelper, 'transloadit/rails/form_helper'
|
7
9
|
|
8
10
|
config.to_prepare do
|
@@ -15,14 +17,18 @@ class Transloadit::Rails < Rails::Engine
|
|
15
17
|
end
|
16
18
|
|
17
19
|
initializer 'transloadit.configure' do |app|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
self.class.application = app
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configuration
|
24
|
+
YAML.load_file self.application.root.join(CONFIG_PATH)
|
22
25
|
end
|
23
26
|
|
24
27
|
class << self
|
25
|
-
attr_accessor :
|
28
|
+
attr_accessor :application
|
29
|
+
|
30
|
+
extend ActiveSupport::Memoizable
|
31
|
+
memoize :configuration unless Rails.env.development?
|
26
32
|
end
|
27
33
|
|
28
34
|
#
|
@@ -30,8 +36,8 @@ class Transloadit::Rails < Rails::Engine
|
|
30
36
|
#
|
31
37
|
def self.transloadit
|
32
38
|
Transloadit.new(
|
33
|
-
:key => self.
|
34
|
-
:secret => self.
|
39
|
+
:key => self.configuration['auth']['key'],
|
40
|
+
:secret => self.configuration['auth']['secret']
|
35
41
|
)
|
36
42
|
end
|
37
43
|
|
@@ -39,7 +45,7 @@ class Transloadit::Rails < Rails::Engine
|
|
39
45
|
# Creates an assembly for the named template.
|
40
46
|
#
|
41
47
|
def self.template(name, options = {})
|
42
|
-
template = self.
|
48
|
+
template = self.configuration['templates'].try(:fetch, name.to_s)
|
43
49
|
|
44
50
|
self.transloadit.assembly case template
|
45
51
|
when String then { :template_id => template }.merge(options)
|
@@ -8,12 +8,6 @@ class InstallGeneratorTest < Rails::Generators::TestCase
|
|
8
8
|
prepare_destination
|
9
9
|
end
|
10
10
|
|
11
|
-
test 'the jQuery plugin is installed' do
|
12
|
-
run_generator
|
13
|
-
|
14
|
-
assert_file 'public/javascripts/jquery.transloadit2.js'
|
15
|
-
end
|
16
|
-
|
17
11
|
test 'the Transloadit config file is installed' do
|
18
12
|
run_generator
|
19
13
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: transloadit-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.9.
|
5
|
+
version: 0.9.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Stephen
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-04-
|
14
|
+
date: 2011-04-19 00:00:00 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: transloadit
|