uploadcare-rails 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ Here is a rails gem for using uploadcare.com service.
2
+
3
+ * Add gem in your gemfile: `gem uploadcare-rails`
4
+ * Add string attribute in your model: 'rails g migration add_uploadcare_field_in_model <attribute>:string'
5
+ * Add in your model: `is_uploadcare_file :<attribute>`
6
+ * Write in your js and css:
7
+
8
+ in css
9
+ // = require uploadcare/widget
10
+
11
+ in js
12
+ // = require uploadcare/widget
13
+
14
+ * Add widget to your form
15
+
16
+ / for regular form
17
+ = form.uploadcare_uploader_field :<attribute>
18
+
19
+ / form simple_form
20
+ = form.input :<attribute> , as: :uploadcare_uploader
21
+
22
+ * Use attribute in your code:
23
+
24
+ @my_model.attribute.public_url('crop/200x200')
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'UploadcareRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -1,138 +1,8 @@
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"
1
+ require 'uploadcare'
2
+ require 'uploadcare-widget'
3
+ require "uploadcare/rails/engine"
7
4
 
8
5
  module Uploadcare
9
6
  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
7
  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
8
+ end
@@ -0,0 +1,27 @@
1
+ module Uploadcare::Rails::ActionView
2
+ module FormHelper
3
+ def uploadcare_uploader_tag(name)
4
+ hidden_field_tag name, nil, role: 'uploadcare-uploader'
5
+ end
6
+
7
+ def uploadcare_uploader_field(object_name, method, options = {})
8
+ options.symbolize_keys!
9
+ role = "#{options[:role]} uploadcare-uploader"
10
+ options.update(role: "#{options[:role]} uploadcare-uploader")
11
+
12
+ hidden_field(object_name, method, options)
13
+ end
14
+
15
+ def self.included(arg)
16
+ ActionView::Helpers::FormBuilder.send(:include, Uploadcare::Rails::ActionView::FormBuilder)
17
+ end
18
+ end
19
+
20
+ module FormBuilder
21
+ def uploadcare_uploader_field(method, options = {})
22
+ @template.uploadcare_uploader_field(@object_name, method, objectify_options(options))
23
+ end
24
+ end
25
+ end
26
+
27
+ ActionView::Base.send :include, Uploadcare::Rails::ActionView::FormHelper
@@ -0,0 +1,34 @@
1
+ module Uploadcare
2
+ module Rails
3
+ module ActiveRecord
4
+ def is_uploadcare_file attribute, options = {}
5
+ options.symbolize_keys!
6
+ opts = {
7
+ autostore: true
8
+ }.update options
9
+
10
+ define_method "#{attribute}" do
11
+ return nil unless attributes[attribute.to_s].present?
12
+
13
+ if instance_variable_defined?("@#{attribute}_cached")
14
+ instance_variable_get("@#{attribute}_cached")
15
+ else
16
+ file_data = ::Rails.application.config.uploadcare.api.file(attributes[attribute.to_s])
17
+ instance_variable_set("@#{attribute}_cached", file_data)
18
+ file_data
19
+ end
20
+ end
21
+
22
+ after_create "store_#{attribute}" if opts[:autostore]
23
+
24
+ define_method "store_#{attribute}" do
25
+ if send(attribute).present?
26
+ send(attribute).store
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ ActiveRecord::Base.extend Uploadcare::Rails::ActiveRecord
@@ -0,0 +1,26 @@
1
+ require 'uploadcare/rails/settings'
2
+
3
+ module Uploadcare
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ initializer 'uploadcare_rails.init_configuration', before: :load_config_initializers do |app|
7
+ app.config.uploadcare = Uploadcare::Rails::Settings.new
8
+ end
9
+
10
+ initializer 'uploadcare_rails.make_api' do |app|
11
+ app.config.uploadcare.make_api
12
+ end
13
+
14
+ initializer 'uploadcare_rails.load' do
15
+ ActiveSupport.on_load :active_record do
16
+ require 'uploadcare/rails/active_record'
17
+ end
18
+
19
+ ActiveSupport.on_load(:action_view) do
20
+ require 'uploadcare/rails/action_view'
21
+ require 'uploadcare/rails/simple_form' if defined?(SimpleForm)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ module Uploadcare
2
+ module Rails
3
+ class Settings
4
+ @@keys = [
5
+ :public_key, :private_key, :upload_url_base,
6
+ :api_url_base, :static_url_base, :api_version,
7
+ :widget_version
8
+ ]
9
+ cattr_reader :keys
10
+
11
+ keys.each { |key| attr_accessor key }
12
+ attr_reader :api, :uploader
13
+
14
+
15
+ def initialize(settings = {})
16
+ self.class.keys.each do |key|
17
+ send "#{key}=", settings[key] if settings[key].present?
18
+ end
19
+ end
20
+
21
+ def get_settings
22
+ Hash[self.class.keys.select{|k| send(k).present? }.map{|k| [k, send(k)]}]
23
+ end
24
+
25
+ def make_api
26
+ @api = Uploadcare::Api.new(get_settings)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ module Uploadcare::Rails::SimpleForm
2
+ class UploadcareUploaderInput < SimpleForm::Inputs::HiddenInput
3
+ def input_html_options
4
+ @input_html_options.merge role: "#{@input_html_options[:role]} uploadcare-uploader"
5
+ end
6
+ end
7
+ end
8
+
9
+ SimpleForm::Inputs.send :include, Uploadcare::Rails::SimpleForm
@@ -1,5 +1,5 @@
1
1
  module Uploadcare
2
2
  module Rails
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,19 +1,51 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uploadcare-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Alexander Zinchenko
8
+ - Vadim Rastyagaev
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-13 00:00:00.000000000 Z
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: uploadcare
16
- requirement: &70126901194860 !ruby/object:Gem::Requirement
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: uploadcare-api
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: uploadcare-widget
48
+ requirement: !ruby/object:Gem::Requirement
17
49
  none: false
18
50
  requirements:
19
51
  - - ! '>='
@@ -21,30 +53,80 @@ dependencies:
21
53
  version: '0'
22
54
  type: :runtime
23
55
  prerelease: false
24
- version_requirements: *70126901194860
25
- description: Rubygem to use UploadCare.com service and serve file uploads in your
26
- Rails projects.
27
- email: az@uploadcare.com
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: pry-rails
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Rails gem for uploadcare.com service.
111
+ email:
112
+ - abc@oktoberliner.ru
28
113
  executables: []
29
114
  extensions: []
30
115
  extra_rdoc_files: []
31
116
  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/simple_form.rb
37
- - lib/uploadcare-rails/upload.rb
38
- - lib/uploadcare-rails/version.rb
117
+ - lib/uploadcare/rails/action_view.rb
118
+ - lib/uploadcare/rails/active_record.rb
119
+ - lib/uploadcare/rails/engine.rb
120
+ - lib/uploadcare/rails/settings.rb
121
+ - lib/uploadcare/rails/simple_form.rb
122
+ - lib/uploadcare/rails/version.rb
39
123
  - lib/uploadcare-rails.rb
40
- - vendor/assets/javascripts/uploadcare.js
41
- - vendor/assets/javascripts/uploadcare.min.js
42
- - init.rb
43
- homepage: http://github.com/uploadcare/uploadcare-rails
124
+ - MIT-LICENSE
125
+ - Rakefile
126
+ - README.md
127
+ homepage: http://uploadcare.com
44
128
  licenses: []
45
- post_install_message: ! " ==================================================\n Thank
46
- you for installing Uploadcare.com Rails gem.\n Make sure you completed steps described
47
- in the\n gem README.\n ==================================================\n"
129
+ post_install_message:
48
130
  rdoc_options: []
49
131
  require_paths:
50
132
  - lib
@@ -54,16 +136,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
136
  - - ! '>='
55
137
  - !ruby/object:Gem::Version
56
138
  version: '0'
139
+ segments:
140
+ - 0
141
+ hash: 2858975572859248920
57
142
  required_rubygems_version: !ruby/object:Gem::Requirement
58
143
  none: false
59
144
  requirements:
60
145
  - - ! '>='
61
146
  - !ruby/object:Gem::Version
62
147
  version: '0'
148
+ segments:
149
+ - 0
150
+ hash: 2858975572859248920
63
151
  requirements: []
64
152
  rubyforge_project:
65
- rubygems_version: 1.8.10
153
+ rubygems_version: 1.8.23
66
154
  signing_key:
67
155
  specification_version: 3
68
- summary: Rubygem to use UploadCare.com service with ease.
156
+ summary: Rails gem for uploadcare.com service.
69
157
  test_files: []