enju_export 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +47 -0
  4. data/app/controllers/export_files_controller.rb +106 -0
  5. data/app/models/export_file.rb +19 -0
  6. data/app/views/export_files/_download.html.erb +1 -0
  7. data/app/views/export_files/_form.html.erb +14 -0
  8. data/app/views/export_files/edit.html.erb +6 -0
  9. data/app/views/export_files/index.html.erb +32 -0
  10. data/app/views/export_files/new.html.erb +5 -0
  11. data/app/views/export_files/show.html.erb +25 -0
  12. data/app/views/export_files/show.js.erb +1 -0
  13. data/app/views/page/403.html.erb +9 -0
  14. data/app/views/page/403.mobile.erb +5 -0
  15. data/app/views/page/403.xml.erb +4 -0
  16. data/app/views/page/404.html.erb +9 -0
  17. data/app/views/page/404.mobile.erb +5 -0
  18. data/app/views/page/404.xml.erb +4 -0
  19. data/config/locales/translation_en.yml +9 -0
  20. data/config/locales/translation_ja.yml +8 -0
  21. data/config/routes.rb +3 -0
  22. data/db/migrate/20120610075759_create_export_files.rb +12 -0
  23. data/lib/enju_export/engine.rb +9 -0
  24. data/lib/enju_export/version.rb +3 -0
  25. data/lib/enju_export.rb +36 -0
  26. data/lib/tasks/enju_export_tasks.rake +4 -0
  27. data/spec/controllers/export_files_controller_spec.rb +75 -0
  28. data/spec/dummy/README.rdoc +261 -0
  29. data/spec/dummy/Rakefile +7 -0
  30. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  31. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  32. data/spec/dummy/app/controllers/application_controller.rb +23 -0
  33. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  34. data/spec/dummy/app/models/ability.rb +14 -0
  35. data/spec/dummy/app/models/role.rb +47 -0
  36. data/spec/dummy/app/models/user.rb +28 -0
  37. data/spec/dummy/app/models/user_has_role.rb +4 -0
  38. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  39. data/spec/dummy/config/application.rb +60 -0
  40. data/spec/dummy/config/boot.rb +10 -0
  41. data/spec/dummy/config/database.yml +25 -0
  42. data/spec/dummy/config/environment.rb +5 -0
  43. data/spec/dummy/config/environments/development.rb +37 -0
  44. data/spec/dummy/config/environments/production.rb +67 -0
  45. data/spec/dummy/config/environments/test.rb +37 -0
  46. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/spec/dummy/config/initializers/devise.rb +205 -0
  48. data/spec/dummy/config/initializers/inflections.rb +15 -0
  49. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  50. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  51. data/spec/dummy/config/initializers/session_store.rb +8 -0
  52. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/spec/dummy/config/locales/en.yml +5 -0
  54. data/spec/dummy/config/routes.rb +60 -0
  55. data/spec/dummy/config.ru +4 -0
  56. data/spec/dummy/db/migrate/20111201121844_create_roles.rb +12 -0
  57. data/spec/dummy/db/migrate/20111201155456_create_users.rb +13 -0
  58. data/spec/dummy/db/migrate/20111201155513_add_devise_to_users.rb +52 -0
  59. data/spec/dummy/db/migrate/20111201163718_create_user_has_roles.rb +10 -0
  60. data/spec/dummy/db/schema.rb +73 -0
  61. data/spec/dummy/db/test.sqlite3 +0 -0
  62. data/spec/dummy/public/404.html +26 -0
  63. data/spec/dummy/public/422.html +26 -0
  64. data/spec/dummy/public/500.html +25 -0
  65. data/spec/dummy/public/favicon.ico +0 -0
  66. data/spec/dummy/script/rails +6 -0
  67. data/spec/fixtures/roles.yml +21 -0
  68. data/spec/fixtures/user_has_roles.yml +41 -0
  69. data/spec/fixtures/users.yml +69 -0
  70. data/spec/models/export_file_spec.rb +5 -0
  71. data/spec/requests/export_files_spec.rb +11 -0
  72. data/spec/routing/export_files_routing_spec.rb +19 -0
  73. data/spec/spec_helper.rb +34 -0
  74. data/spec/support/controller_macros.rb +25 -0
  75. data/spec/support/devise.rb +4 -0
  76. data/spec/views/export_files/edit.html.erb_spec.rb +24 -0
  77. data/spec/views/export_files/index.html.erb_spec.rb +29 -0
  78. data/spec/views/export_files/new.html.erb_spec.rb +24 -0
  79. data/spec/views/export_files/show.html.erb_spec.rb +18 -0
  80. metadata +273 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = EnjuExport
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
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 = 'EnjuExport'
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
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+ require 'rspec/core'
40
+ require 'rspec/core/rake_task'
41
+
42
+ RSpec::Core::RakeTask.new(:spec) do |spec|
43
+ spec.pattern = FileList['spec/**/*_spec.rb']
44
+ end
45
+
46
+
47
+ task :default => :spec
@@ -0,0 +1,106 @@
1
+ class ExportFilesController < ApplicationController
2
+ load_and_authorize_resource
3
+
4
+ # GET /export_files
5
+ # GET /export_files.json
6
+ def index
7
+ @export_files = ExportFile.page(params[:page])
8
+
9
+ respond_to do |format|
10
+ format.html # index.html.erb
11
+ format.json { render :json => @export_files }
12
+ end
13
+ end
14
+
15
+ # GET /export_files/1
16
+ # GET /export_files/1.json
17
+ def show
18
+ @export_file = ExportFile.find(params[:id])
19
+
20
+ respond_to do |format|
21
+ format.html { # show.html.erb
22
+ if params[:mode] == 'download'
23
+ if @export_file.state == 'completed'
24
+ redirect_to export_file_path(@export_file, :format => :csv)
25
+ #render 'download'
26
+ else
27
+ redirect_to(@export_file, :notice => 'in process')
28
+ end
29
+ end
30
+ }
31
+ format.json { render :json => @export_file }
32
+ format.js
33
+ format.csv {
34
+ if @export_file.state == 'completed'
35
+ if configatron.uploaded_file.storage == :s3
36
+ redirect_to @export_file.export.expiring_url(10)
37
+ else
38
+ send_file @export_file.export.path, :disposition => 'attachment'
39
+ end
40
+ else
41
+ not_found
42
+ end
43
+ }
44
+ end
45
+ end
46
+
47
+ # GET /export_files/new
48
+ # GET /export_files/new.json
49
+ def new
50
+ @export_file = ExportFile.new
51
+
52
+ respond_to do |format|
53
+ format.html # new.html.erb
54
+ format.json { render :json => @export_file }
55
+ end
56
+ end
57
+
58
+ # GET /export_files/1/edit
59
+ def edit
60
+ @export_file = ExportFile.find(params[:id])
61
+ end
62
+
63
+ # POST /export_files
64
+ # POST /export_files.json
65
+ def create
66
+ @export_file = ExportFile.new(params[:export_file])
67
+
68
+ respond_to do |format|
69
+ if @export_file.save
70
+ format.html { redirect_to @export_file, :notice => ' export file was successfully created.' }
71
+ format.json { render :json => @export_file, :status => :created, location: @export_file }
72
+ else
73
+ format.html { render :action => "new" }
74
+ format.json { render :json => @export_file.errors, :status => :unprocessable_entity }
75
+ end
76
+ end
77
+ end
78
+
79
+ # PUT /export_files/1
80
+ # PUT /export_files/1.json
81
+ def update
82
+ @export_file = ExportFile.find(params[:id])
83
+
84
+ respond_to do |format|
85
+ if @export_file.update_attributes(params[:export_file])
86
+ format.html { redirect_to @export_file, :notice => ' export file was successfully updated.' }
87
+ format.json { head :no_content }
88
+ else
89
+ format.html { render :action => "edit" }
90
+ format.json { render :json => @export_file.errors, :status => :unprocessable_entity }
91
+ end
92
+ end
93
+ end
94
+
95
+ # DELETE /export_files/1
96
+ # DELETE /export_files/1.json
97
+ def destroy
98
+ @export_file = ExportFile.find(params[:id])
99
+ @export_file.destroy
100
+
101
+ respond_to do |format|
102
+ format.html { redirect_to export_files_url }
103
+ format.json { head :no_content }
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,19 @@
1
+ class ExportFile < ActiveRecord::Base
2
+ attr_accessible :export_content_type, :export_file_name, :export_file_size, :state
3
+
4
+ has_attached_file :export
5
+
6
+ state_machine :initial => :pending do
7
+ event :sm_start do
8
+ transition [:pending, :started] => :started
9
+ end
10
+
11
+ event :sm_complete do
12
+ transition :started => :completed
13
+ end
14
+
15
+ event :sm_fail do
16
+ transition :started => :failed
17
+ end
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ <%= link_to t('export_file.download'), export_file_path(@export_file, :format => :csv) %>
@@ -0,0 +1,14 @@
1
+ <%= simple_form_for(@export_file) do |f| %>
2
+ <%= f.error_notification %>
3
+
4
+ <div class="form-inputs">
5
+ <%= f.input :export_file_name %>
6
+ <%= f.input :export_content_type %>
7
+ <%= f.input :export_file_size %>
8
+ <%= f.input :state %>
9
+ </div>
10
+
11
+ <div class="form-actions">
12
+ <%= f.button :submit %>
13
+ </div>
14
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing export_file</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @export_file %> |
6
+ <%= link_to 'Back', export_files_path %>
@@ -0,0 +1,32 @@
1
+ <div id="content_detail" class="ui-corner-all">
2
+ <h1 class="title"><%= t('page.listing', :model => t('activerecord.models.export_file')) -%></h1>
3
+ <div id="content_list">
4
+
5
+ <table class="index">
6
+ <tr>
7
+ <th>Export file name</th>
8
+ <th>Export content type</th>
9
+ <th>Export file size</th>
10
+ <th>State</th>
11
+ <th></th>
12
+ <th></th>
13
+ </tr>
14
+
15
+ <% @export_files.each do |export_file| %>
16
+ <tr>
17
+ <td><%= export_file.export_file_name %></td>
18
+ <td><%= export_file.export_content_type %></td>
19
+ <td><%= export_file.export_file_size %></td>
20
+ <td><%= export_file.state %></td>
21
+ <td><%= link_to t('page.show'), export_file %></td>
22
+ <td><%= link_to t('page.destroy'), export_file, :data => {:confirm => t('page.are_you_sure')}, :method => :delete %></td>
23
+ </tr>
24
+ <% end %>
25
+ </table>
26
+
27
+ <%= will_paginate(@export_files) %>
28
+ </div>
29
+ </div>
30
+
31
+ <div id="submenu">
32
+ </div>
@@ -0,0 +1,5 @@
1
+ <h1>New export_file</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', export_files_path %>
@@ -0,0 +1,25 @@
1
+ <div id="content_detail" class="ui-corner-all">
2
+ <h1 class="title"><%= t('page.showing', :model => t('activerecord.models.export_file')) -%></h1>
3
+ <div id="content_list">
4
+ <p id="notice"><%= notice %></p>
5
+
6
+ <div id="download">
7
+ <script>
8
+ (function poll(){
9
+ setTimeout(function(){
10
+ $.ajax({ url: "<%= export_file_path(@export_file, :format => :json) %>", success: function(data){
11
+ if(data.state == 'completed'){
12
+ $.get("<%= export_file_url(@export_file, :format => :js) %>");
13
+ }
14
+ poll();
15
+ }, dataType: "json"});
16
+ }, 5000);
17
+ })();
18
+ </script>
19
+ <%= t('export_file.preparing_download') %> <%= image_tag 'spinner.gif' %>
20
+ </div>
21
+ </div>
22
+ </div>
23
+
24
+ <div id="submenu" class="ui-corner-all">
25
+ </div>
@@ -0,0 +1 @@
1
+ $("#download").html("<%= escape_javascript(render("download")) %>");
@@ -0,0 +1,9 @@
1
+ <div id="content_detail" class="ui-corner-all">
2
+ <h1 class="title"><%= t('page.access_denied') -%></h1>
3
+ <div id="content_list">
4
+ <%= render 'page/search_form' %>
5
+ </div>
6
+ </div>
7
+
8
+ <div id="submenu" class="ui-corner-all">
9
+ </div>
@@ -0,0 +1,5 @@
1
+ <div data-role="header">
2
+ <h2 class="title"><%= t('page.access_denied') -%></h2>
3
+ </div>
4
+ <div data-role="content">
5
+ </div>
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <error>
3
+ <status>403</status>
4
+ </error>
@@ -0,0 +1,9 @@
1
+ <div id="content_detail" class="ui-corner-all">
2
+ <h1 class="title"><%= t('page.not_found') -%></h1>
3
+ <div id="content_list">
4
+ <%= render 'page/search_form' %>
5
+ </div>
6
+ </div>
7
+
8
+ <div id="submenu" class="ui-corner-all">
9
+ </div>
@@ -0,0 +1,5 @@
1
+ <div data-role="header">
2
+ <h2 class="title"><%= t('page.not_found') -%></h2>
3
+ </div>
4
+ <div data-role="content">
5
+ </div>
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <error>
3
+ <status>404</status>
4
+ </error>
@@ -0,0 +1,9 @@
1
+ ja:
2
+ activerecord:
3
+ models:
4
+ export_file: Export file
5
+
6
+ en:
7
+ export_file:
8
+ download: Download
9
+ preparing_download: Preparing download...
@@ -0,0 +1,8 @@
1
+ ja:
2
+ activerecord:
3
+ models:
4
+ export_file: エクスポートファイル
5
+
6
+ export_file:
7
+ download: ダウンロード
8
+ preparing_download: ダウンロード準備中…
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resources :export_files, :only => [:index, :show, :destroy]
3
+ end
@@ -0,0 +1,12 @@
1
+ class CreateExportFiles < ActiveRecord::Migration
2
+ def change
3
+ create_table :export_files do |t|
4
+ t.string :export_file_name
5
+ t.string :export_content_type
6
+ t.string :export_file_size
7
+ t.string :state
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'state_machine'
2
+ require 'paperclip'
3
+ require 'simple_form'
4
+ require 'will_paginate/array'
5
+
6
+ module EnjuExport
7
+ class Engine < ::Rails::Engine
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module EnjuExport
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ require "enju_export/engine"
2
+
3
+ module EnjuExport
4
+ module ActsAsMethods
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def enju_export
11
+ include ExportCSV
12
+ end
13
+ end
14
+
15
+ module ExportCSV
16
+ def self.included(base)
17
+ base.extend ClassMethods
18
+ end
19
+
20
+ module ClassMethods
21
+ def generate_csv(ids, attrs, export_file, role = nil)
22
+ export_file.sm_start!
23
+ file = Tempfile.new("#{self.name.downcase}_")
24
+ self.where('required_role_id >= ?', role.try(:id).to_i).find_each(:conditions => {:id => ids}) do |record|
25
+ file.write(attrs.map{|attr| record.send(attr)}.join(',') + "\n")
26
+ end
27
+ file.close
28
+ export_file.export = File.new(file.path)
29
+ export_file.sm_complete!
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ ActiveRecord::Base.send :include, EnjuExport::ActsAsMethods
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :enju_export do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ # This spec was generated by rspec-rails when you ran the scaffold generator.
4
+ # It demonstrates how one might use RSpec to specify the controller code that
5
+ # was generated by Rails when you ran the scaffold generator.
6
+ #
7
+ # It assumes that the implementation code is generated by the rails scaffold
8
+ # generator. If you are using any extension libraries to generate different
9
+ # controller code, this generated spec may or may not pass.
10
+ #
11
+ # It only uses APIs available in rails and/or rspec-rails. There are a number
12
+ # of tools you can use to make these specs even more expressive, but we're
13
+ # sticking to rails and rspec-rails APIs to keep things simple and stable.
14
+ #
15
+ # Compared to earlier versions of this generator, there is very limited use of
16
+ # stubs and message expectations in this spec. Stubs are only used when there
17
+ # is no simpler way to get a handle on the object needed for the example.
18
+ # Message expectations are only used when there is no simpler way to specify
19
+ # that an instance is receiving a specific message.
20
+
21
+ describe ExportFilesController do
22
+ fixtures :all
23
+
24
+ # This should return the minimal set of attributes required to create a valid
25
+ # ExportFile. As you add validations to ExportFile, be sure to
26
+ # update the return value of this method accordingly.
27
+ def valid_attributes
28
+ {}
29
+ end
30
+
31
+ # This should return the minimal set of values that should be in the session
32
+ # in order to pass any filters (e.g. authentication) defined in
33
+ # ExportFilesController. Be sure to keep this updated too.
34
+ def valid_session
35
+ {}
36
+ end
37
+
38
+ describe "GET index" do
39
+ login_fixture_admin
40
+
41
+ it "assigns all export_files as @export_files" do
42
+ export_file = ExportFile.create! valid_attributes
43
+ get :index, {} #, valid_session
44
+ assigns(:export_files).should eq([export_file])
45
+ end
46
+ end
47
+
48
+ describe "GET show" do
49
+ login_fixture_admin
50
+
51
+ it "assigns the requested export_file as @export_file" do
52
+ export_file = ExportFile.create! valid_attributes
53
+ get :show, {:id => export_file.to_param} #, valid_session
54
+ assigns(:export_file).should eq(export_file)
55
+ end
56
+ end
57
+
58
+ describe "DELETE destroy" do
59
+ login_fixture_admin
60
+
61
+ it "destroys the requested export_file" do
62
+ export_file = ExportFile.create! valid_attributes
63
+ expect {
64
+ delete :destroy, {:id => export_file.to_param} #, valid_session
65
+ }.to change(ExportFile, :count).by(-1)
66
+ end
67
+
68
+ it "redirects to the export_files list" do
69
+ export_file = ExportFile.create! valid_attributes
70
+ delete :destroy, {:id => export_file.to_param} #, valid_session
71
+ response.should redirect_to(export_files_url)
72
+ end
73
+ end
74
+
75
+ end