sunrise-file-upload 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,6 +74,7 @@ module Sunrise
74
74
  if fileuploads_columns.include?(method.to_sym)
75
75
  asset = new_record? ? self.class.fileupload_find(method, fileupload_guid) : send(method)
76
76
  asset ||= send("build_#{method}") if respond_to?("build_#{method}")
77
+ asset
77
78
  end
78
79
  end
79
80
 
@@ -23,11 +23,9 @@ module Sunrise
23
23
  request = Request.new(env)
24
24
  params = request.params.symbolize_keys
25
25
 
26
- klass = find_klass(params)
27
- asset = find_asset(klass, params) || klass.new(params[:asset])
28
-
26
+ asset = find_or_build_asset(params)
29
27
  asset.assetable_type = params[:assetable_type]
30
- asset.assetable_id = params[:assetable_id].blank? ? 0 : params[:assetable_id].to_i
28
+ asset.assetable_id = (params[:assetable_id] || 0).to_i
31
29
  asset.guid = params[:guid]
32
30
  asset.data = Http.normalize_param(params[:qqfile], request)
33
31
 
@@ -45,21 +43,34 @@ module Sunrise
45
43
 
46
44
  [status, {'Content-Type' => 'text/html', 'Content-Length' => body.size.to_s}, body]
47
45
  end
48
-
49
- def find_klass(params)
50
- params[:klass].blank? ? Asset : params[:klass].classify.constantize
46
+
47
+ def find_or_build_asset(params)
48
+ assetable = load_klass(params[:assetable_type])
49
+ attribute_name = params[:method].to_s.downcase.to_sym
50
+ reflection = assetable.reflect_on_association(attribute_name)
51
+ klass = load_klass(reflection.class_name)
52
+
53
+ asset = nil
54
+ asset = find_asset(klass, params) unless reflection.collection?
55
+ asset || klass.new(params[:asset])
56
+ end
57
+
58
+ def load_klass(class_name)
59
+ return nil if class_name.blank?
60
+ class_name.to_s.classify.constantize
51
61
  end
52
62
 
53
63
  def find_asset(klass, params)
54
- query = klass.scoped
64
+ query = klass.scoped.where(:assetable_type => params[:assetable_type])
55
65
 
56
- unless params[:assetable_id].blank? || params[:assetable_type].blank?
66
+ if !params[:assetable_id].blank?
57
67
  query = query.where(:assetable_id => params[:assetable_id].to_i)
58
- query = query.where(:assetable_type => params[:assetable_type])
59
- else
68
+ elsif !params[:guid]
60
69
  query = query.where(:guid => params[:guid])
70
+ else
71
+ query = query.where(:id => params[:id])
61
72
  end
62
-
73
+
63
74
  query.first
64
75
  end
65
76
 
@@ -1,5 +1,5 @@
1
1
  module Sunrise
2
2
  module FileUpload
3
- VERSION = "0.1.1".freeze
3
+ VERSION = "0.1.2".freeze
4
4
  end
5
5
  end
@@ -16,7 +16,7 @@ module Sunrise
16
16
  script_options = (options.delete(:script) || {}).stringify_keys
17
17
 
18
18
  params = {
19
- :klass => value.class.name,
19
+ :method => method,
20
20
  :assetable_id => object.new_record? ? nil : object.id,
21
21
  :assetable_type => object.class.name,
22
22
  :guid => element_guid
@@ -26,13 +26,11 @@ module Sunrise
26
26
  script_options['allowedExtensions'] ||= ['jpg', 'jpeg', 'png', 'gif']
27
27
  script_options['multiple'] ||= object.fileupload_multiple?(method)
28
28
 
29
- content_tag(:div, :class => 'fileupload') do
30
- content_tag(:div, :id => element_id) do
31
- content_tag(:noscript) do
32
- fields_for object do |form|
33
- form.fields_for method, value do |f|
34
- f.file_field :data
35
- end
29
+ content_tag(:div, :class => 'fileupload', :id => element_id) do
30
+ content_tag(:noscript) do
31
+ fields_for object do |form|
32
+ form.fields_for method, value do |f|
33
+ f.file_field :data
36
34
  end
37
35
  end
38
36
  end + javascript_tag( fileupload_script(element_id, value, script_options) )
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunrise-file-upload
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Igor Galeta
@@ -16,7 +16,8 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-05-19 00:00:00 Z
19
+ date: 2011-05-23 00:00:00 +03:00
20
+ default_executable:
20
21
  dependencies: []
21
22
 
22
23
  description: Sunrise is a Aimbulance CMS
@@ -44,6 +45,7 @@ files:
44
45
  - lib/sunrise/file_upload/request.rb
45
46
  - lib/sunrise/file_upload/version.rb
46
47
  - lib/sunrise/file_upload/view_helper.rb
48
+ has_rdoc: true
47
49
  homepage: https://github.com/galetahub/sunrise-file-upload
48
50
  licenses: []
49
51
 
@@ -73,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
75
  requirements: []
74
76
 
75
77
  rubyforge_project:
76
- rubygems_version: 1.8.2
78
+ rubygems_version: 1.6.2
77
79
  signing_key:
78
80
  specification_version: 3
79
81
  summary: Rails FileUpload