uploadcare-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/init.rb +4 -0
- data/lib/uploadcare-rails.rb +138 -0
- data/lib/uploadcare-rails/engine.rb +6 -0
- data/lib/uploadcare-rails/exception.rb +11 -0
- data/lib/uploadcare-rails/inject.rb +21 -0
- data/lib/uploadcare-rails/install.rb +59 -0
- data/lib/uploadcare-rails/upload.rb +106 -0
- data/lib/uploadcare-rails/version.rb +5 -0
- metadata +66 -0
data/init.rb
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
require "uploadcare-rails/engine"
|
|
2
|
+
require "uploadcare-rails/version"
|
|
3
|
+
require "uploadcare-rails/exception"
|
|
4
|
+
require "uploadcare-rails/install"
|
|
5
|
+
require "uploadcare-rails/inject"
|
|
6
|
+
require "uploadcare-rails/upload"
|
|
7
|
+
|
|
8
|
+
module Uploadcare
|
|
9
|
+
module Rails
|
|
10
|
+
def self.installed?
|
|
11
|
+
Install.installed?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.install
|
|
15
|
+
Install.install
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.config_location
|
|
19
|
+
Install.config_location
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.config
|
|
23
|
+
config = if installed?
|
|
24
|
+
YAML.load_file(config_location)
|
|
25
|
+
else
|
|
26
|
+
Install.default_config
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
module Rails
|
|
32
|
+
module ClassMethods
|
|
33
|
+
def has_uploadcare_file(name, options = {})
|
|
34
|
+
include InstanceMethods
|
|
35
|
+
|
|
36
|
+
options[:file_column] ||= name.to_s
|
|
37
|
+
options[:auto_keep] = true if options[:auto_keep].nil?
|
|
38
|
+
|
|
39
|
+
unless self.column_names.include?(options[:file_column])
|
|
40
|
+
raise DatabaseError, "File column not found in columns list, please generate a migration to create a file column and 'rake db:migrate' to apply changes to database.", caller
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
define_method name do
|
|
44
|
+
upload_for name, options
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
define_method "#{name.to_s}_before_type_cast" do
|
|
48
|
+
read_attribute(name.to_sym)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
define_method "#{name.to_s}=" do |uuid|
|
|
52
|
+
self.instance_variable_set("@_#{options[:file_column]}", read_attribute(options[:file_column]))
|
|
53
|
+
self.instance_variable_set("@_#{name.to_s}", uuid)
|
|
54
|
+
write_attribute(name.to_sym, uuid)
|
|
55
|
+
upload_for(name, options).assign_uuid(uuid)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# TODO: Think about validation
|
|
60
|
+
#
|
|
61
|
+
# def validates_upload_type(name, options = {})
|
|
62
|
+
# end
|
|
63
|
+
|
|
64
|
+
# def validates_upload_size(name, options = {})
|
|
65
|
+
# min = options[:min] || options[:in] && options[:in].first || 0
|
|
66
|
+
# max = options[:max] || options[:in] && options[:in].last || 0
|
|
67
|
+
# message = options[:message] || "file size must be between :min and :max bytes"
|
|
68
|
+
# message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
|
|
69
|
+
|
|
70
|
+
# validates_each :"#{name}" do |record, attr, value|
|
|
71
|
+
# if_clause_passed = options[:if].nil? || (options[:if].respond_to?(:call) ? options[:if].call(record) != false : record.send(options[:if]))
|
|
72
|
+
# unless_clause_passed = options[:unless].nil? || (options[:unless].respond_to?(:call) ? !!options[:unless].call(record) == false : !record.send(options[:unless]))
|
|
73
|
+
# upload = record.send("#{name}")
|
|
74
|
+
# if upload.info_loaded?
|
|
75
|
+
# if if_clause_passed && unless_clause_passed && (upload.size < min || (upload.size > max && !max.zero?))
|
|
76
|
+
# record.errors.add("#{name}", message)
|
|
77
|
+
# end
|
|
78
|
+
# else
|
|
79
|
+
# record.errors.add("#{name}", message)
|
|
80
|
+
# end
|
|
81
|
+
# end
|
|
82
|
+
# end
|
|
83
|
+
|
|
84
|
+
def validates_upload_presence name, options = {}
|
|
85
|
+
message = options[:message] || "must be present"
|
|
86
|
+
validates_each :"#{name}" do |record, attr, value|
|
|
87
|
+
upload = record.send :"#{name}"
|
|
88
|
+
unless upload.uuid_value =~ /[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/
|
|
89
|
+
record.errors.add "#{name}", message
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
module InstanceMethods
|
|
98
|
+
def upload_for(name, options)
|
|
99
|
+
Upload.new(name, options, self)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
module ActiveRecord
|
|
104
|
+
def self.included(base)
|
|
105
|
+
base.extend ClassMethods
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
module FormBuilder
|
|
110
|
+
module InstanceMethods
|
|
111
|
+
def uploadcare_field(method, options = {})
|
|
112
|
+
config = ::Uploadcare::Rails.config
|
|
113
|
+
options[:role] ||= case config["widget_type"]
|
|
114
|
+
when "plain"
|
|
115
|
+
"uploadcare-plain-uploader"
|
|
116
|
+
when "line"
|
|
117
|
+
"uploadcare-line-uploader"
|
|
118
|
+
else
|
|
119
|
+
"uploadcare-plain-uploader"
|
|
120
|
+
end
|
|
121
|
+
options["data-public-key"] ||= config["public_key"]
|
|
122
|
+
|
|
123
|
+
self.hidden_field method, options
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def self.included(base)
|
|
128
|
+
base.send(:include, InstanceMethods)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
class File
|
|
134
|
+
def remove
|
|
135
|
+
@ucare.make_request('DELETE', api_uri).parsed_response
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class Uploadcare::Rails::Error < RuntimeError
|
|
2
|
+
end
|
|
3
|
+
|
|
4
|
+
class Uploadcare::Rails::InstallationError < Uploadcare::Rails::Error
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class Uploadcare::Rails::CommunicationError < Uploadcare::Rails::Error
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Uploadcare::Rails::DatabaseError < Uploadcare::Rails::Error
|
|
11
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'uploadcare-rails'
|
|
2
|
+
|
|
3
|
+
module Uploadcare::Rails::Inject
|
|
4
|
+
if defined? Rails::Railtie
|
|
5
|
+
require 'rails'
|
|
6
|
+
|
|
7
|
+
class Railtie < Rails::Railtie
|
|
8
|
+
initializer 'uploadcare.insert_into_active_record' do
|
|
9
|
+
ActiveSupport.on_load :active_record do
|
|
10
|
+
Uploadcare::Rails.install unless Uploadcare::Rails.installed?
|
|
11
|
+
Uploadcare::Rails::Inject.try_inject
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.try_inject
|
|
18
|
+
ActiveRecord::Base.send(:include, Uploadcare::Rails::ActiveRecord) if defined? ActiveRecord
|
|
19
|
+
ActionView::Helpers::FormBuilder.send(:include, Uploadcare::Rails::FormBuilder) if defined? ActionView
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Uploadcare::Rails::Install
|
|
2
|
+
def self.installed?
|
|
3
|
+
File.exist?(self.config_location)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def self.install
|
|
7
|
+
loc = self.config_location
|
|
8
|
+
config = self.default_config
|
|
9
|
+
|
|
10
|
+
f = File.open(loc, "w")
|
|
11
|
+
begin
|
|
12
|
+
f.puts config.to_yaml
|
|
13
|
+
rescue
|
|
14
|
+
raise Uploadcare::Rails::InstallationError, "Failed to write configuration file in '#{loc}'", caller
|
|
15
|
+
ensure
|
|
16
|
+
f.close unless f.nil?
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.backup_config(filename = "")
|
|
21
|
+
if filename.empty?
|
|
22
|
+
filename = self.get_location "uploadcare.yml.old"
|
|
23
|
+
filename += ".old" if File.exist?(filename)
|
|
24
|
+
end
|
|
25
|
+
begin
|
|
26
|
+
FileUtils.copy_file self.config_location, filename
|
|
27
|
+
rescue
|
|
28
|
+
raise Uploadcare::Rails::InstallationError, "Failed to backup configuration file to '#{filename}'", caller
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.rewrite_config(backup = true)
|
|
33
|
+
if self.installed? && backup
|
|
34
|
+
self.backup_config
|
|
35
|
+
end
|
|
36
|
+
self.install
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.default_config
|
|
40
|
+
Hash[
|
|
41
|
+
"public_key" => "",
|
|
42
|
+
"private_key" => "",
|
|
43
|
+
"widget_type" => "plain"
|
|
44
|
+
]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.config_location
|
|
48
|
+
self.get_location "uploadcare.yml"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.get_location(filename)
|
|
52
|
+
if defined? Rails && !Rails.root.blank?
|
|
53
|
+
Rails.root.join("config", filename)
|
|
54
|
+
else
|
|
55
|
+
# Try relative path
|
|
56
|
+
"config/#{filename}"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require 'uploadcare'
|
|
2
|
+
|
|
3
|
+
class Uploadcare::Rails::Upload
|
|
4
|
+
SERVICES_URL = 'http://services.uploadcare.com'
|
|
5
|
+
IMAGE_MIMES = ['jpeg', 'jpg', 'png', 'gif', 'tiff'].map!{|x| "image/#{x}"}
|
|
6
|
+
attr_reader :uuid
|
|
7
|
+
|
|
8
|
+
def initialize(field_method, options, instance)
|
|
9
|
+
@_field_method = field_method
|
|
10
|
+
@_options = options
|
|
11
|
+
@_instance = instance
|
|
12
|
+
@uuid = self.uuid_value
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def removed?
|
|
16
|
+
inf = self.file_info
|
|
17
|
+
!inf["removed"].blank? && inf["removed"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def image?
|
|
21
|
+
IMAGE_MIMES.include?(@_file_info["mime_type"])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def url
|
|
25
|
+
self.file_info["url"]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def resized_url(to = '640x480')
|
|
29
|
+
url = [SERVICES_URL].push('resizer').push(@uuid).push(to).join('/')
|
|
30
|
+
url + "/"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def crop_url(to = '640x480')
|
|
34
|
+
url = [self.resized_url(to)].push('crop').join('/')
|
|
35
|
+
url + "/"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def original_url
|
|
39
|
+
self.file_info["original_file_url"]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def size
|
|
43
|
+
self.file_info["size"].blank? ? 0 : self.file_info["size"]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def kept?
|
|
47
|
+
!self.file_info["last_keep_claim"].blank?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def file_info
|
|
51
|
+
get_file_info
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def assign_uuid(new_uuid)
|
|
55
|
+
if @_options[:auto_keep]
|
|
56
|
+
self.keep
|
|
57
|
+
else
|
|
58
|
+
self.reload unless new_uuid.blank?
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def uuid_value
|
|
63
|
+
@_instance.send("#{@_field_method.to_s}_before_type_cast")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def keep
|
|
67
|
+
f = self.get_file_instance
|
|
68
|
+
f.keep
|
|
69
|
+
self.reload
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def reload
|
|
73
|
+
save_instance
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def remove
|
|
77
|
+
self.get_file_instance.remove
|
|
78
|
+
save_instance
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
protected
|
|
82
|
+
def get_file_info
|
|
83
|
+
f = self.get_file_instance
|
|
84
|
+
f.info
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def get_file_instance
|
|
88
|
+
self.load_config
|
|
89
|
+
self.load_api_client
|
|
90
|
+
|
|
91
|
+
@_api_client.file(@uuid)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def load_config
|
|
95
|
+
@_config = ::Uploadcare::Rails.config if @_config.nil?
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def load_api_client
|
|
99
|
+
self.load_config if @_config.nil?
|
|
100
|
+
@_api_client = ::Uploadcare.new(@_config["public_key"], @_config["private_key"])
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def save_instance
|
|
104
|
+
@_instance.save unless self.uuid_value.blank?
|
|
105
|
+
end
|
|
106
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: uploadcare-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Alexander Zinchenko
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-07-09 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: uploadcare
|
|
16
|
+
requirement: &70124451385440 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70124451385440
|
|
25
|
+
description: Rubygem to use UploadCare.com service and serve file uploads in your
|
|
26
|
+
Rails projects.
|
|
27
|
+
email: az@uploadcare.com
|
|
28
|
+
executables: []
|
|
29
|
+
extensions: []
|
|
30
|
+
extra_rdoc_files: []
|
|
31
|
+
files:
|
|
32
|
+
- lib/uploadcare-rails/engine.rb
|
|
33
|
+
- lib/uploadcare-rails/exception.rb
|
|
34
|
+
- lib/uploadcare-rails/inject.rb
|
|
35
|
+
- lib/uploadcare-rails/install.rb
|
|
36
|
+
- lib/uploadcare-rails/upload.rb
|
|
37
|
+
- lib/uploadcare-rails/version.rb
|
|
38
|
+
- lib/uploadcare-rails.rb
|
|
39
|
+
- init.rb
|
|
40
|
+
homepage: http://github.com/uploadcare/uploadcare-rails
|
|
41
|
+
licenses: []
|
|
42
|
+
post_install_message: ! " ==================================================\n Thank
|
|
43
|
+
you for installing Uploadcare.com Rails gem.\n Make sure you completed steps described
|
|
44
|
+
in the\n gem README.\n ==================================================\n"
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
none: false
|
|
56
|
+
requirements:
|
|
57
|
+
- - ! '>='
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
requirements: []
|
|
61
|
+
rubyforge_project:
|
|
62
|
+
rubygems_version: 1.8.10
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 3
|
|
65
|
+
summary: Rubygem to use UploadCare.com service with ease.
|
|
66
|
+
test_files: []
|