file_folder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/file_folder.gemspec +37 -0
- data/lib/file_folder/version.rb +3 -0
- data/lib/file_folder.rb +99 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d9e7369032f152f99beb507f916ddcfc2122de8d
|
4
|
+
data.tar.gz: b5dd7a043ed12855358fdc0f4d1315c5c39be437
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d885122d17df67623c1791f1abf33ce2c2f35a523fb56c29683884ce01996ba43a86c9398eccf593497a8a06967cc6bd9b75a5cb6c7efd7122056ed76461d5de
|
7
|
+
data.tar.gz: 7407d33a5db60b2ed05441d8d22ed6088cfde184c0386787866a4781c646a1db8b09e3dee2d00fa734e24220e3f3e56c947ec1dcb59a00c7650ee47a2e44ba05
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Rajeev Kannav Sharma
|
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,29 @@
|
|
1
|
+
# FileFolder
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'file_folder'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install file_folder
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/file_folder/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/file_folder.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'file_folder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "file_folder"
|
8
|
+
spec.version = FileFolder::VERSION
|
9
|
+
spec.authors = ["Rajeev Kannav Sharma"]
|
10
|
+
spec.email = ["rajeevsharma86@gmail.com"]
|
11
|
+
spec.summary = %q{
|
12
|
+
Generates file field from file_field arguments and provides methods like assignment, assignment?, assignment_path,
|
13
|
+
assignment_path_to_write, assignment_persisted?, assignment_destroy!
|
14
|
+
}
|
15
|
+
spec.description = %q{
|
16
|
+
#assignment >> Getter,
|
17
|
+
#assignment? >> Returns true if assignment is having value other than nil else false,
|
18
|
+
#assignment = >> Setter. Acceptable inputs are file upload(ActionDispatch::Http::UploadedFile), filer handle and String(format validation is ignored),
|
19
|
+
#assignment_path >> File path of assignment,
|
20
|
+
#assignment_path_to_write >> File path of assignment to write (specific to writing. Used internally),
|
21
|
+
#assignment_persisted? >> true if provided file/data is stored on disk,
|
22
|
+
#assignment_store! >> to store provided file/data on disk,
|
23
|
+
#assignment_destroy! >> to store destroy stored file/data from disk
|
24
|
+
|
25
|
+
|
26
|
+
}
|
27
|
+
spec.homepage = ""
|
28
|
+
spec.license = "MIT"
|
29
|
+
|
30
|
+
spec.files = `git ls-files -z`.split("\x0")
|
31
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
32
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
36
|
+
spec.add_development_dependency "rake"
|
37
|
+
end
|
data/lib/file_folder.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require "file_folder/version"
|
2
|
+
|
3
|
+
module FileFolder
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
attr_accessor :filefield_flag
|
10
|
+
attr_accessor :file_field
|
11
|
+
attr_accessor :file_folder_repository
|
12
|
+
attr_accessor :file_formats
|
13
|
+
|
14
|
+
#Getter to check if model is enabled with file_folder
|
15
|
+
def file_folder_enabled?
|
16
|
+
@filefield_flag.nil? ? false : @filefield_flag
|
17
|
+
end
|
18
|
+
|
19
|
+
#Generates file field from file_field arguments and provides methods like
|
20
|
+
#if file_field is "assignment" then it provides methods on top of it as
|
21
|
+
def file_folder_field(file_field, options = {})
|
22
|
+
if options.is_a?(Hash)
|
23
|
+
@file_formats ||= {}
|
24
|
+
@file_formats[file_field] = options[:formats].is_a?(Array) ? options[:formats].collect { |x| x.starts_with?(".") ? x : ".#{x}" } : [".txt", ".docx", ".jpeg", ".pdf"]
|
25
|
+
else
|
26
|
+
raise "InvalidArguments"
|
27
|
+
end
|
28
|
+
@file_field ||=[]
|
29
|
+
@file_field << file_field
|
30
|
+
send(:after_save, "#{file_field}_store!")
|
31
|
+
send(:after_destroy, "#{file_field}_destroy!")
|
32
|
+
|
33
|
+
define_method "#{file_field}" do
|
34
|
+
@ff_file_buffer.nil? ? (send("#{file_field}_persisted?") ? File.read(send("#{file_field}_path")) : nil) : @ff_file_buffer
|
35
|
+
end
|
36
|
+
|
37
|
+
define_method "#{file_field}?" do
|
38
|
+
send("#{file_field}").nil? ? false : true
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method "#{file_field}=" do |file_upload|
|
42
|
+
if file_upload.class == ActionDispatch::Http::UploadedFile
|
43
|
+
return false unless self.class.file_formats[file_field].include?(File.extname(file_upload.original_filename).downcase)
|
44
|
+
@ff_file_buffer = File.read(file_upload.path)
|
45
|
+
@ff_file_ofn = File.basename(file_upload.original_filename).gsub(/[^a-zA-Z0-9\-\._]+/, '_')
|
46
|
+
elsif file_upload.class == File
|
47
|
+
return false unless self.class.file_formats[file_field].include?(File.extname(file_upload.path).downcase)
|
48
|
+
@ff_file_buffer = file_upload.read
|
49
|
+
@ff_file_ofn = File.basename(file_upload.path).gsub(/[^a-zA-Z0-9\-\._]+/, '_')
|
50
|
+
elsif file_upload.class == String
|
51
|
+
#return false unless self.class.file_formats.include?(File.extname(file_upload).downcase)
|
52
|
+
@ff_file_buffer = file_upload
|
53
|
+
#@ff_file_ofn = File.basename(file_upload.path) #As there is nothing like original_file_name for a string :)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
define_method "#{file_field}_path" do
|
58
|
+
FileUtils.mkdir_p File.join(self.class.file_folder_repository, [id.to_s, self.class.name.underscore].join('_'), "#{self.version}_version")
|
59
|
+
end
|
60
|
+
|
61
|
+
define_method "#{file_field}_path_to_write" do
|
62
|
+
#directoy_path = FileUtils.mkdir_p File.join(self.class.file_folder_repository, self.id.to_s, file_field.to_s)
|
63
|
+
File.join(send("#{file_field}_path"), "#{self.version}V_" + @ff_file_ofn)
|
64
|
+
end
|
65
|
+
|
66
|
+
define_method "#{file_field}_persisted?" do
|
67
|
+
(@ff_file_buffer.nil? and File.file?(send("#{file_field}_path")))
|
68
|
+
end
|
69
|
+
|
70
|
+
define_method "#{file_field}_store!" do
|
71
|
+
return unless self.class.file_folder_enabled?
|
72
|
+
unless @ff_file_buffer.nil?
|
73
|
+
_path_to_write = send("#{file_field}_path_to_write")
|
74
|
+
File.delete(_path_to_write) if File.exist?(_path_to_write) #Clearing any existing existence
|
75
|
+
File.open(_path_to_write, "wb+") { |f| f.puts(@ff_file_buffer) }
|
76
|
+
@ff_file_buffer = nil
|
77
|
+
@ff_file_ofn = nil
|
78
|
+
send("#{file_field}_url=", _path_to_write)
|
79
|
+
self.try(:save_update)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
define_method "#{file_field}_destroy!" do
|
84
|
+
return unless self.class.file_folder_enabled?
|
85
|
+
@ff_file_buffer = nil
|
86
|
+
# File.delete(send("#{file_field}_path")) if File.exist?(send("#{file_field}_path"))
|
87
|
+
end
|
88
|
+
|
89
|
+
@filefield_flag = true
|
90
|
+
end
|
91
|
+
|
92
|
+
def file_folder_repository
|
93
|
+
@file_folder_repository ||= FileUtils.mkdir_p File.join(Rails.root, "file_folder", self.name.underscore.pluralize)
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
ActiveRecord::Base.send(:include, FileFolder)
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_folder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rajeev Kannav Sharma
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: "\n #assignment >> Getter,\n #assignment? >> Returns true if
|
42
|
+
assignment is having value other than nil else false,\n #assignment = >> Setter.
|
43
|
+
Acceptable inputs are file upload(ActionDispatch::Http::UploadedFile), filer handle
|
44
|
+
and String(format validation is ignored),\n #assignment_path >> File path of
|
45
|
+
assignment,\n #assignment_path_to_write >> File path of assignment to write (specific
|
46
|
+
to writing. Used internally),\n #assignment_persisted? >> true if provided file/data
|
47
|
+
is stored on disk,\n #assignment_store! >> to store provided file/data on disk,\n
|
48
|
+
\ #assignment_destroy! >> to store destroy stored file/data from disk\n\n\n "
|
49
|
+
email:
|
50
|
+
- rajeevsharma86@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- file_folder.gemspec
|
61
|
+
- lib/file_folder.rb
|
62
|
+
- lib/file_folder/version.rb
|
63
|
+
homepage: ''
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.2.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Generates file field from file_field arguments and provides methods like
|
87
|
+
assignment, assignment?, assignment_path, assignment_path_to_write, assignment_persisted?,
|
88
|
+
assignment_destroy!
|
89
|
+
test_files: []
|