beam 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.md +6 -0
  6. data/Gemfile.lock +165 -1
  7. data/README.md +34 -4
  8. data/Rakefile +35 -0
  9. data/beam.gemspec +13 -0
  10. data/lib/beam.rb +20 -0
  11. data/lib/beam/extensions/exception.rb +6 -6
  12. data/lib/beam/upload.rb +10 -15
  13. data/lib/beam/upload_controller_methods.rb +112 -0
  14. data/lib/beam/version.rb +1 -1
  15. data/lib/generators/beam/config/initializers/beam.rb +5 -0
  16. data/lib/generators/beam/install_generator.rb +1 -1
  17. data/spec/controllers/users_controller_spec.rb +134 -0
  18. data/spec/dummy/README.rdoc +28 -0
  19. data/spec/dummy/Rakefile +6 -0
  20. data/spec/dummy/app/assets/images/.keep +0 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/javascripts/users.js +2 -0
  23. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  24. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  25. data/spec/dummy/app/assets/stylesheets/users.css +4 -0
  26. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  27. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  28. data/spec/dummy/app/controllers/users_controller.rb +58 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  30. data/spec/dummy/app/helpers/users_helper.rb +2 -0
  31. data/spec/dummy/app/mailers/.keep +0 -0
  32. data/spec/dummy/app/models/.keep +0 -0
  33. data/spec/dummy/app/models/concerns/.keep +0 -0
  34. data/spec/dummy/app/models/user.rb +2 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/spec/dummy/app/views/users/_form.html.erb +25 -0
  37. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  38. data/spec/dummy/app/views/users/index.html.erb +29 -0
  39. data/spec/dummy/app/views/users/new.html.erb +5 -0
  40. data/spec/dummy/app/views/users/show.html.erb +14 -0
  41. data/spec/dummy/bin/bundle +3 -0
  42. data/spec/dummy/bin/rails +4 -0
  43. data/spec/dummy/bin/rake +4 -0
  44. data/spec/dummy/config.ru +4 -0
  45. data/spec/dummy/config/application.rb +28 -0
  46. data/spec/dummy/config/boot.rb +5 -0
  47. data/spec/dummy/config/database.yml +25 -0
  48. data/spec/dummy/config/environment.rb +6 -0
  49. data/spec/dummy/config/environments/development.rb +29 -0
  50. data/spec/dummy/config/environments/production.rb +80 -0
  51. data/spec/dummy/config/environments/test.rb +36 -0
  52. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  53. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  54. data/spec/dummy/config/initializers/inflections.rb +16 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  56. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  57. data/spec/dummy/config/initializers/session_store.rb +3 -0
  58. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy/config/locales/en.yml +23 -0
  60. data/spec/dummy/config/routes.rb +4 -0
  61. data/spec/dummy/db/migrate/20140101111420_create_users.rb +10 -0
  62. data/spec/dummy/db/schema.rb +23 -0
  63. data/spec/dummy/lib/assets/.keep +0 -0
  64. data/spec/dummy/public/404.html +58 -0
  65. data/spec/dummy/public/422.html +58 -0
  66. data/spec/dummy/public/500.html +57 -0
  67. data/spec/dummy/public/favicon.ico +0 -0
  68. data/spec/fixturesspec/.DS_Store +0 -0
  69. data/spec/fixturesspec/users.csv +5 -0
  70. data/spec/fixturesspec/users.csv.zip +0 -0
  71. data/spec/fixturesspec/users_with_errors.csv.zip +0 -0
  72. data/spec/lib/beam/beam_config_spec.rb +15 -0
  73. data/spec/lib/beam/extensions/exception_spec.rb +15 -0
  74. data/spec/lib/beam/upload_spec.rb +67 -0
  75. data/spec/spec_helper.rb +33 -0
  76. metadata +311 -4
  77. data/config/initializers/beam.rb +0 -8
@@ -1,3 +1,3 @@
1
1
  module Beam
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,5 @@
1
+ # Beam.config[:error_file_needed] = true
2
+ # Beam.config[:batch_process] = true
3
+ # Beam.config[:batch_size] = 1_000
4
+ # Beam.config[:zipped] = true
5
+ Beam.config[:data_upload_path] = "#{Rails.root}/tmp"
@@ -3,7 +3,7 @@ require 'rails'
3
3
  module Beam
4
4
  module Generators
5
5
  class InstallGenerator < ::Rails::Generators::Base
6
- source_root File.expand_path('../../../../config/initializers', __FILE__)
6
+ source_root File.expand_path('../config/initializers', __FILE__)
7
7
 
8
8
  desc "This generator adds beam.rb to initializers with default config"
9
9
  def copy_beam_config
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+ require_relative Beam.lib+'/beam/upload_controller_methods'
3
+ class UsersController < ApplicationController
4
+ include Beam::UploadControllerMethods
5
+ end
6
+
7
+ # class FakeTest
8
+ # end
9
+
10
+ describe UsersController do
11
+ before(:each) do
12
+ Rails.application.routes.draw do
13
+ post "users/upload", to: "users#upload"
14
+ get "users/error_file", to: "users#error_file"
15
+ end
16
+ end
17
+
18
+ after(:each) do
19
+ Rails.application.reload_routes!
20
+ end
21
+
22
+ it 'should send error file' do
23
+ expect(@controller).to receive(:send_file).with("#{Beam.config[:data_upload_path]}/errors_users.csv")
24
+ expect(@controller).to receive(:render)
25
+
26
+ get :error_file
27
+
28
+ response.should be_success
29
+ end
30
+
31
+ context 'upload' do
32
+ before(:each) do
33
+ @request.env["HTTP_ACCEPT"] = "application/json"
34
+ @request.env["CONTENT_TYPE"] = "application/json"
35
+ end
36
+
37
+ it 'should return error when the file is not detected' do
38
+ expect(@controller).to receive(:params).and_return({ upload: {} })
39
+
40
+ post :upload, {upload: {}}
41
+
42
+ response.header['Content-Type'].should include('application/json')
43
+ response.body.should == {error_msg: "No file detected, please upload a file",
44
+ message: "No file detected, please upload a file",
45
+ status: "failure",
46
+ status_code: 500
47
+ }.to_json
48
+ end
49
+
50
+ it 'should upload file and return error when the file is not a zipped csv file' do
51
+ expect(@controller).to receive(:params).and_return({ upload: {upload_file: file=double(content_type: 'text/xlsx')} })
52
+
53
+ post :upload, {upload: {upload_file: "some mock file"}}
54
+
55
+ response.header['Content-Type'].should include('application/json')
56
+ response.body.should == {error_msg: "Only zipped csv files are allowed",
57
+ message: "Only zipped csv files are allowed",
58
+ status: "failure",
59
+ status_code: 500
60
+ }.to_json
61
+ end
62
+
63
+ it 'should upload file and return failure when file upload process gives 500' do
64
+ expect(@controller).to receive(:params).and_return({ upload: {upload_file: file=double(content_type: 'application/zip', original_filename: 'test.zip')} })
65
+ expect(@controller).to receive(:upload_file).with(file).and_return(upload_response = {status: 500})
66
+ expect(@controller).to receive(:zip_file?).with(file).and_return(true)
67
+ post :upload, {upload: {upload_file: "some mock file"}}
68
+
69
+ response.header['Content-Type'].should include('application/json')
70
+ response.body.should == {
71
+ num_errors: 1,
72
+ controller_name: 'users',
73
+ error_msg: "Please check the format of zipped file",
74
+ message: "Please check the format of zipped file",
75
+ status: "failure",
76
+ status_code: 500
77
+ }.to_json
78
+ end
79
+
80
+ it 'should upload file and return success when uploaded through queue' do
81
+ pending "DelayedJob & SideKiq placehoder"
82
+ end
83
+
84
+ it 'should upload file and return success when uploaded with zero error' do
85
+ tempfile = fixture_file_upload(Beam.spec + '/fixturesspec/users.csv.zip', 'application/zip')
86
+ expect(@controller).to receive(:params).and_return({ upload: {
87
+ upload_file: file=double(content_type: 'application/zip',
88
+ tempfile: tempfile,
89
+ original_filename: 'users.csv.zip')
90
+ }
91
+ })
92
+ expect(User).to receive(:upload_file).with('users.csv.zip', Beam.tmp).and_return(upload_response = {status: 200, errors: 0})
93
+ expect(@controller).to receive(:zip_file?).with(file).and_return(true)
94
+
95
+ post :upload, {upload: {upload_file: "some mock file"}}
96
+
97
+ response.header['Content-Type'].should include('application/json')
98
+ response.body.should == {
99
+ num_errors: 0,
100
+ controller_name: 'users',
101
+ success_msg: "File uploaded successfully",
102
+ message: "File uploaded successfully",
103
+ status: "success",
104
+ status_code: 200
105
+ }.to_json
106
+
107
+ end
108
+
109
+ it 'should upload file and return success when uploaded with > 0 error' do
110
+ tempfile = fixture_file_upload(Beam.spec + '/fixturesspec/users.csv.zip', 'application/zip')
111
+ expect(@controller).to receive(:params).and_return({ upload: {
112
+ upload_file: file=double(content_type: 'application/zip',
113
+ tempfile: tempfile,
114
+ original_filename: 'users.csv.zip')
115
+ }
116
+ })
117
+ expect(User).to receive(:upload_file).with('users.csv.zip', Beam.tmp).and_return(upload_response = {status: 200, errors: 1})
118
+ expect(@controller).to receive(:zip_file?).with(file).and_return(true)
119
+ post :upload, {upload: {upload_file: "some mock file"}}
120
+
121
+ response.header['Content-Type'].should include('application/json')
122
+ response.body.should == {
123
+ num_errors: 1,
124
+ controller_name: 'users',
125
+ error_msg: msg = "There are 1 row(s) with errors while uploading config file." +
126
+ " Please check whether you have selected correct module or you have valid data in the file.",
127
+ message: msg,
128
+ status: "failure",
129
+ status_code: 500
130
+ }.to_json
131
+
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,13 @@
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
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,13 @@
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_self
12
+ *= require_tree .
13
+ */
@@ -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,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,58 @@
1
+ class UsersController < ApplicationController
2
+ before_action :set_user, only: [:show, :edit, :update, :destroy]
3
+
4
+ # GET /users
5
+ def index
6
+ @users = User.all
7
+ end
8
+
9
+ # GET /users/1
10
+ def show
11
+ end
12
+
13
+ # GET /users/new
14
+ def new
15
+ @user = User.new
16
+ end
17
+
18
+ # GET /users/1/edit
19
+ def edit
20
+ end
21
+
22
+ # POST /users
23
+ def create
24
+ @user = User.new(user_params)
25
+
26
+ if @user.save
27
+ redirect_to @user, notice: 'User was successfully created.'
28
+ else
29
+ render action: 'new'
30
+ end
31
+ end
32
+
33
+ # PATCH/PUT /users/1
34
+ def update
35
+ if @user.update(user_params)
36
+ redirect_to @user, notice: 'User was successfully updated.'
37
+ else
38
+ render action: 'edit'
39
+ end
40
+ end
41
+
42
+ # DELETE /users/1
43
+ def destroy
44
+ @user.destroy
45
+ redirect_to users_url, notice: 'User was successfully destroyed.'
46
+ end
47
+
48
+ private
49
+ # Use callbacks to share common setup or constraints between actions.
50
+ def set_user
51
+ @user = User.find(params[:id])
52
+ end
53
+
54
+ # Only allow a trusted parameter "white list" through.
55
+ def user_params
56
+ params.require(:user).permit(:name, :email)
57
+ end
58
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,25 @@
1
+ <%= form_for(@user) do |f| %>
2
+ <% if @user.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @user.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :name %><br>
16
+ <%= f.text_field :name %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :email %><br>
20
+ <%= f.text_field :email %>
21
+ </div>
22
+ <div class="actions">
23
+ <%= f.submit %>
24
+ </div>
25
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing user</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @user %> |
6
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,29 @@
1
+ <h1>Listing users</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Name</th>
7
+ <th>Email</th>
8
+ <th></th>
9
+ <th></th>
10
+ <th></th>
11
+ </tr>
12
+ </thead>
13
+
14
+ <tbody>
15
+ <% @users.each do |user| %>
16
+ <tr>
17
+ <td><%= user.name %></td>
18
+ <td><%= user.email %></td>
19
+ <td><%= link_to 'Show', user %></td>
20
+ <td><%= link_to 'Edit', edit_user_path(user) %></td>
21
+ <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
22
+ </tr>
23
+ <% end %>
24
+ </tbody>
25
+ </table>
26
+
27
+ <br>
28
+
29
+ <%= link_to 'New User', new_user_path %>