jakewendt-simply_documents 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
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,40 @@
1
+ namespace :app do
2
+
3
+ # task :args_as_array do
4
+ # args = $*.dup.slice(1..-1)
5
+ # puts args.collect {|arg| "X:" << arg }.join("\n")
6
+ # exit
7
+ # end
8
+
9
+ desc "Add some expected users."
10
+ task :add_users => :environment do
11
+ puts "Adding users"
12
+ %w( 859908 228181 214766 180918 66458 808 768475
13
+ 10883 86094 754783 769067 854720 16647 ).each do |uid|
14
+ puts " - Adding user with uid:#{uid}:"
15
+ User.find_create_and_update_by_uid(uid)
16
+ end
17
+ end
18
+
19
+ desc "Deputize user by UID"
20
+ task :deputize => :environment do
21
+ puts
22
+ if ENV['uid'].blank?
23
+ puts "User's CalNet UID required."
24
+ puts "Usage: rake #{$*} uid=INTEGER"
25
+ puts
26
+ exit
27
+ end
28
+ if !User.exists?(:uid => ENV['uid'])
29
+ puts "No user found with uid=#{ENV['uid']}."
30
+ puts
31
+ exit
32
+ end
33
+ user = User.find(:first, :conditions => { :uid => ENV['uid'] })
34
+ puts "Found user #{user.displayname}. Deputizing..."
35
+ user.deputize
36
+ puts "User deputized: #{user.is_administrator?}"
37
+ puts
38
+ end
39
+
40
+ end
@@ -0,0 +1,52 @@
1
+ namespace :db do
2
+
3
+ desc "Create yml fixtures for given model in database\n" <<
4
+ "rake db:extract_fixtures_from pages"
5
+ task :extract_fixtures_from => :environment do
6
+ me = $*.shift
7
+ while( table_name = $*.shift )
8
+ File.open("#{RAILS_ROOT}/db/#{table_name}.yml", 'w') do |file|
9
+ data = table_name.singularize.capitalize.constantize.find(
10
+ :all).collect(&:attributes)
11
+ file.write data.inject({}) { |hash, record|
12
+ record.delete('created_at')
13
+ record.delete('updated_at')
14
+ hash["#{table_name}_#{record['id']}"] = record
15
+ hash
16
+ }.to_yaml
17
+ end
18
+ end
19
+ exit
20
+ end
21
+
22
+ desc "Dump MYSQL table descriptions."
23
+ task :describe => :environment do
24
+ puts
25
+ puts "FYI: This task ONLY works on MYSQL databases."
26
+ puts
27
+ config = ActiveRecord::Base.connection.instance_variable_get(:@config)
28
+ #=> {:adapter=>"mysql", :host=>"localhost", :password=>nil, :username=>"root", :database=>"my_development", :encoding=>"utf8"}
29
+
30
+ tables = ActiveRecord::Base.connection.execute('show tables;')
31
+ while( table = tables.fetch_row ) do
32
+ puts "Table: #{table}"
33
+
34
+ # may have to include host and port
35
+ system("mysql --table=true " <<
36
+ "--user=#{config[:username]} " <<
37
+ "--password='#{config[:password]}' " <<
38
+ "--execute='describe #{table}' " <<
39
+ config[:database]);
40
+
41
+ #
42
+ # mysql formats the table well so doing it by hand is something that
43
+ # will have to wait until I feel like wasting my time
44
+ #
45
+ # columns = ActiveRecord::Base.connection.execute("describe #{table};")
46
+ # while( column = columns.fetch_hash ) do
47
+ # puts column.keys Extra Default Null Type Field Key
48
+ # end
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,68 @@
1
+ #
2
+ # This file has been copied from rails
3
+ # .../rails-2.3.5/lib/tasks/documentation.rake
4
+ # so that parts of it could be modified.
5
+
6
+ namespace :doc do |doc|
7
+
8
+ # Rake::RDocTask.new("app") { |rdoc|
9
+ #
10
+ # We cannot overwrite or override an RDoc rake task.
11
+ # Redefining it here actually creates another
12
+ # of the same name and both are run when
13
+ # `rake doc:app` is called. The Rakefile
14
+ # is modified to handle the modifications.
15
+ #
16
+ # Actually, that's not entirely true. This would
17
+ # add another task, but you can remove and override
18
+ # a task. The rdoc_rails plugin was overriding my
19
+ # override, which caused all the frustration!!!
20
+ #
21
+ # }
22
+
23
+ plugins = FileList['vendor/plugins/**'].collect { |plugin|
24
+ File.basename(plugin) }
25
+
26
+ namespace :plugins do
27
+ # Define doc tasks for each plugin
28
+ plugins.each do |plugin|
29
+
30
+ # clear rails' Rake::Task of the same name
31
+ Rake::Task[plugin].clear_actions
32
+ Rake::Task[plugin].clear_prerequisites
33
+
34
+ Rake::RDocTask.new(plugin) { |rdoc|
35
+ plugin_base = "vendor/plugins/#{plugin}"
36
+ ENV['format'] ||= 'railsfish'
37
+ rdoc.rdoc_dir = "doc/plugins/#{plugin}"
38
+ rdoc.template = ENV['template'] if ENV['template']
39
+ rdoc.title = "#{plugin.titlecase} Plugin Documentation"
40
+ rdoc.options << '--line-numbers' << '--inline-source'
41
+ rdoc.options << '--charset' << 'utf-8'
42
+ rdoc.options << '--format' << ENV['format']
43
+ rdoc.rdoc_files.include("#{plugin_base}/lib/**/*.rb")
44
+ rdoc.rdoc_files.include("#{plugin_base}/app/**/*.rb")
45
+
46
+ %w( README README.rdoc ).each do |readme|
47
+ if File.exist?("#{plugin_base}/#{readme}")
48
+ rdoc.main = "#{plugin_base}/#{readme}"
49
+ break
50
+ end
51
+ end
52
+ %w( TODO.org MIT-LICENSE LICENSE CHANGELOG README README.rdoc ).each do |possible_file|
53
+ if File.exist?("#{plugin_base}/#{possible_file}")
54
+ rdoc.rdoc_files.include("#{plugin_base}/#{possible_file}")
55
+ end
56
+ end
57
+ }
58
+
59
+ end
60
+ end
61
+
62
+ task :parse_readme => :environment do
63
+ require 'rdoc/markup/to_html'
64
+ h = RDoc::Markup::ToHtml.new
65
+ puts h.convert( File.read('README.rdoc') )
66
+ end
67
+
68
+ end
@@ -0,0 +1,44 @@
1
+ #
2
+ # This is from Advanced Rails Recipes, page 277
3
+ #
4
+
5
+ # TODO use the version in simply_helpful and delete this
6
+
7
+ #namespace :test do
8
+ #
9
+ # desc 'Tracks test coverage with rcov'
10
+ # task :coverage do
11
+ # rm_f "coverage"
12
+ # rm_f "coverage.data"
13
+ #
14
+ # unless PLATFORM['i386-mswin32']
15
+ # rcov = "rcov --sort coverage --rails --aggregate coverage.data " <<
16
+ # "--text-summary -Ilib -T " <<
17
+ # "-x gems/*,db/migrate/*,jrails/*/*" <<
18
+ # ',\(eval\),\(recognize_optimized\),\(erb\)' << # needed in jruby
19
+ # ",yaml,yaml/*,lib/tmail/parser.y,jruby.jar!/*" << # needed in jruby
20
+ # ",html_test/*/*" <<
21
+ # ",html_test_extension/*/*"
22
+ # else
23
+ # rcov = "rcov.cmd --sort coverage --rails --aggregate " <<
24
+ # "coverage.data --text-summary -Ilib -T"
25
+ # end
26
+ #
27
+ # dirs = Dir.glob("test/**/*_test.rb").collect{|f|File.dirname(f)}.uniq
28
+ # lastdir = dirs.pop
29
+ # dirs.each do |dir|
30
+ # system("#{rcov} --no-html #{dir}/*_test.rb")
31
+ # end
32
+ # system("#{rcov} --html #{lastdir}/*_test.rb") unless lastdir.nil?
33
+ #
34
+ # unless PLATFORM['i386-mswin32']
35
+ ## jruby-1.5.0.RC1 > PLATFORM
36
+ ## => "java"
37
+ ## system("open coverage/index.html") if PLATFORM['darwin']
38
+ # system("open coverage/index.html")
39
+ # else
40
+ # system("\"C:/Program Files/Mozilla Firefox/firefox.exe\" " +
41
+ # "coverage/index.html")
42
+ # end
43
+ # end
44
+ #end
@@ -0,0 +1,2 @@
1
+ # From `script/generate simply_authorized` ...
2
+ require 'simply_authorized/test_tasks'
@@ -0,0 +1,2 @@
1
+ # From `script/generate simply_helpful` ...
2
+ require 'simply_helpful/test_tasks'
@@ -0,0 +1,5 @@
1
+ # From `script/generate simply_sessions` ...
2
+ unless Gem.source_index.find_name('jakewendt-simply_sessions').empty?
3
+ gem 'jakewendt-simply_sessions'
4
+ require 'simply_sessions/test_tasks'
5
+ end
@@ -0,0 +1,50 @@
1
+ #namespace :ccls do
2
+ # desc "Sync extra files from CCLS engine."
3
+ # task :sync do
4
+ #
5
+ ## How to make this work ONLY for apps and not self/plugin/engine.
6
+ #
7
+ # FileUtils.mkdir_p('db/migrate') unless File.directory?('db/migrate')
8
+ # rsync_command = <<-EOF.gsub(/\s+/,' ').squish!
9
+ # rsync -ruv
10
+ # --exclude='versions'
11
+ # vendor/plugins/ucb_ccls_engine/db/migrate db
12
+ # EOF
13
+ # system rsync_command
14
+ #
15
+ # FileUtils.mkdir_p('public') unless File.directory?('public')
16
+ # rsync_command = <<-EOF.gsub(/\s+/,' ').squish!
17
+ # rsync -ruv
18
+ # --exclude='versions'
19
+ # vendor/plugins/ucb_ccls_engine/public .
20
+ # EOF
21
+ # system rsync_command
22
+ #
23
+ # rsync_command = <<-EOF.gsub(/\s+/,' ').squish!
24
+ # rsync -ruv
25
+ # --exclude='app'
26
+ # --exclude='assets'
27
+ # --exclude='config'
28
+ # --exclude='db'
29
+ # --exclude='extensions'
30
+ # --exclude='fixtures'
31
+ # --exclude='helpers'
32
+ # --exclude='log'
33
+ # --exclude='versions'
34
+ # --exclude='test_helper.rb'
35
+ # --exclude='engine_\*_test.rb'
36
+ # vendor/plugins/ucb_ccls_engine/test .
37
+ # EOF
38
+ # system rsync_command
39
+ # end
40
+ #end
41
+
42
+ #
43
+ # If the gem doesn't exist then this would block
44
+ # the usage of rake gems:install
45
+ # If we wrap it in this condition, it works fine.
46
+ #
47
+ if Gem.searcher.find('paperclip')
48
+ require 'paperclip'
49
+ load "tasks/paperclip.rake"
50
+ end
@@ -0,0 +1 @@
1
+ require 'jakewendt-simply_documents'
@@ -0,0 +1,41 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ before_filter :login_required
4
+
5
+ helper :all # include all helpers, all the time
6
+
7
+ helper_method :current_user, :logged_in?
8
+
9
+ # See ActionController::RequestForgeryProtection for details
10
+ protect_from_forgery
11
+
12
+ def logged_in?
13
+ !current_user.nil?
14
+ end
15
+
16
+ def current_user_required
17
+ unless logged_in?
18
+ access_denied("goodbye","/some_fake_login_path")
19
+ end
20
+ end
21
+ alias_method :login_required, :current_user_required
22
+
23
+ def current_user
24
+ @current_user ||= if( session && session[:uid] )
25
+ # if the user model hasn't been loaded yet
26
+ # this will return nil and fail.
27
+ User.find_by_uid(session[:uid])
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ def redirections
34
+ @redirections ||= HashWithIndifferentAccess.new({
35
+ :not_be_user => {
36
+ :redirect_to => user_path(current_user)
37
+ }
38
+ })
39
+ end
40
+
41
+ end
@@ -0,0 +1,10 @@
1
+ class HomeController < ApplicationController
2
+
3
+ skip_before_filter :login_required
4
+
5
+ def show
6
+ render :text => "You are home.",
7
+ :layout => true
8
+ end
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ authorized
3
+ end
@@ -0,0 +1,9 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+
3
+ map.resource :home, :only => :show
4
+ map.root :controller => :home, :action => :show
5
+
6
+ # map.logout 'logout', :controller => 'sessions', :action => 'destroy'
7
+ # map.resource :session, :only => [ :destroy ]
8
+
9
+ end
@@ -0,0 +1,222 @@
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
+ # newer versions of paperclip do not include the trailing "." after file name
102
+ assert_match %r{\Ahttp(s)?://s3.amazonaws.com/ccls/documents/\d+/bogus_file_name\?AWSAccessKeyId=\w+&Expires=\d+&Signature=.+\z}, @response.redirected_to
103
+ end
104
+
105
+
106
+
107
+
108
+
109
+
110
+ test "should NOT download document with nil document and #{cu} login" do
111
+ document = Factory(:document)
112
+ assert document.document.path.blank?
113
+ login_as send(cu)
114
+ get :show, :id => document.id
115
+ assert_redirected_to preview_document_path(document)
116
+ assert_not_nil flash[:error]
117
+ end
118
+
119
+ test "should NOT download document with no document and #{cu} login" do
120
+ document = Factory(:document, :document_file_name => 'bogus_file_name')
121
+ assert !File.exists?(document.document.path)
122
+ login_as send(cu)
123
+ get :show, :id => document.id
124
+ assert_redirected_to preview_document_path(document)
125
+ assert_not_nil flash[:error]
126
+ end
127
+
128
+ test "should NOT download nonexistant document with #{cu} login" do
129
+ assert !File.exists?('some_fake_file_name.doc')
130
+ login_as send(cu)
131
+ get :show, :id => 'some_fake_file_name',:format => 'doc'
132
+ assert_redirected_to documents_path
133
+ assert_not_nil flash[:error]
134
+ end
135
+
136
+ test "should preview document with document and #{cu} login" do
137
+ document = Factory(:document)
138
+ login_as send(cu)
139
+ get :preview, :id => document.id
140
+ assert_response :success
141
+ assert_nil flash[:error]
142
+ end
143
+
144
+ test "should download document by id with document and #{cu} login" do
145
+ document = Document.create!(Factory.attributes_for(:document,
146
+ :document => File.open(File.dirname(__FILE__) +
147
+ '/../../assets/edit_save_wireframe.pdf')))
148
+ login_as send(cu)
149
+ get :show, :id => document.reload.id
150
+ assert_nil flash[:error]
151
+ assert_not_nil @response.headers['Content-disposition'].match(
152
+ /attachment;.*pdf/)
153
+ document.destroy
154
+ end
155
+
156
+ test "should download document by name with document and #{cu} login" do
157
+ document = Document.create!(Factory.attributes_for(:document,
158
+ :document => File.open(File.dirname(__FILE__) +
159
+ '/../../assets/edit_save_wireframe.pdf')))
160
+ login_as send(cu)
161
+ get :show, :id => 'edit_save_wireframe',
162
+ :format => 'pdf'
163
+ assert_nil flash[:error]
164
+ assert_not_nil @response.headers['Content-disposition'].match(
165
+ /attachment;.*pdf/)
166
+ document.destroy
167
+ end
168
+
169
+ test "should NOT create invalid document with #{cu} login" do
170
+ login_as send(cu)
171
+ assert_no_difference('Document.count') do
172
+ post :create, :document => {}
173
+ end
174
+ assert_not_nil flash[:error]
175
+ assert_template 'new'
176
+ assert_response :success
177
+ end
178
+
179
+ test "should NOT update invalid document with #{cu} login" do
180
+ login_as send(cu)
181
+ put :update, :id => Factory(:document).id,
182
+ :document => { :title => "a" }
183
+ assert_not_nil flash[:error]
184
+ assert_template 'edit'
185
+ assert_response :success
186
+ end
187
+
188
+ end
189
+
190
+ %w( interviewer reader active_user ).each do |cu|
191
+
192
+ test "should NOT preview document with #{cu} login" do
193
+ document = Factory(:document)
194
+ login_as send(cu)
195
+ get :preview, :id => document.id
196
+ assert_redirected_to root_path
197
+ assert_not_nil flash[:error]
198
+ end
199
+
200
+ test "should NOT download document with #{cu} login" do
201
+ document = Factory(:document)
202
+ login_as send(cu)
203
+ get :show, :id => document.id
204
+ assert_redirected_to root_path
205
+ assert_not_nil flash[:error]
206
+ end
207
+
208
+ end
209
+
210
+ test "should NOT preview document without login" do
211
+ document = Factory(:document)
212
+ get :preview, :id => document.id
213
+ assert_redirected_to_login
214
+ end
215
+
216
+ test "should NOT download document without login" do
217
+ document = Factory(:document)
218
+ get :show, :id => document.id
219
+ assert_redirected_to_login
220
+ end
221
+
222
+ end