jakewendt-simply_documents 1.3.4

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.
Files changed (47) hide show
  1. data/README.rdoc +70 -0
  2. data/app/controllers/documents_controller.rb +99 -0
  3. data/app/models/document.rb +28 -0
  4. data/app/views/documents/_document.html.erb +15 -0
  5. data/app/views/documents/_form.html.erb +13 -0
  6. data/app/views/documents/edit.html.erb +14 -0
  7. data/app/views/documents/index.html.erb +15 -0
  8. data/app/views/documents/new.html.erb +11 -0
  9. data/app/views/documents/preview.html.erb +17 -0
  10. data/config/document.yml +54 -0
  11. data/config/routes.rb +5 -0
  12. data/generators/simply_documents/USAGE +0 -0
  13. data/generators/simply_documents/simply_documents_generator.rb +87 -0
  14. data/generators/simply_documents/templates/autotest_simply_documents.rb +2 -0
  15. data/generators/simply_documents/templates/functional/documents_controller_test.rb +220 -0
  16. data/generators/simply_documents/templates/javascripts/documents.js +0 -0
  17. data/generators/simply_documents/templates/migrations/add_attachments_document_to_document.rb +19 -0
  18. data/generators/simply_documents/templates/migrations/create_documents.rb +19 -0
  19. data/generators/simply_documents/templates/migrations/polymorphicize_document_owner.rb +13 -0
  20. data/generators/simply_documents/templates/simply_documents.rake +6 -0
  21. data/generators/simply_documents/templates/stylesheets/documents.css +4 -0
  22. data/generators/simply_documents/templates/unit/document_test.rb +18 -0
  23. data/lib/jakewendt-simply_documents.rb +1 -0
  24. data/lib/simply_documents.rb +54 -0
  25. data/lib/simply_documents/autotest.rb +26 -0
  26. data/lib/simply_documents/factories.rb +4 -0
  27. data/lib/simply_documents/file_utils_extension.rb +18 -0
  28. data/lib/simply_documents/owner.rb +12 -0
  29. data/lib/simply_documents/tasks.rb +1 -0
  30. data/lib/simply_documents/test_tasks.rb +56 -0
  31. data/lib/tasks/application.rake +40 -0
  32. data/lib/tasks/database.rake +52 -0
  33. data/lib/tasks/documentation.rake +68 -0
  34. data/lib/tasks/rcov.rake +44 -0
  35. data/lib/tasks/simply_authorized.rake +2 -0
  36. data/lib/tasks/simply_helpful.rake +2 -0
  37. data/lib/tasks/simply_sessions.rake +5 -0
  38. data/lib/tasks/ucb_ccls_engine_tasks.rake +50 -0
  39. data/rails/init.rb +1 -0
  40. data/test/app/controllers/application_controller.rb +41 -0
  41. data/test/app/controllers/home_controller.rb +10 -0
  42. data/test/app/models/user.rb +3 -0
  43. data/test/assets/edit_save_wireframe.pdf +0 -0
  44. data/test/config/routes.rb +9 -0
  45. data/test/functional/documents/documents_controller_test.rb +222 -0
  46. data/test/unit/documents/document_test.rb +18 -0
  47. metadata +271 -0
@@ -0,0 +1,2 @@
1
+ # From `script/generate simply_documents` ...
2
+ require 'simply_documents/autotest'
@@ -0,0 +1,220 @@
1
+ #require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'test_helper'
3
+
4
+ class SimplyDocuments::DocumentsControllerTest < ActionController::TestCase
5
+ tests DocumentsController
6
+
7
+ ASSERT_ACCESS_OPTIONS = {
8
+ :model => 'Document',
9
+ :actions => [:new,:create,:edit,:update,:destroy,:index],
10
+ :method_for_create => :factory_create,
11
+ :attributes_for_create => :factory_attributes
12
+ }
13
+
14
+ def factory_create
15
+ Factory(:document)
16
+ end
17
+ def factory_attributes
18
+ Factory.attributes_for(:document)
19
+ end
20
+
21
+ assert_access_with_https
22
+ assert_access_with_login({
23
+ :logins => [:super_user,:admin,:editor]})
24
+
25
+ assert_no_access_with_http
26
+ assert_no_access_with_login({
27
+ :logins => [:interviewer,:reader,:active_user] })
28
+ assert_no_access_without_login
29
+
30
+ assert_no_access_with_login(
31
+ :attributes_for_create => nil,
32
+ :method_for_create => nil,
33
+ :actions => nil,
34
+ :suffix => " and invalid id",
35
+ :login => :superuser,
36
+ :redirect => :documents_path,
37
+ :edit => { :id => 0 },
38
+ :update => { :id => 0 },
39
+ :destroy => { :id => 0 }
40
+ )
41
+
42
+ %w( super_user admin editor ).each do |cu|
43
+
44
+
45
+ ## still only privacy filter is based on "may_maintain_pages"
46
+ ## which isn't really gonna work
47
+ # test "should get redirect to public s3 document with #{cu} login" do
48
+ # Document.any_instance.stubs(:s3_public?).returns(true)
49
+ # document = Factory(:document, :document_file_name => 'bogus_file_name')
50
+ # assert !File.exists?(document.document.path)
51
+ # login_as send(cu)
52
+ # get :show, :id => document.id
53
+ # assert_redirected_to document.document.url
54
+ # end
55
+ #
56
+ # test "should get redirect to private s3 document with #{cu} login" do
57
+ # Document.any_instance.stubs(:s3_private?).returns(true)
58
+ # document = Factory(:document, :document_file_name => 'bogus_file_name')
59
+ # assert !File.exists?(document.document.path)
60
+ # login_as send(cu)
61
+ # get :show, :id => document.id
62
+ # assert_redirected_to document.s3_url
63
+ # end
64
+
65
+ # Add may_download_document
66
+
67
+ # test "should get redirect to public s3 document with #{cu} login" do
68
+ # Document.any_instance.stubs(:s3_public?).returns(true)
69
+ # document = Factory(:document, :document_file_name => 'bogus_file_name')
70
+ # assert !File.exists?(document.document.path)
71
+ # login_as send(cu)
72
+ # get :show, :id => document.id
73
+ # assert_redirected_to document.document.url
74
+ # end
75
+
76
+ test "should get redirect to private s3 document with #{cu} login" do
77
+ Document.has_attached_file :document, {
78
+ :s3_headers => {
79
+ 'x-amz-storage-class' => 'REDUCED_REDUNDANCY' },
80
+ :s3_permissions => :private,
81
+ :storage => :s3,
82
+ :s3_protocol => 'https',
83
+ :s3_credentials => "#{Rails.root}/config/s3.yml",
84
+ :bucket => 'ccls',
85
+ :path => "documents/:id/:filename"
86
+ }
87
+
88
+ # Since the REAL S3 credentials are only in production
89
+ # Bad credentials make exists? return true????
90
+ Rails.stubs(:env).returns('production')
91
+ document = Factory(:document, :document_file_name => 'bogus_file_name')
92
+ assert !document.document.exists?
93
+ assert !File.exists?(document.document.path)
94
+
95
+ AWS::S3::S3Object.stubs(:exists?).returns(true)
96
+
97
+ login_as send(cu)
98
+ get :show, :id => document.id
99
+ assert_response :redirect
100
+ assert_match %r{\Ahttp(s)?://s3.amazonaws.com/ccls/documents/\d+/bogus_file_name\.\?AWSAccessKeyId=\w+&Expires=\d+&Signature=.+\z}, @response.redirected_to
101
+ end
102
+
103
+
104
+
105
+
106
+
107
+
108
+ test "should NOT download document with nil document and #{cu} login" do
109
+ document = Factory(:document)
110
+ assert document.document.path.blank?
111
+ login_as send(cu)
112
+ get :show, :id => document.id
113
+ assert_redirected_to preview_document_path(document)
114
+ assert_not_nil flash[:error]
115
+ end
116
+
117
+ test "should NOT download document with no document and #{cu} login" do
118
+ document = Factory(:document, :document_file_name => 'bogus_file_name')
119
+ assert !File.exists?(document.document.path)
120
+ login_as send(cu)
121
+ get :show, :id => document.id
122
+ assert_redirected_to preview_document_path(document)
123
+ assert_not_nil flash[:error]
124
+ end
125
+
126
+ test "should NOT download nonexistant document with #{cu} login" do
127
+ assert !File.exists?('some_fake_file_name.doc')
128
+ login_as send(cu)
129
+ get :show, :id => 'some_fake_file_name',:format => 'doc'
130
+ assert_redirected_to documents_path
131
+ assert_not_nil flash[:error]
132
+ end
133
+
134
+ test "should preview document with document and #{cu} login" do
135
+ document = Factory(:document)
136
+ login_as send(cu)
137
+ get :preview, :id => document.id
138
+ assert_response :success
139
+ assert_nil flash[:error]
140
+ end
141
+
142
+ test "should download document by id with document and #{cu} login" do
143
+ document = Document.create!(Factory.attributes_for(:document,
144
+ :document => File.open(File.dirname(__FILE__) +
145
+ '/../../assets/edit_save_wireframe.pdf')))
146
+ login_as send(cu)
147
+ get :show, :id => document.reload.id
148
+ assert_nil flash[:error]
149
+ assert_not_nil @response.headers['Content-disposition'].match(
150
+ /attachment;.*pdf/)
151
+ document.destroy
152
+ end
153
+
154
+ test "should download document by name with document and #{cu} login" do
155
+ document = Document.create!(Factory.attributes_for(:document,
156
+ :document => File.open(File.dirname(__FILE__) +
157
+ '/../../assets/edit_save_wireframe.pdf')))
158
+ login_as send(cu)
159
+ get :show, :id => 'edit_save_wireframe',
160
+ :format => 'pdf'
161
+ assert_nil flash[:error]
162
+ assert_not_nil @response.headers['Content-disposition'].match(
163
+ /attachment;.*pdf/)
164
+ document.destroy
165
+ end
166
+
167
+ test "should NOT create invalid document with #{cu} login" do
168
+ login_as send(cu)
169
+ assert_no_difference('Document.count') do
170
+ post :create, :document => {}
171
+ end
172
+ assert_not_nil flash[:error]
173
+ assert_template 'new'
174
+ assert_response :success
175
+ end
176
+
177
+ test "should NOT update invalid document with #{cu} login" do
178
+ login_as send(cu)
179
+ put :update, :id => Factory(:document).id,
180
+ :document => { :title => "a" }
181
+ assert_not_nil flash[:error]
182
+ assert_template 'edit'
183
+ assert_response :success
184
+ end
185
+
186
+ end
187
+
188
+ %w( interviewer reader active_user ).each do |cu|
189
+
190
+ test "should NOT preview document with #{cu} login" do
191
+ document = Factory(:document)
192
+ login_as send(cu)
193
+ get :preview, :id => document.id
194
+ assert_redirected_to root_path
195
+ assert_not_nil flash[:error]
196
+ end
197
+
198
+ test "should NOT download document with #{cu} login" do
199
+ document = Factory(:document)
200
+ login_as send(cu)
201
+ get :show, :id => document.id
202
+ assert_redirected_to root_path
203
+ assert_not_nil flash[:error]
204
+ end
205
+
206
+ end
207
+
208
+ test "should NOT preview document without login" do
209
+ document = Factory(:document)
210
+ get :preview, :id => document.id
211
+ assert_redirected_to_login
212
+ end
213
+
214
+ test "should NOT download document without login" do
215
+ document = Factory(:document)
216
+ get :show, :id => document.id
217
+ assert_redirected_to_login
218
+ end
219
+
220
+ end
@@ -0,0 +1,19 @@
1
+ class AddAttachmentsDocumentToDocument < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :documents, :document_file_name, :string
4
+ add_column :documents, :document_content_type, :string
5
+ add_column :documents, :document_file_size, :integer
6
+ add_column :documents, :document_updated_at, :datetime
7
+
8
+ add_index :documents, :document_file_name, :unique => true
9
+ end
10
+
11
+ def self.down
12
+ remove_index :documents, :document_file_name
13
+
14
+ remove_column :documents, :document_file_name
15
+ remove_column :documents, :document_content_type
16
+ remove_column :documents, :document_file_size
17
+ remove_column :documents, :document_updated_at
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ class CreateDocuments < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :documents do |t|
4
+ t.references :owner
5
+ t.string :title, :null => false
6
+ t.text :abstract
7
+ t.boolean :shared_with_all,
8
+ :default => false, :null => false
9
+ t.boolean :shared_with_select,
10
+ :default => false, :null => false
11
+ t.timestamps
12
+ end
13
+ add_index :documents, :owner_id
14
+ end
15
+
16
+ def self.down
17
+ drop_table :documents
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ class PolymorphicizeDocumentOwner < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :documents, :owner_type, :string
4
+ remove_index :documents, :owner_id
5
+ add_index :documents, [:owner_id,:owner_type]
6
+ end
7
+
8
+ def self.down
9
+ remove_index :documents, [:owner_id,:owner_type]
10
+ add_index :documents, :owner_id
11
+ remove_column :documents, :owner_type_string
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ # From `script/generate simply_documents` ...
2
+ unless Gem.source_index.find_name('jakewendt-simply_documents').empty?
3
+ gem 'jakewendt-simply_documents'
4
+ require 'simply_documents'
5
+ require 'simply_documents/test_tasks'
6
+ end
@@ -0,0 +1,4 @@
1
+ form.new_document,
2
+ form.edit_document {
3
+ display:inline-block;
4
+ }
@@ -0,0 +1,18 @@
1
+ #require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'test_helper'
3
+
4
+ class SimplyDocuments::DocumentTest < ActiveSupport::TestCase
5
+
6
+ assert_should_require(:title)
7
+ assert_should_require_attribute_length(:title, :minimum => 4)
8
+ assert_should_belong_to(:owner,:class_name => 'User')
9
+
10
+ test "should create document" do
11
+ assert_difference 'Document.count' do
12
+ object = create_object
13
+ assert !object.new_record?,
14
+ "#{object.errors.full_messages.to_sentence}"
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1 @@
1
+ require 'simply_documents'
@@ -0,0 +1,54 @@
1
+ require 'active_record'
2
+ require 'active_support'
3
+ require 'ruby_extension'
4
+ require 'simply_helpful'
5
+ require 'gravatar'
6
+ #require 'calnet_authenticated'
7
+ require 'simply_authorized'
8
+ require 'acts_as_list'
9
+ module SimplyDocuments
10
+ # predefine namespace
11
+ end
12
+ require 'simply_documents/owner'
13
+
14
+ # This doesn't seem necessary
15
+ %w{models controllers}.each do |dir|
16
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../app', dir))
17
+ ActiveSupport::Dependencies.autoload_paths << path
18
+ ActiveSupport::Dependencies.autoload_once_paths << path
19
+ end
20
+
21
+ require 'action_controller' # loads HTML
22
+ HTML::WhiteListSanitizer.allowed_attributes.merge(%w(
23
+ id class style
24
+ ))
25
+
26
+ if defined?(Rails) && Rails.env == 'test' && Rails.class_variable_defined?("@@configuration")
27
+ require 'active_support/test_case'
28
+ require 'factory_girl'
29
+ require 'simply_documents/factories'
30
+ # else
31
+ # running a rake task
32
+ end
33
+
34
+ if RUBY_PLATFORM =~ /java/i
35
+ require 'simply_documents/file_utils_extension'
36
+ end
37
+
38
+ require 'paperclip'
39
+ if defined? ::Paperclip::Glue
40
+ ActiveRecord::Base.send(:include, ::Paperclip::Glue)
41
+ else
42
+ ActiveRecord::Base.send(:include, ::Paperclip)
43
+ end
44
+
45
+ ActionController::Routing::Routes.add_configuration_file(
46
+ File.expand_path(
47
+ File.join(
48
+ File.dirname(__FILE__), '../config/routes.rb')))
49
+
50
+ ActionController::Base.view_paths <<
51
+ File.expand_path(
52
+ File.join(
53
+ File.dirname(__FILE__), '../app/views'))
54
+
@@ -0,0 +1,26 @@
1
+ class Autotest::Rails
2
+
3
+ #
4
+ # Need both the mapping and the extra files
5
+ #
6
+ def run_with_simply_documents
7
+ add_exception %r%config/%
8
+ add_exception %r%versions/%
9
+ add_exception %r%\.git/%
10
+ self.extra_files << File.expand_path(File.join(
11
+ File.dirname(__FILE__),'/../../test/unit/documents/'))
12
+
13
+ self.extra_files << File.expand_path(File.join(
14
+ File.dirname(__FILE__),'/../../test/functional/documents/'))
15
+
16
+ add_mapping(
17
+ %r{^#{File.expand_path(File.join(File.dirname(__FILE__),'/../../test/'))}/(unit|functional)/documents/.*_test\.rb$}
18
+ ) do |filename, _|
19
+ filename
20
+ end
21
+ run_without_simply_documents
22
+ end
23
+ alias_method_chain :run, :simply_documents
24
+
25
+
26
+ end
@@ -0,0 +1,4 @@
1
+ Factory.define :document do |f|
2
+ f.sequence(:title) { |n| "Title#{n}" }
3
+ # f.sequence(:document_file_name) { |n| "document_file_name#{n}" }
4
+ end
@@ -0,0 +1,18 @@
1
+ # Needed for Paperclip gem 2.3.3
2
+ # http://jira.codehaus.org/browse/JRUBY-3381
3
+ # http://github.com/thoughtbot/paperclip/issues/issue/193
4
+ # Errno::EACCES: Permission denied - /var/folders/kV/kV5XVPtqE9uZBCjn3z6vmk+++TM/-Tmp-/stream,19661,34729.pdf or /Users/jakewendt/github_repo/jakewendt/ucb_ccls_buffler/development/documents/2/edit_save_wireframe.pdf
5
+ FileUtils.module_eval do
6
+ class << self
7
+ alias_method :built_in_mv, :mv
8
+
9
+ def mv(src, dest, options = {})
10
+ begin
11
+ built_in_mv(src, dest, options)
12
+ rescue Errno::EACCES
13
+ cp(src, dest)
14
+ rm(src)
15
+ end
16
+ end
17
+ end
18
+ end unless FileUtils.methods.include?('built_in_mv')
@@ -0,0 +1,12 @@
1
+ module SimplyDocuments::Owner
2
+ def self.included(base)
3
+ base.extend(PrepMethod)
4
+ end
5
+ module PrepMethod
6
+ def document_owner(*args)
7
+ options = args.extract_options!
8
+ has_many :documents, :as => :owner
9
+ end
10
+ end
11
+ end
12
+ ActiveRecord::Base.send(:include,SimplyDocuments::Owner)
@@ -0,0 +1 @@
1
+ Dir["#{File.dirname(__FILE__)}/../tasks/**/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,56 @@
1
+ module SimplyDocuments;end
2
+ namespace :test do
3
+ namespace :units do
4
+ Rake::TestTask.new(:simply_documents => "db:test:prepare") do |t|
5
+ t.pattern = File.expand_path(File.join(
6
+ File.dirname(__FILE__),'/../../test/unit/documents/*_test.rb'))
7
+ t.libs << "test"
8
+ t.verbose = true
9
+ end
10
+ end
11
+ namespace :functionals do
12
+ Rake::TestTask.new(:simply_documents => "db:test:prepare") do |t|
13
+ # t.pattern = File.expand_path(File.join(
14
+ # File.dirname(__FILE__),'/../../test/functional/documents/*_test.rb'))
15
+ t.libs << "test"
16
+ t.verbose = true
17
+ # this enables user override
18
+ # used to work without this, but now it doesn't???!?!?
19
+ t.test_files = [
20
+ File.expand_path(File.join(File.dirname(__FILE__),
21
+ '/../../test/functional/documents/*_test.rb')),
22
+ File.expand_path(File.join(Rails.root,
23
+ '/test/functional/documents/*_test.rb'))
24
+ ]
25
+ end
26
+ end
27
+ end
28
+ Rake::Task['test:functionals'].prerequisites.unshift(
29
+ "test:functionals:simply_documents" )
30
+ Rake::Task['test:units'].prerequisites.unshift(
31
+ "test:units:simply_documents" )
32
+
33
+ # I thought of possibly just including this file
34
+ # but that would make __FILE__ different.
35
+ # Hmmm
36
+
37
+ #
38
+ # used in simply_helpful's rake test:coverage to run gem's
39
+ # tests in the context of the application
40
+ #
41
+ @gem_test_dirs ||= []
42
+ #@gem_test_dirs << File.expand_path(File.join(File.dirname(__FILE__),
43
+ # '/../../test/unit/documents/'))
44
+ #@gem_test_dirs << File.expand_path(File.join(File.dirname(__FILE__),
45
+ # '/../../test/functional/documents/'))
46
+
47
+ #
48
+ # More flexible. Find all test files, pick out their dir, uniq 'em and add.
49
+ #
50
+ Dir.glob( File.expand_path(File.join(File.dirname(__FILE__),
51
+ '/../../test/*/documents/*_test.rb'))
52
+ ).collect{|f|
53
+ File.dirname(f)
54
+ }.uniq.each{ |dir|
55
+ @gem_test_dirs << dir
56
+ }