importable_attachments 0.0.13
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/.gitignore +24 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +38 -0
- data/MIT-LICENSE +20 -0
- data/README.md +8 -0
- data/README.rdoc +3 -0
- data/Rakefile +29 -0
- data/app/assets/images/importable_attachments/.gitkeep +0 -0
- data/app/assets/images/importable_attachments/buttons/.htaccess +5 -0
- data/app/assets/images/importable_attachments/buttons/download_32.png +0 -0
- data/app/assets/images/importable_attachments/buttons/upload_32.png +0 -0
- data/app/assets/javascripts/importable_attachments/application.js +14 -0
- data/app/assets/javascripts/importable_attachments/attachments.coffee +41 -0
- data/app/assets/stylesheets/importable_attachments/application.css +14 -0
- data/app/assets/stylesheets/importable_attachments/attachments.css +4 -0
- data/app/assets/stylesheets/scaffold.css +56 -0
- data/app/controllers/importable_attachments/application_controller.rb +4 -0
- data/app/controllers/importable_attachments/attachments_controller.rb +190 -0
- data/app/controllers/importable_attachments/versions_controller.rb +87 -0
- data/app/helpers/importable_attachments/application_helper.rb +4 -0
- data/app/models/attachment.rb +24 -0
- data/app/models/importable_attachments/attachment.rb +143 -0
- data/app/models/importable_attachments/version.rb +50 -0
- data/app/validators/existing_class_validator.rb +17 -0
- data/app/validators/importable_attachments/csv_validator.rb +36 -0
- data/app/validators/importable_attachments/excel.rb +18 -0
- data/app/validators/importable_attachments/excel_validator.rb +18 -0
- data/app/views/importable_attachments/attachments/_attachment.html.haml +9 -0
- data/app/views/importable_attachments/attachments/_form.html.haml +22 -0
- data/app/views/importable_attachments/attachments/_nested_form.html.haml +20 -0
- data/app/views/importable_attachments/attachments/edit.html.haml +39 -0
- data/app/views/importable_attachments/attachments/index.html.haml +23 -0
- data/app/views/importable_attachments/attachments/index.xml.builder +23 -0
- data/app/views/importable_attachments/attachments/new.html.haml +10 -0
- data/app/views/importable_attachments/attachments/show.html.haml +43 -0
- data/app/views/importable_attachments/versions/_form.html.haml +25 -0
- data/app/views/importable_attachments/versions/edit.html.haml +7 -0
- data/app/views/importable_attachments/versions/index.html.haml +27 -0
- data/app/views/importable_attachments/versions/new.html.haml +5 -0
- data/app/views/importable_attachments/versions/show.html.haml +21 -0
- data/app/views/layouts/_version.html.haml +33 -0
- data/app/views/layouts/importable_attachments/application.html.haml +48 -0
- data/bin/set_lc.sh +47 -0
- data/config/database.yml +25 -0
- data/config/features/attachments.rb +8 -0
- data/config/features/mark_requirements.rb +3 -0
- data/config/features/smarter_dates.rb +3 -0
- data/config/features/versioning.rb +7 -0
- data/config/initializers/0_configuration.rb +7 -0
- data/config/initializers/formtastic.rb +76 -0
- data/config/initializers/generators.rb +10 -0
- data/config/initializers/paperclip.rb +27 -0
- data/config/locales/responders.en.yml +10 -0
- data/config/routes.rb +11 -0
- data/db/migrate/001_create_importable_attachments_versions.rb +14 -0
- data/db/migrate/100_create_attachments.rb +19 -0
- data/importable_attachments.gemspec +81 -0
- data/lib/generators/importable_attachments/install_generator.rb +66 -0
- data/lib/generators/importable_attachments/templates/features/attachments.rb.erb +7 -0
- data/lib/generators/importable_attachments/templates/features/versioning.rb +7 -0
- data/lib/generators/importable_attachments/templates/initializers/paperclip.rb +27 -0
- data/lib/importable_attachments/base.rb +108 -0
- data/lib/importable_attachments/blueprints.rb +9 -0
- data/lib/importable_attachments/engine.rb +8 -0
- data/lib/importable_attachments/importers/csv.rb +208 -0
- data/lib/importable_attachments/importers/excel.rb +37 -0
- data/lib/importable_attachments/importers.rb +7 -0
- data/lib/importable_attachments/version.rb +3 -0
- data/lib/importable_attachments.rb +9 -0
- data/lib/paperclip_processors/save_upload.rb +33 -0
- data/lib/tasks/importable_attachments_tasks.rake +4 -0
- data/script/rails +8 -0
- data/spec/attachments/books.csv +6 -0
- data/spec/attachments/books.txt +6 -0
- data/spec/attachments/books2.csv +4 -0
- data/spec/attachments/empty.csv +0 -0
- data/spec/attachments/failed_instances.csv +3 -0
- data/spec/attachments/invalid_headers.csv +3 -0
- data/spec/attachments/just_headers.csv +1 -0
- data/spec/attachments/mostly_empty.csv +2 -0
- data/spec/attachments/mostly_empty_copy.xls +0 -0
- data/spec/controllers/importable_attachments/attachments_controller_spec.rb +236 -0
- data/spec/controllers/importable_attachments/versions_controller_spec.rb +158 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/book.rb +13 -0
- data/spec/dummy/app/models/library.rb +54 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +65 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/0_configuration.rb +7 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/101_create_libraries.rb +10 -0
- data/spec/dummy/db/migrate/102_create_books.rb +12 -0
- data/spec/dummy/db/schema.rb +57 -0
- data/spec/dummy/features/person_uploads_generic_file.feature +11 -0
- data/spec/dummy/features/step_definitions/person_uploads_generic_file_steps.rb +11 -0
- data/spec/dummy/features/step_definitions/web_steps.rb +211 -0
- data/spec/dummy/features/support/capybara.rb +6 -0
- data/spec/dummy/features/support/database_cleaner.rb +26 -0
- data/spec/dummy/features/support/developer_helpers.rb +47 -0
- data/spec/dummy/features/support/env.rb +53 -0
- data/spec/dummy/features/support/paths.rb +33 -0
- data/spec/dummy/features/support/poltergeist.rb +1 -0
- data/spec/dummy/features/support/selectors.rb +39 -0
- data/spec/dummy/features/support/transactional_fixtures.rb +14 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +38 -0
- data/spec/dummy/public/422.html +38 -0
- data/spec/dummy/public/500.html +36 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/support/.gitkeep +0 -0
- data/spec/dummy/spec/support/paperclip.rb +1 -0
- data/spec/models/importable_attachments/attachment_spec.rb +177 -0
- data/spec/models/importable_attachments/library_spec.rb +155 -0
- data/spec/models/importable_attachments/version_spec.rb +25 -0
- data/spec/routing/importable_attachments/versions_routing_spec.rb +43 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +30 -0
- metadata +737 -0
data/.gitignore
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
**.orig
|
|
2
|
+
*.rbc
|
|
3
|
+
*.sassc
|
|
4
|
+
.rspec
|
|
5
|
+
.rvmrc
|
|
6
|
+
.sass-cache?
|
|
7
|
+
/.bundle
|
|
8
|
+
/coverage/
|
|
9
|
+
/db/*.sqlite3
|
|
10
|
+
/log/*
|
|
11
|
+
/public/system/*
|
|
12
|
+
/spec/dummy/.sass-cache
|
|
13
|
+
/spec/dummy/db/*.sqlite3
|
|
14
|
+
/spec/dummy/log/*.log
|
|
15
|
+
/spec/dummy/tmp/
|
|
16
|
+
/spec/dummy/public/system/
|
|
17
|
+
/spec/tmp/*
|
|
18
|
+
/tmp/*
|
|
19
|
+
/vendor/bundle
|
|
20
|
+
Gemfile.lock
|
|
21
|
+
capybara-*.html
|
|
22
|
+
pickle-email-*.html
|
|
23
|
+
pkg/
|
|
24
|
+
rerun.txt
|
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
importable_attachments
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-1.9.3-p392-patched
|
data/Gemfile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
source 'http://rubygems.org'
|
|
2
|
+
# Declare any dependencies that are still in development here instead of in
|
|
3
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
|
4
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
|
5
|
+
# your gem to rubygems.org.
|
|
6
|
+
|
|
7
|
+
# Declare your gem's dependencies in importable_attachments.gemspec.
|
|
8
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
|
9
|
+
# development dependencies will be added by default to the :development group.
|
|
10
|
+
gemspec
|
|
11
|
+
|
|
12
|
+
gem 'rubygems-bundler'
|
|
13
|
+
|
|
14
|
+
# jquery-rails is used by the dummy application
|
|
15
|
+
gem 'jquery-rails'
|
|
16
|
+
|
|
17
|
+
# needed by the models (to test against the dummy app)
|
|
18
|
+
gem 'configuration'
|
|
19
|
+
gem 'smarter_dates'
|
|
20
|
+
gem 'rails-mark_requirements'
|
|
21
|
+
gem 'rails-alpha_numeric_validator'
|
|
22
|
+
gem 'ruby-filemagic'
|
|
23
|
+
|
|
24
|
+
# needed by the controllers (to test against the dummy app)
|
|
25
|
+
gem 'haml-rails'
|
|
26
|
+
gem 'formtastic'
|
|
27
|
+
|
|
28
|
+
group :development, :test do
|
|
29
|
+
gem 'debugger'
|
|
30
|
+
|
|
31
|
+
# needed by the models simply to run rspec
|
|
32
|
+
gem 'valid_attribute'
|
|
33
|
+
gem 'machinist'
|
|
34
|
+
gem 'rspec-paper_trail'
|
|
35
|
+
gem 'cucumber-rails', require: false
|
|
36
|
+
gem 'capybara', '< 2.0.0'
|
|
37
|
+
gem 'poltergeist', '< 1.1.0', require: 'capybara/poltergeist'
|
|
38
|
+
end
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2013 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
data/README.rdoc
ADDED
data/Rakefile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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 = 'ImportableAttachments'
|
|
18
|
+
rdoc.options << '--line-numbers'
|
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
|
|
24
|
+
load 'rails/tasks/engine.rake'
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
Bundler::GemHelper.install_tasks
|
|
29
|
+
|
|
File without changes
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
|
2
|
+
// listed below.
|
|
3
|
+
//
|
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
|
6
|
+
//
|
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
|
8
|
+
// the compiled file.
|
|
9
|
+
//
|
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
|
12
|
+
//
|
|
13
|
+
//= require jquery
|
|
14
|
+
//= require jquery_ujs
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class @ProjectPage
|
|
2
|
+
download_icon: ->
|
|
3
|
+
jQuery('#icon_file_download')
|
|
4
|
+
|
|
5
|
+
upload_icon: ->
|
|
6
|
+
jQuery('#icon_file_upload')
|
|
7
|
+
|
|
8
|
+
input_file_field: ->
|
|
9
|
+
jQuery('#attachment_attributes_io_stream_fields input[id$=_io_stream][type=file]')
|
|
10
|
+
|
|
11
|
+
upload_form: ->
|
|
12
|
+
jQuery('#contents form:first')
|
|
13
|
+
|
|
14
|
+
file_name_label: ->
|
|
15
|
+
jQuery('#file_name')
|
|
16
|
+
|
|
17
|
+
hide_upload_field: ->
|
|
18
|
+
jQuery('#attachment_attributes_io_stream_fields li[id$=_io_stream_input]').addClass('visually_hidden')
|
|
19
|
+
|
|
20
|
+
bind_upload_icon_to_file_field: ->
|
|
21
|
+
@upload_icon().click ->
|
|
22
|
+
page = new ProjectPage
|
|
23
|
+
file_field = page.input_file_field()
|
|
24
|
+
file_field.click()
|
|
25
|
+
file_field.change ->
|
|
26
|
+
page.file_name_label().text(jQuery(@).val().replace("C:\\fakepath\\", ""))
|
|
27
|
+
page.upload_form().find('input[type=submit]').click()
|
|
28
|
+
|
|
29
|
+
drop_new_form_faux_link: ->
|
|
30
|
+
@download_icon().removeClass 'faux_link' if @upload_form().attr('id') is 'new_attachment'
|
|
31
|
+
|
|
32
|
+
hide_new_form_download_icon: ->
|
|
33
|
+
@download_icon().hide() if @upload_form().attr('id') is 'new_attachment'
|
|
34
|
+
|
|
35
|
+
jQuery(document).ready ->
|
|
36
|
+
page = new ProjectPage
|
|
37
|
+
page.hide_upload_field()
|
|
38
|
+
page.bind_upload_icon_to_file_field()
|
|
39
|
+
page.drop_new_form_faux_link()
|
|
40
|
+
page.hide_new_form_download_icon()
|
|
41
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
|
3
|
+
* listed below.
|
|
4
|
+
*
|
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
|
7
|
+
*
|
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
|
10
|
+
*
|
|
11
|
+
*= require formtastic
|
|
12
|
+
*= require importable_attachments/attachments
|
|
13
|
+
*= require_self
|
|
14
|
+
*/
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
body { background-color: #fff; color: #333; }
|
|
2
|
+
|
|
3
|
+
body, p, ol, ul, td {
|
|
4
|
+
font-family: verdana, arial, helvetica, sans-serif;
|
|
5
|
+
font-size: 13px;
|
|
6
|
+
line-height: 18px;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
pre {
|
|
10
|
+
background-color: #eee;
|
|
11
|
+
padding: 10px;
|
|
12
|
+
font-size: 11px;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
a { color: #000; }
|
|
16
|
+
a:visited { color: #666; }
|
|
17
|
+
a:hover { color: #fff; background-color:#000; }
|
|
18
|
+
|
|
19
|
+
div.field, div.actions {
|
|
20
|
+
margin-bottom: 10px;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
#notice {
|
|
24
|
+
color: green;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.field_with_errors {
|
|
28
|
+
padding: 2px;
|
|
29
|
+
background-color: red;
|
|
30
|
+
display: table;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#error_explanation {
|
|
34
|
+
width: 450px;
|
|
35
|
+
border: 2px solid red;
|
|
36
|
+
padding: 7px;
|
|
37
|
+
padding-bottom: 0;
|
|
38
|
+
margin-bottom: 20px;
|
|
39
|
+
background-color: #f0f0f0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#error_explanation h2 {
|
|
43
|
+
text-align: left;
|
|
44
|
+
font-weight: bold;
|
|
45
|
+
padding: 5px 5px 5px 15px;
|
|
46
|
+
font-size: 12px;
|
|
47
|
+
margin: -7px;
|
|
48
|
+
margin-bottom: 0px;
|
|
49
|
+
background-color: #c00;
|
|
50
|
+
color: #fff;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#error_explanation ul li {
|
|
54
|
+
font-size: 12px;
|
|
55
|
+
list-style: square;
|
|
56
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# This controller uses Representational State Transfer (REST) principles
|
|
2
|
+
# http://en.wikipedia.org/wiki/REST
|
|
3
|
+
module ImportableAttachments
|
|
4
|
+
class AttachmentsController < ApplicationController
|
|
5
|
+
respond_to :html, :json
|
|
6
|
+
before_filter :find_parent_by_id, :only => [:show, :edit, :update, :destroy, :download]
|
|
7
|
+
before_filter :find_all_parents, :only => [:index]
|
|
8
|
+
before_filter :generate_parent, :only => [:new]
|
|
9
|
+
before_filter :set_headers, :only => [:show]
|
|
10
|
+
before_filter :respond_with_instance, :only => [:new, :show, :edit]
|
|
11
|
+
|
|
12
|
+
# GET /attachments
|
|
13
|
+
def index
|
|
14
|
+
respond_with @attachments
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# GET /attachments/new
|
|
18
|
+
def new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# GET /attachments/:id
|
|
22
|
+
def show
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def download
|
|
26
|
+
send_data(File.read(stream_path), send_file_options)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# GET /attachments/:id/edit
|
|
30
|
+
def edit
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# To debug:
|
|
34
|
+
# require 'mechanize'
|
|
35
|
+
# attachment = Attachment.first
|
|
36
|
+
# file_name = 'to_import.xls'
|
|
37
|
+
# spec_file = Rails.root.join('spec', 'attachments', file_name)
|
|
38
|
+
# post_path = "/attachments/:id"
|
|
39
|
+
# get_from = "/attachments"
|
|
40
|
+
#
|
|
41
|
+
# post_path.sub!(/:id/,file_name)
|
|
42
|
+
# post_to = URI.parse("http://localhost/#{post_path}")
|
|
43
|
+
#
|
|
44
|
+
# page_form = 'new_attachment_form'
|
|
45
|
+
# page_field = 'attachment[foo]'
|
|
46
|
+
#
|
|
47
|
+
# agent = Mechanize.new
|
|
48
|
+
# page = agent.get("http://localhost/#{get_path.sub(/^\//,'')}")
|
|
49
|
+
# form = page.form(page_form)
|
|
50
|
+
# form.file_upload(:name => page_field).file_name = spec_file
|
|
51
|
+
# result = agent.submit(form)
|
|
52
|
+
|
|
53
|
+
# POST /attachments/:id
|
|
54
|
+
def create
|
|
55
|
+
@attachment = Attachment.new params[:attachment]
|
|
56
|
+
if @attachment.save
|
|
57
|
+
redirect_to attachments_path, :notice => 'Successfully created attachment.'
|
|
58
|
+
else
|
|
59
|
+
render :new
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# PUT /attachments/:id
|
|
64
|
+
def update
|
|
65
|
+
if @attachment.update_attributes params[:attachment]
|
|
66
|
+
redirect_to attachments_path, :notice => 'Successfully updated attachment.'
|
|
67
|
+
else
|
|
68
|
+
render :edit
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# DELETE /attachment/:id
|
|
73
|
+
def destroy
|
|
74
|
+
@attachment.destroy
|
|
75
|
+
redirect_to attachments_path, :notice => 'Successfully destroyed attachment.'
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
protected
|
|
79
|
+
|
|
80
|
+
# :call-seq:
|
|
81
|
+
# find_parent_by_id :id
|
|
82
|
+
#
|
|
83
|
+
# yields an attachment by its :id
|
|
84
|
+
|
|
85
|
+
def find_parent_by_id
|
|
86
|
+
@attachment = Attachment.find(params[:id])
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# :call-seq:
|
|
90
|
+
# find_all_parents
|
|
91
|
+
#
|
|
92
|
+
# yields attachments ordered by :io_stream_updated_at
|
|
93
|
+
|
|
94
|
+
def find_all_parents
|
|
95
|
+
@attachments = Attachment.order(:io_stream_updated_at)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# :call-seq:
|
|
99
|
+
# generate_parent
|
|
100
|
+
#
|
|
101
|
+
# yields a new attachment
|
|
102
|
+
|
|
103
|
+
def generate_parent
|
|
104
|
+
@attachment = Attachment.new
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# :call-seq:
|
|
108
|
+
# respond_with_instance
|
|
109
|
+
#
|
|
110
|
+
# responds_with :attachment
|
|
111
|
+
|
|
112
|
+
def respond_with_instance
|
|
113
|
+
respond_with @attachment
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# :call-seq:
|
|
117
|
+
# stream_path
|
|
118
|
+
#
|
|
119
|
+
# yields the file-streams file-system path, including Rails.root
|
|
120
|
+
|
|
121
|
+
def stream_path
|
|
122
|
+
@attachment.io_stream.path(params[:style] || :original)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# :call-seq:
|
|
126
|
+
# set_headers
|
|
127
|
+
#
|
|
128
|
+
# ensure the attachment file exists and is readable
|
|
129
|
+
|
|
130
|
+
def set_headers
|
|
131
|
+
not_found = set_not_found_header
|
|
132
|
+
no_bits = set_permission_denied_header
|
|
133
|
+
is_extinct = set_not_exist_header
|
|
134
|
+
return false if not_found || no_bits || is_extinct
|
|
135
|
+
true
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def set_not_found_header
|
|
139
|
+
return unless @attachment.nil?
|
|
140
|
+
logger.info "attachments.id = (#{params[:id]}) not found"
|
|
141
|
+
head(:not_found)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def set_not_exist_header
|
|
145
|
+
return unless File.exist? stream_path
|
|
146
|
+
logger.info "file not found: #{stream_path}"
|
|
147
|
+
head(:bad_request)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def set_permission_denied_header
|
|
151
|
+
return unless File.readable? stream_path
|
|
152
|
+
logger.info "permission denied: #{stream_path}"
|
|
153
|
+
head(:forbidden)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# :call-seq:
|
|
157
|
+
# send_file_options
|
|
158
|
+
#
|
|
159
|
+
# yields options suitable for send_file() e.g. x_sendfile headers
|
|
160
|
+
|
|
161
|
+
def send_file_options
|
|
162
|
+
st = stream_path
|
|
163
|
+
opts = {type: MIME::Types.type_for(st).to_s, filename: @attachment.io_stream_file_name}
|
|
164
|
+
|
|
165
|
+
case send_file_method
|
|
166
|
+
when :apache then
|
|
167
|
+
opts[:x_sendfile] = true
|
|
168
|
+
when :nginx then
|
|
169
|
+
head(:x_accel_redirect => st.gsub(Rails.root, ""), :content_type => opts[:type])
|
|
170
|
+
else
|
|
171
|
+
true
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
opts
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# :call-seq:
|
|
178
|
+
# send_file_method
|
|
179
|
+
#
|
|
180
|
+
# detect apache / nginx
|
|
181
|
+
#
|
|
182
|
+
# TODO: implement me
|
|
183
|
+
|
|
184
|
+
def send_file_method
|
|
185
|
+
:default
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require_dependency "importable_attachments/application_controller"
|
|
2
|
+
|
|
3
|
+
module ImportableAttachments
|
|
4
|
+
class VersionsController < ApplicationController
|
|
5
|
+
# GET /versions
|
|
6
|
+
# GET /versions.json
|
|
7
|
+
def index
|
|
8
|
+
@versions = Version.all
|
|
9
|
+
|
|
10
|
+
respond_to do |format|
|
|
11
|
+
format.html # index.html.erb
|
|
12
|
+
format.json { render json: @versions }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# GET /versions/1
|
|
17
|
+
# GET /versions/1.json
|
|
18
|
+
def show
|
|
19
|
+
@version = Version.find(params[:id])
|
|
20
|
+
|
|
21
|
+
respond_to do |format|
|
|
22
|
+
format.html # show.html.erb
|
|
23
|
+
format.json { render json: @version }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# GET /versions/new
|
|
28
|
+
# GET /versions/new.json
|
|
29
|
+
def new
|
|
30
|
+
@version = Version.new
|
|
31
|
+
|
|
32
|
+
respond_to do |format|
|
|
33
|
+
format.html # new.html.erb
|
|
34
|
+
format.json { render json: @version }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# GET /versions/1/edit
|
|
39
|
+
def edit
|
|
40
|
+
@version = Version.find(params[:id])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# POST /versions
|
|
44
|
+
# POST /versions.json
|
|
45
|
+
def create
|
|
46
|
+
@version = Version.new(params[:version])
|
|
47
|
+
|
|
48
|
+
respond_to do |format|
|
|
49
|
+
if @version.save
|
|
50
|
+
format.html { redirect_to @version, notice: 'Version was successfully created.' }
|
|
51
|
+
format.json { render json: @version, status: :created, location: @version }
|
|
52
|
+
else
|
|
53
|
+
format.html { render action: "new" }
|
|
54
|
+
format.json { render json: @version.errors, status: :unprocessable_entity }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# PUT /versions/1
|
|
60
|
+
# PUT /versions/1.json
|
|
61
|
+
def update
|
|
62
|
+
@version = Version.find(params[:id])
|
|
63
|
+
|
|
64
|
+
respond_to do |format|
|
|
65
|
+
if @version.update_attributes(params[:version])
|
|
66
|
+
format.html { redirect_to @version, notice: 'Version was successfully updated.' }
|
|
67
|
+
format.json { head :no_content }
|
|
68
|
+
else
|
|
69
|
+
format.html { render action: "edit" }
|
|
70
|
+
format.json { render json: @version.errors, status: :unprocessable_entity }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# DELETE /versions/1
|
|
76
|
+
# DELETE /versions/1.json
|
|
77
|
+
def destroy
|
|
78
|
+
@version = Version.find(params[:id])
|
|
79
|
+
@version.destroy
|
|
80
|
+
|
|
81
|
+
respond_to do |format|
|
|
82
|
+
format.html { redirect_to versions_url }
|
|
83
|
+
format.json { head :no_content }
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# An attachment represents a file within the system.
|
|
2
|
+
class Attachment < ImportableAttachments::Attachment
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
# == Schema Information
|
|
6
|
+
#
|
|
7
|
+
# Table name: attachments
|
|
8
|
+
#
|
|
9
|
+
# id :integer not null, primary key
|
|
10
|
+
# attachable_type :string(255)
|
|
11
|
+
# attachable_id :string(255)
|
|
12
|
+
# io_stream_file_name :string(255)
|
|
13
|
+
# io_stream_content_type :string(255)
|
|
14
|
+
# io_stream_file_size :integer
|
|
15
|
+
# io_stream_updated_at :datetime
|
|
16
|
+
# created_at :datetime not null
|
|
17
|
+
# updated_at :datetime not null
|
|
18
|
+
#
|
|
19
|
+
# Indexes
|
|
20
|
+
#
|
|
21
|
+
# idx_importable_attachments_on_attachable_type_and_id (attachable_type,attachable_id)
|
|
22
|
+
# index_attachments_on_io_stream_file_name (io_stream_file_name)
|
|
23
|
+
#
|
|
24
|
+
|