Permalink
Browse files

List added. Files can be uploaded through flash and html5

  • Loading branch information...
1 parent cbaaa7a commit f3442671d7f4b5f0789ec6832982c2ad15ef18ba Arturs Meisters committed Mar 29, 2011
Showing with 749 additions and 130 deletions.
  1. +6 −6 Gemfile
  2. +50 −0 app/controllers/lolita/upload/files_controller.rb
  3. +0 −27 app/models/lolita/multimedia/file.rb
  4. +34 −0 app/models/lolita/upload/file.rb
  5. +9 −0 app/uploaders/file_uploader.rb
  6. +11 −0 app/views/components/lolita/tab/_files.html.erb
  7. +5 −0 app/views/components/lolita/tab/files/_cells.html.erb
  8. +1 −0 app/views/components/lolita/tab/files/_destroy.js.erb
  9. +1 −0 app/views/components/lolita/tab/files/_edit.js.erb
  10. +3 −0 app/views/components/lolita/tab/files/_form.html.erb
  11. +13 −0 app/views/components/lolita/tab/files/_list.html.erb
  12. +6 −0 app/views/components/lolita/tab/files/_list_form.html.erb
  13. +3 −0 app/views/components/lolita/tab/files/_row.html.erb
  14. +1 −0 app/views/components/lolita/tab/files/_update.js.erb
  15. +43 −0 app/views/components/lolita/tab/files/_upload_script.html.erb
  16. +3 −0 config/locales/en.yml
  17. +4 −4 features/configuration_for_tab.feature
  18. +11 −7 features/extend_resources_with_new_routes.feature
  19. +0 −33 features/humanize_sizes_in_multimedia_file.feature
  20. +25 −21 features/step_definitions/lolita_steps.rb
  21. +31 −0 features/support_byte_converter.feature
  22. +4 −1 features/upload_file.feature
  23. +20 −0 lib/generators/lolita_file_upload/install_generator.rb
  24. +25 −0 lib/generators/lolita_file_upload/templates/migration.rb
  25. +7 −4 lib/lolita-file-upload.rb
  26. +19 −2 lib/lolita-file-upload/configuration/tab/{file.rb → files.rb}
  27. +5 −1 lib/lolita-file-upload/module.rb
  28. +1 −0 lib/lolita-file-upload/rails.rb
  29. +20 −0 lib/lolita-file-upload/rails/file_upload_routes.rb
  30. +9 −3 lib/support/bytes.rb
  31. 0 log/development.log
  32. BIN public/images/lolita/upload/plupload/backgrounds.gif
  33. BIN public/images/lolita/upload/plupload/buttons-disabled.png
  34. BIN public/images/lolita/upload/plupload/buttons.png
  35. BIN public/images/lolita/upload/plupload/delete.gif
  36. BIN public/images/lolita/upload/plupload/done.gif
  37. BIN public/images/lolita/upload/plupload/error.gif
  38. BIN public/images/lolita/upload/plupload/plupload-bw.png
  39. BIN public/images/lolita/upload/plupload/plupload.png
  40. BIN public/images/lolita/upload/plupload/throbber.gif
  41. BIN public/images/lolita/upload/plupload/transp50.png
  42. +1 −0 public/javascripts/lolita/upload/jquery.plupload.queue.min.js
  43. +1 −0 public/javascripts/lolita/upload/jquery.ui.plupload.min.js
  44. BIN public/javascripts/lolita/upload/plupload.flash.swf
  45. +2 −0 public/javascripts/lolita/upload/plupload.full.min.js
  46. BIN public/javascripts/lolita/upload/plupload.silverlight.xap
  47. +139 −0 public/stylesheets/lolita/upload/jquery.ui.plupload.css
  48. +177 −0 public/stylesheets/lolita/upload/plupload.queue.css
  49. +3 −3 spec/configuration/tab/{file_spec.rb → files_spec.rb}
  50. +6 −0 spec/lolita/support/bytes_spec.rb
  51. +5 −5 spec/models/file_spec.rb
  52. +8 −9 spec/spec_helper.rb
  53. +9 −2 test_orm/active_record/post.rb
  54. +1 −1 test_orm/coverage.rb
  55. +3 −0 test_orm/db/migrate/02_create_files.rb
  56. +8 −0 test_orm/rails/config/application.rb
  57. +5 −0 test_orm/rails/config/enviroment.rb
  58. +3 −0 test_orm/rails/config/routes.rb
  59. 0 test_orm/rails/log/development.log
  60. +8 −1 test_orm/support.rb
View
@@ -3,23 +3,23 @@ source "http://rubygems.org"
#gem "lolita"
gem "carrierwave", "~> 0.5.2"
group :test do
- gem "rspec-rails", "~> 2.5.0"
+ gem "rspec-rails", "~> 2.5"
gem "ffaker"
gem "factory_girl"
gem "sqlite3"
end
group :development do
- gem "cucumber", "~> 0.10.0"
+ gem "cucumber", "~> 0.10"
gem "cucumber-rails", "~> 0.3.2"
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.5.2"
+ gem "bundler", "~> 1.0"
+ gem "jeweler", "~> 1.5"
end
group :test do
- gem "rspec", "~> 2.5.0"
+ gem "rspec", "~> 2.5"
gem 'cover_me', '>= 1.0.0.rc6'
- gem 'activerecord', "~> 3.0.0", :require=>false
+ gem 'activerecord', "~> 3.0", :require=>false
end
@@ -0,0 +1,50 @@
+module Lolita
+ module Upload
+ class FilesController < ApplicationController
+ include Lolita::Controllers::UserHelpers
+ include Lolita::Controllers::InternalHelpers
+ before_filter :authenticate_lolita_user!
+ before_filter :build_file, :only=>[:create]
+ before_filter :set_file, :only=>[:edit,:update,:destroy]
+ before_filter :set_tab, :only=>[:edit,:update,:destroy,:create]
+
+ respond_to :js,:html
+
+ def create
+ @file.asset = params[:file]
+ @file.save!
+ render_component :"lolita/tab/files",:row,:file=>@file, :tab=>@tab
+ end
+
+ def edit
+ render_component :"lolita/tab/files", :edit, :file=>@file,:tab=>@tab
+ end
+
+ def update
+ @file.update_attributes!(params[:file])
+ render_component :"lolita/tab/files", :update, :file=>@file,:tab=>@tab
+ end
+
+
+ def destroy
+ @file.destroy
+ render_component :"lolita/tab/files",:destroy,:file=>@file,:tab=>@tab
+ end
+
+ private
+
+ def build_file
+ @file = Lolita::Upload::File.new(params[:upload])
+ end
+
+ def set_file
+ dbi=Lolita::DBI::Base.new(Lolita::Upload::File)
+ @file=dbi.find_by_id(params[:id]) || build_file
+ end
+
+ def set_tab
+ @tab=@file.fileable ? @file.fileable.lolita.tabs.by_type(:files) : nil
+ end
+ end
+ end
+end
@@ -1,27 +0,0 @@
-module Lolita
- module Multimedia
- class File < ActiveRecord::Base
- set_table_name :lolita_files
-
- mount_uploader :asset, FileUploader
- belongs_to :fileable, :polymorphic=>true
-
- validate :size_limit
-
-
- private
-
- def size_limit
- if self.fileable && lolita=self.fileable.class.lolita
- maxsize=lolita.tabs.by_type(:file).maxfilesize
- if maxsize.to_i>0 && self.asset.file.size>maxsize
- bytes=Lolita::Support::Bytes.new(size)
- self.errors.add(:asset,"file too big (max #{bytes.value} #{bytes.unit})")
- end
- end
- end
-
- end
- end
-end
-
@@ -0,0 +1,34 @@
+module Lolita
+ module Upload
+ class File < ActiveRecord::Base
+ set_table_name :lolita_files
+
+ mount_uploader :asset, FileUploader
+ belongs_to :fileable, :polymorphic=>true
+
+ validate :size_limit
+ before_save :set_metadata
+
+
+ private
+
+ def size_limit
+ if self.fileable && lolita=self.fileable.class.lolita
+ maxsize=lolita.tabs.by_type(:files).maxfilesize
+ if maxsize.to_i>0 && self.asset.file.size>maxsize
+ bytes=Lolita::Support::Bytes.new(maxsize)
+ self.errors.add(:asset,I18n.t("lolita.upload.errors.file too big",:value=>bytes.value,:unit=>bytes.unit))
+ end
+ end
+ end
+
+ def set_metadata
+ self.name||=::File.basename(self.asset.to_s)
+ self.asset_size=self.asset.file.size
+ self.asset_extension=::File.extname(self.asset.to_s).gsub(".","")
+ end
+
+ end
+ end
+end
+
@@ -1,3 +1,12 @@
class FileUploader < CarrierWave::Uploader::Base
storage :file
+ def timestamp
+ (model.created_at || Time.now).strftime("%Y%m")
+ end
+
+ # With slash in first place it will be absolute url, otherwise relative to Rails.root+"/public"
+ def store_dir
+ "uploads/#{model.class.to_s.underscore}/#{timestamp}/#{model.id}"
+ end
+
end
@@ -0,0 +1,11 @@
+<%= form_tag(send(:"lolita_upload_#{tab.type}_path"),:method=>:post) do %>
+ <div id="file_upload_container">
+ </div>
+<% end %>
+ <div id="filelist">
+ <%= render_component :"lolita/tab/files", :list,:tab=>tab %>
+ </div>
+<%= render_component :"lolita/tab/files", :upload_script,
+ :container=>"file_upload_container",:tab=>tab
+%>
+
@@ -0,0 +1,5 @@
+<td><%=file.name || "-"%></td>
+<td>
+ <%=link_to "E",send(:"edit_lolita_upload_#{tab.type.to_s.singularize}_path",:id=>file.id),:remote=>true %>
+ <%= link_to "D",send(:"lolita_upload_#{tab.type.to_s.singularize}_path",:id=>file.id),:method=>:delete,:remote=>true,:confirm=>"Are you sure?" %>
+</td>
@@ -0,0 +1 @@
+$("#upload_<%=tab.type%>_row_<%=file.id%>").remove()
@@ -0,0 +1 @@
+$("#upload_<%=tab.type%>_row_<%=file.id%>").html("<%=escape_javascript(render_component(:"lolita/tab/files",:list_form,:tab=>tab,:file=>file,:format=>"html"))%>")
@@ -0,0 +1,3 @@
+<%= form_tag send(:"lolita_upload_#{tab.type.to_s.singularize}_path",:id=>file.id),:method=>:put,:remote=>true,:id=>"edit_#{tab.type}_#{file.id}" do %>
+ <%= text_field_tag "file[name]",file.name %>
+ <% end %>
@@ -0,0 +1,13 @@
+<table id="file_list_for_<%=tab.type%>" >
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>*</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% resource.send(tab.association.name).each do |file| %>
+ <%= render_component :"lolita/tab/files", :row, :file=>file,:tab=>tab,:value=>nil %>
+ <% end %>
+ </tbody>
+</table>
@@ -0,0 +1,6 @@
+<td>
+ <%=render_component :"lolita/tab/files", :form, :tab=>tab,:file=>file%>
+</td>
+<td>
+ <%= link_to "S", "#", :onclick=>"$('##{"edit_#{tab.type}_#{file.id}"}').submit();return false;" %>
+</td>
@@ -0,0 +1,3 @@
+<tr id="upload_<%=tab.type%>_row_<%=file.id%>" >
+ <%= render_component :"lolita/tab/files", :cells, :tab=>tab, :file=>file %>
+</tr>
@@ -0,0 +1 @@
+$("#upload_<%=tab.type%>_row_<%=file.id%>").html("<%=escape_javascript(render_component(:"lolita/tab/files",:cells,:tab=>tab,:file=>file,:format=>"html"))%>")
@@ -0,0 +1,43 @@
+<% content_for :style do %>
+ <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/overcast/jquery-ui.css" type="text/css" />
+<link rel="stylesheet" href="/stylesheets/lolita/upload/jquery.ui.plupload.css" type="text/css" />
+<% end %>
+<% content_for :script do %>
+<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript"></script>
+<script type="text/javascript" src="/javascripts/lolita/upload/plupload.full.min.js"></script>
+<script type="text/javascript" src="/javascripts/lolita/upload/jquery.ui.plupload.min.js"></script>
+<script type="text/javascript" src="/javascripts/lolita/upload/ru.js"></script>
+<script type="text/javascript">
+ $(function(){
+ $("#<%=container%>").plupload({
+ // General settings
+ runtimes : 'flash,html5',
+ url : '<%=send(:"lolita_upload_#{tab.type}_path")%>',
+ max_file_size : '10mb',
+ chunk_size : '1mb',
+ unique_names : true,
+ rename: true,
+ multipart: true,
+ multipart_params: {
+ "authenticity_token" : '<%= form_authenticity_token %>',
+ "<%="upload[#{tab.association.name.to_s.singularize}able_type]"%>": "<%=resource.class%>",
+ "<%="upload[#{tab.association.name.to_s.singularize}able_id]"%>": "<%=resource.new_record? ? 0 : resource.id%>"
+ },
+ headers:{
+ "X-CSRF-Token": $("meta[name='csrf-token']").attr("content")
+ },
+
+ // Flash settings
+ flash_swf_url : '/javascripts/lolita/upload/plupload.flash.swf',
+ init:{
+ FileUploaded:function(uploader, file, info){
+
+ $("#file_list_for_<%=tab.type%>>tbody").append(info.response)
+ }
+ },
+
+ });
+
+ });
+</script>
+<% end %>
@@ -1,5 +1,8 @@
en:
lolita:
+ upload:
+ errors:
+ "file too big": "file too big (max %{value} %{unit})"
support:
bytes:
byte:
@@ -11,16 +11,16 @@ Scenario: i am able to define new tab with type :file and set allowed file types
Scenario: set maximum file upload size
Given a lolita
- And lolita-file-upload
- And rails engine
+ And a rails
+ And rails application
And file tab for post
When I set maximum file upload size to 100000
Then I cannot upload file large_file.txt
-Scenario: set assciation with Lolita::Multimedia::File
+Scenario: set assciation with Lolita::Upload::File
Given a lolita
And lolita-file-upload
When I define a file tab for post
- Then Post has association with Lolita::Multimedia::File
+ Then Post has association with Lolita::Upload::File
And association name for Post is :files
@@ -1,13 +1,17 @@
Feature: all resoures have new routes for file upload
-All resources have new routes like /posts/1/files/new or for new files /posts/files/new
+File upload lives in lolita/upload and loads only once when any of lolita resources has defined files tab.
Scenario: extend resources path
Given a lolita
+ And a rails
+ And rails application
And lolita-file-upload
- Then I have get route /posts/1/files/new
- And I have post route /posts/1/files
- And I have get route /posts/1/files/1/edit
- And I have get route /posts/1/files
- And I have put route /post/1/files/1
- And I have delete route /posts/files/1
+ When I load routes
+ Then I have get route /lolita/upload/files/new
+ And I have get route /lolita/upload/files/1/edit
+ And I have get route /lolita/upload/files/1
+ And I have post route /lolita/upload/files
+ And I have put route /lolita/upload/files/1
+ And I have delete route /lolita/upload/files/1
+ And I have get route /lolita/upload/files
@@ -1,33 +0,0 @@
-Feature: user can create Lolita::Multimedia::File objects
-
-In oreder to upload files and link them with other models, there is Lolita::Multimedia::File
-class. It validates file based on lolita configuration given in parent class. ::File class is
-polymorphic so any other same ORM class can be added to it. Also this class provide size convertion
-for bytes, that is used in error messages.
-
-Scenario Outline: humanize file sizes
- Given a rails
- Given rails engine
- And multimedia file object
- When I humanize "<size>"
- Then I should get "<unit>" and "<limit>"
-
- Scenarios: to bytes
- | size | unit | limit |
- | 0 | bytes | 0.0 |
- | 1023 | bytes | 1023.0 |
-
- Scenarios: to kilobytes
- | size | unit | limit |
- | 1024 | kilobytes | 1.0 |
- | 1024**2-1 | kilobytes | 1023.99 |
-
- Scenarios: to megabytes
- | size | unit | limit |
- | 1024**2 | megabytes | 1.0 |
- | 1024**3-1 | megabytes | 1023.99 |
-
- Scenarios: to gigabytes
- | size | unit | limit |
- | 1024**3 | gigabytes | 1.0 |
- | 1024**4-1 | gigabytes | 1023.99 |
Oops, something went wrong. Retry.

0 comments on commit f344267

Please sign in to comment.