uploadify_rails 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uploadify_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ gem 'uploadify_rails'
6
+
7
+ And then execute:
8
+
9
+ $ bundle
10
+
11
+ ## Usage
12
+
13
+ Migration (Photo work with Paperclip):
14
+
15
+ create_table :photos do |t|
16
+ t.integer :advert_id # объявление
17
+ t.integer :user_id # пользователь (авторизованный)
18
+ t.string :session_id # пользователь (гость)
19
+ t.string :data_content_type
20
+ t.string :data_file_name
21
+ t.integer :data_file_size
22
+ t.timestamps
23
+ end
24
+
25
+ Routes:
26
+
27
+ resources :photos, :only => [:create]
28
+ post "/photo/:id" => "photos#destroy"
29
+
30
+ Javascripts:
31
+
32
+ //= require uploadify
33
+
34
+ Stylesheets:
35
+
36
+ *= require uploadify
37
+
38
+ Model Parent (F.E. Advert):
39
+
40
+ class Advert ...
41
+ ...
42
+ has_many :photos, :dependent => :destroy
43
+ accepts_nested_attributes_for :photos, :allow_destroy => true
44
+ ...
45
+ # Support only one relation name in just moment
46
+ uploadify_nested_parent :relations => [:photos]
47
+ ...
48
+
49
+ Model with Photo:
50
+
51
+ class Photo ...
52
+ uploadify_nested_resource
53
+
54
+ Photos Controller:
55
+
56
+ class PhotosController < ApplicationController
57
+ uploadify_nested_resource
58
+ end
59
+
60
+ Adverts Controller:
61
+
62
+ class AdvertsController < ApplicationController
63
+ ...
64
+ def create
65
+ @advert = Advert.new
66
+ @advert.build_attributes_from_params(params, current_user, session[:session_id])
67
+ if @advert.save
68
+ ...
69
+
70
+ View:
71
+
72
+ Example: in app/views/shared/uploadify/...
73
+
74
+ Formtastic Form Integration (@advert form):
75
+
76
+ = form.input :photos, :as => :uploadify
77
+
78
+ ## Enjoy!
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
87
+
88
+ ## Thanks
89
+
90
+ Thanks to https://github.com/dead-zygote for javascripts and first time realization
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ <div class="fields">
2
+ <div class="uploaded-container">
3
+ <a class="show_big" href="<%= object.data.url(:big) %>">
4
+ <img src="<%= object.data.url(:small) %>" width="100" height="100" />
5
+ </a>
6
+ </div>
7
+ <div class="uploaded-info">
8
+ <input type="hidden" value="<%= object.id %>" name="photo_ids[]" />
9
+ <div><%= number_to_human_size(object.data_file_size) %></div>
10
+ <div><%= object.data_content_type %></div>
11
+ <div>
12
+ <a id="photo_<%= object.id %>" class="delete" href="javascript:void(0)">Удалить</a>
13
+ </div>
14
+ </div>
15
+ </div>
@@ -0,0 +1,6 @@
1
+ <div id="add-nested-resources" data-uploader="/photos"></div>
2
+ <div id="nested_resources">
3
+ <% form.object.photos.each do |photo| %>
4
+ <%= render "shared/uploadify/photos/fields", :object => photo %>
5
+ <% end %>
6
+ </div>
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+ module UploadifyRails
3
+ module Controllers
4
+ module Base
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def uploadify_nested_resource
11
+ unless included_modules.include? InstanceMethods
12
+ include InstanceMethods
13
+ end
14
+ skip_before_filter :verify_authenticity_token, :only => [:create]
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+ def create
20
+ object = controller_name.classify.constantize.new
21
+ object.build_nested_resource(params, current_user)
22
+ if object.save
23
+ render :partial => "shared/uploadify/#{controller_name}/fields",
24
+ :locals => {:object => object}
25
+ else
26
+ head 442
27
+ end
28
+ end
29
+
30
+ def destroy
31
+ load_object
32
+ @object.destroy
33
+ head :ok
34
+ end
35
+
36
+ protected
37
+
38
+ def load_object
39
+ model = controller_name.classify.constantize
40
+ object_by_session = model.where(:session_id => session[:session_id], :id => params[:id]).first
41
+ object_by_current_user = current_user.send(controller_name).find_by_id(params[:id]) if current_user
42
+ @object = object_by_session || (object_by_current_user if object_by_current_user)
43
+ permission_denied if @object.nil?
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module UploadifyRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace UploadifyRails
4
+ engine_name "uploadify_rails"
5
+
6
+ initializer "uploadify_rails.hooks" do
7
+ if Object.const_defined?("Formtastic")
8
+ require "uploadify_rails/hooks/formtastic"
9
+ end
10
+ end
11
+
12
+ initializer "uploadify_rails.assets_flash" do |app|
13
+ app.config.assets.paths << app.root.join("vendor", "assets", "flash")
14
+ end
15
+
16
+ initializer "uploadify_rails.includers" do |app|
17
+ ActionController::Base.send :include, UploadifyRails::Controllers::Base
18
+ ActiveRecord::Base.send :include, UploadifyRails::Models::Resource
19
+ ActiveRecord::Base.send :include, UploadifyRails::Models::Parent
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ require "formtastic"
3
+ require "renderer"
4
+
5
+ module Formtastic
6
+ module Inputs
7
+ class UploadifyInput
8
+ # include ActionView::Helpers::TextHelper
9
+ include Base
10
+
11
+ def to_html
12
+ uploader = Renderer.render("shared/uploadify/#{method.to_s}/form", {:form => builder})
13
+ input_wrapping do
14
+ label_html << uploader.html_safe
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ module UploadifyRails
3
+ module Models
4
+ module Parent
5
+ include ActionView::Helpers::TextHelper
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def uploadify_nested_parent(options = {})
13
+ class_attribute :uploadify_options
14
+ self.uploadify_options = {
15
+ # Only one relation in this version!
16
+ :relations => (options[:relations] || [:photos]),
17
+ }
18
+ unless included_modules.include? InstanceMethods
19
+ include InstanceMethods
20
+ end
21
+ end
22
+ end
23
+
24
+ module InstanceMethods
25
+ def build_attributes_from_params(params, user, session_id)
26
+ parent_class_singular = self.class.to_s.singularize.downcase
27
+ relation_name = uploadify_options[:relations].first.to_s
28
+ relation_singular = relation_name.singularize
29
+ relation_model = relation_singular.classify.constantize
30
+ params[parent_class_singular].delete("#{relation_name}_attributes")
31
+ self.attributes = params[parent_class_singular]
32
+ self.user_id = user ? user.id : nil
33
+ self.session_id = session_id
34
+ object_ids = params["#{relation_singular}_ids"]
35
+ if object_ids && !object_ids.blank?
36
+ checked_object_ids = []
37
+ object_ids.first.values.each do |object_id|
38
+ object = relation_model.find_by_id(object_id)
39
+ if object
40
+ if (object.session_id == session_id ||(user && user.id == object.user_id))
41
+ checked_object_ids << object_id
42
+ object.update_attribute(:user_id, user.id) if user
43
+ end
44
+ end
45
+ end
46
+ self.send("#{relation_singular}_ids=", checked_object_ids)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ module UploadifyRails
3
+ module Models
4
+ module Resource
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def uploadify_nested_resource
11
+ unless included_modules.include? InstanceMethods
12
+ include InstanceMethods
13
+ end
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def build_nested_resource(params, user)
19
+ params[:Filedata].content_type = MIME::Types.type_for(params[:Filedata].original_filename).first
20
+ self.data = params[:Filedata]
21
+ self.session_id = params[:_session_id]
22
+ self.user = user if user
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module UploadifyRails
2
+ VERSION = "0.0.12"
3
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ require "uploadify_rails/controllers/base"
3
+ require "uploadify_rails/models/resource"
4
+ require "uploadify_rails/models/parent"
5
+ require "uploadify_rails/engine"
6
+ require "uploadify_rails/version"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'uploadify_rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "uploadify_rails"
8
+ gem.version = UploadifyRails::VERSION
9
+ gem.authors = ["Andrey"]
10
+ gem.email = ["railscode@gmail.com"]
11
+ gem.description = "Rails 3 multi upload with flash based Uploadify and Rails assets pipeline"
12
+ gem.summary = "Rails 3 multi upload with flash based Uploadify and Rails assets pipeline"
13
+ gem.homepage = "https://github.com/vav/uploadify_rails"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "mime-types", ">= 1.21" # :require => "mime/types"
21
+ gem.add_dependency "flash_cookie_session", ">= 1.1.4"
22
+ gem.add_dependency "nested_form", ">= 0.3.1"
23
+ gem.add_dependency "renderer", ">= 0.0.14"
24
+ end
Binary file
Binary file
Binary file