active_admin_excel_upload 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +28 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/active_admin_excel_upload_manifest.js +2 -0
  6. data/app/assets/javascripts/active_admin_excel_upload/application.js +13 -0
  7. data/app/assets/javascripts/active_admin_excel_upload/cable.js +12 -0
  8. data/app/assets/javascripts/active_admin_excel_upload/channels/excel.coffee +19 -0
  9. data/app/assets/stylesheets/active_admin_excel_upload/application.css +15 -0
  10. data/app/channels/active_admin_excel_upload/excel_channel.rb +19 -0
  11. data/app/controllers/active_admin_excel_upload/application_controller.rb +5 -0
  12. data/app/helpers/active_admin_excel_upload/application_helper.rb +4 -0
  13. data/app/jobs/active_admin_excel_upload/application_job.rb +4 -0
  14. data/app/jobs/active_admin_excel_upload/excel_parser_job.rb +15 -0
  15. data/app/mailers/active_admin_excel_upload/application_mailer.rb +6 -0
  16. data/app/models/active_admin_excel_upload/application_record.rb +5 -0
  17. data/app/views/admin/excel/excel_result.html.erb +10 -0
  18. data/app/views/admin/excel/upload_excel.html.erb +20 -0
  19. data/app/views/layouts/active_admin_excel_upload/application.html.erb +14 -0
  20. data/config/routes.rb +2 -0
  21. data/lib/active_admin_excel_upload/authenticable.rb +21 -0
  22. data/lib/active_admin_excel_upload/dsl.rb +25 -0
  23. data/lib/active_admin_excel_upload/engine.rb +5 -0
  24. data/lib/active_admin_excel_upload/excel_parsable.rb +34 -0
  25. data/lib/active_admin_excel_upload/railtie.rb +8 -0
  26. data/lib/active_admin_excel_upload/version.rb +3 -0
  27. data/lib/active_admin_excel_upload.rb +28 -0
  28. data/lib/tasks/active_admin_excel_upload_tasks.rake +4 -0
  29. metadata +116 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 337bcac793c6bdd8aa517b13808502d69139bb9b
4
+ data.tar.gz: e43dc70e3611487c96f18feeaa3a74ac68a850cd
5
+ SHA512:
6
+ metadata.gz: 7a1ba5e0ab1ab2914d46e949bafe303a9a1fec291b47bd788ea3aa5edeafc10edab702bbb4774d9c954ff7ca6326fb921e00820ce0b7ab722a5aa3f67c08b4a2
7
+ data.tar.gz: 0ecc1def5227899b7147cb14238d2a34268a197a2ca4ab84bf077bcb2a54c5e56da550404bc08d2fd442427f4eb7adcb95d0acd06a95afb4f9d31843dde83a49
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017
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.md ADDED
@@ -0,0 +1,28 @@
1
+ # ActiveAdminExcelUpload
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'active_admin_excel_upload'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install active_admin_excel_upload
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActiveAdminExcelUpload'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/active_admin_excel_upload .js
2
+ //= link_directory ../stylesheets/active_admin_excel_upload .css
@@ -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 any plugin's vendor/assets/javascripts directory 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. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,12 @@
1
+ // Action Cable provides the framework to deal with WebSockets in Rails.
2
+ // You can generate new channels where WebSocket features live using the rails generate channel command.
3
+ //
4
+ //= require action_cable
5
+ //= require_self
6
+ //= require_tree ./
7
+ (function() {
8
+ this.App || (this.App = {});
9
+
10
+ App.cable = ActionCable.createConsumer();
11
+
12
+ }).call(this);
@@ -0,0 +1,19 @@
1
+ App.room = App.cable.subscriptions.create "ActiveAdminExcelUpload::ExcelChannel",
2
+ connected: ->
3
+ # Called when the subscription is ready for use on the server
4
+ console.log('connected')
5
+ disconnected: ->
6
+ # Called when the subscription has been terminated by the server
7
+
8
+ received: (data) ->
9
+ # Called when there's incoming data on the websocket for this channel
10
+ para = document.createElement("P");
11
+ t = document.createTextNode(data["message"]);
12
+ para.appendChild(t);
13
+ para.style.borderBottom = "1px solid lightgrey";
14
+ para.style.margin = "0px";
15
+ para.style.padding= "10px";
16
+ document.getElementById("show_status").appendChild(para);
17
+
18
+ speak: ->
19
+ @perform 'speak'
@@ -0,0 +1,15 @@
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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,19 @@
1
+ module ActiveAdminExcelUpload
2
+ class ExcelChannel < ApplicationCable::Channel
3
+ def subscribed
4
+ # stream_from "some_channel"
5
+ if ActiveAdminExcelUpload.configuration.use_default_connecion_authentication
6
+ stream_from "excel_channel_#{current_admin_user.id}"
7
+ else
8
+ stream_from "excel_channel_#{self.send(ActiveAdminExcelUpload.configuration.connection_identifier).send(:id)}"
9
+ end
10
+ end
11
+
12
+ def unsubscribed
13
+ # Any cleanup needed when channel is unsubscribed
14
+ end
15
+
16
+ def speak
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdminExcelUpload
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveAdminExcelUpload
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveAdminExcelUpload
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveAdminExcelUpload
2
+
3
+ class ExcelParserJob < ApplicationJob
4
+ queue_as :excels_queue
5
+
6
+ before_perform do |job|
7
+ sleep 5
8
+ end
9
+ def perform(klass,file_path,current_admin_user)
10
+ # Do something later
11
+ klass.constantize.send(:excel_process_sheet, file_path, current_admin_user)
12
+ FileUtils.rm(file_path)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module ActiveAdminExcelUpload
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdminExcelUpload
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ <%= javascript_include_tag "active_admin_excel_upload/application" %>
2
+ <div style="padding:10px" >
3
+ Processing Excel. Please wait. It may take a while.
4
+ </div>
5
+ <div style="padding:10px" >
6
+ Please do not go away from this page or you may loose tack of processing.
7
+ </div>
8
+ <div id="show_status" style="margin-top: 30px;border: 1px solid lightgrey">
9
+
10
+ </div>
@@ -0,0 +1,20 @@
1
+ <%= form_for :dump, :url=>{:action=>"import_excel"}, :html => { :multipart => true } do |f| %>
2
+ <table>
3
+ <tr>
4
+ <td>
5
+ <label for="dump_file">
6
+ Select a Excel File :
7
+ </label>
8
+ </td>
9
+ <td>
10
+ <%= f.file_field :file %>
11
+ </td>
12
+
13
+ </tr>
14
+ <tr>
15
+ <td>
16
+ <%= submit_tag 'Submit' %>
17
+ </td>
18
+ </tr>
19
+ </table>
20
+ <% end %>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Active admin excel upload</title>
5
+ <%= stylesheet_link_tag "active_admin_excel_upload/application", media: "all" %>
6
+ <%= javascript_include_tag "active_admin_excel_upload/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ActiveAdminExcelUpload::Engine.routes.draw do
2
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveAdminExcelUpload
2
+ module Authenticable
3
+ def self.included(base)
4
+ base.send(:identified_by,:current_admin_user)
5
+ base.include(InstanceMethods)
6
+ end
7
+ module InstanceMethods
8
+ def connect
9
+ self.current_admin_user = find_verified_user
10
+ end
11
+ private
12
+ def find_verified_user
13
+ if request.env['warden'].user
14
+ request.env['warden'].user
15
+ else
16
+ reject_unauthorized_connection
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveAdminExcelUpload
2
+ module DSL
3
+ def excel_importable
4
+ action_item :only => :index do
5
+ link_to 'Upload Excel', :action => 'upload_excel'
6
+ end
7
+
8
+ collection_action :upload_excel do
9
+ render "admin/excel/upload_excel"
10
+ end
11
+ collection_action :excel_upload_result do
12
+ render "admin/excel/excel_result"
13
+ end
14
+
15
+ collection_action :import_excel, :method => :post do
16
+ file_name = DateTime.now.to_s + params[:dump][:file].original_filename
17
+ tmp = params[:dump][:file].tempfile
18
+ final_path = Rails.root.join('tmp', file_name)
19
+ FileUtils.move tmp.path, final_path
20
+ ExcelParserJob.perform_later(self.resource_class.to_s,final_path.to_s,self.send(ActiveAdmin.application.current_user_method))
21
+ redirect_to :action => :excel_upload_result, :notice => "CSV imported successfully!"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdminExcelUpload
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ActiveAdminExcelUpload
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module ActiveAdminExcelUpload
2
+ module ExcelParsable
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def excel_create_record(row, index, header,channel_name)
7
+ ActionCable.server.broadcast channel_name, message: "processing for #{row}"
8
+ object = Hash[header.zip row]
9
+ record = self.new(object)
10
+ if record.save
11
+ ActionCable.server.broadcast channel_name, message: "Successfully cureated record for #{row}, id: #{record.id}"
12
+ else
13
+ ActionCable.server.broadcast channel_name, message: "Could not create record for #{row}, error: #{record.errors.messages}"
14
+ end
15
+ end
16
+ def excel_process_sheet(sheet,current_admin_user)
17
+ xlsx = Roo::Spreadsheet.open(sheet)
18
+ sheet = xlsx.sheet(xlsx.sheets.index(self.table_name))
19
+ header = sheet.row(1)
20
+ channel_name = "excel_channel_#{current_admin_user.id}"
21
+ header_downcase = header.map(&:parameterize).map(&:underscore)
22
+ ActionCable.server.broadcast channel_name, message: "Start processing sheet #{self.table_name}"
23
+ sheet.parse.each_with_index do |row, index|
24
+ begin
25
+ self.excel_create_record(row,index,header_downcase,channel_name)
26
+ rescue StandardError => e
27
+ ActionCable.server.broadcast channel_name, message: "Exception while processing #{row}, Exception: #{e.message}"
28
+ end
29
+ end
30
+ ActionCable.server.broadcast channel_name, message: "End processing sheet #{self.table_name}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveAdminExcelUpload
2
+ class Railtie < Rails::Railtie
3
+ initializer "active_admin_excel_upload_railtie.configure_rails_initialization" do
4
+ ActiveAdmin::DSL.send(:include, ActiveAdminExcelUpload::DSL)
5
+ ActiveRecord::Base.send(:include, ActiveAdminExcelUpload::ExcelParsable)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveAdminExcelUpload
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ require "active_admin_excel_upload/engine"
2
+ require "activeadmin"
3
+ require "active_admin_excel_upload/dsl"
4
+ require "active_admin_excel_upload/railtie"
5
+ require "active_admin_excel_upload/authenticable"
6
+ require "active_admin_excel_upload/excel_parsable"
7
+ module ActiveAdminExcelUpload
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+ def self.configure
13
+ self.configuration ||= Configuration.new
14
+ yield(configuration)
15
+ if configuration.use_default_connecion_authentication
16
+ ApplicationCable::Connection.send(:include, ActiveAdminExcelUpload::Authenticable)
17
+ end
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :use_default_connecion_authentication
22
+ attr_accessor :connection_identifier
23
+ def initialize
24
+ @use_default_connecion_authentication = true
25
+ @connection_identifier = :current_admin_user
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :active_admin_excel_upload do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_admin_excel_upload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shiv Garg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coffee-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: roo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: active_admin_excel_upload gem brings convention over configuration for
56
+ your excel uploads. This gem is designed to process your excel sheet on a active
57
+ job and display the results live using action cables
58
+ email:
59
+ - shivgarg5676@hotmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - MIT-LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - app/assets/config/active_admin_excel_upload_manifest.js
68
+ - app/assets/javascripts/active_admin_excel_upload/application.js
69
+ - app/assets/javascripts/active_admin_excel_upload/cable.js
70
+ - app/assets/javascripts/active_admin_excel_upload/channels/excel.coffee
71
+ - app/assets/stylesheets/active_admin_excel_upload/application.css
72
+ - app/channels/active_admin_excel_upload/excel_channel.rb
73
+ - app/controllers/active_admin_excel_upload/application_controller.rb
74
+ - app/helpers/active_admin_excel_upload/application_helper.rb
75
+ - app/jobs/active_admin_excel_upload/application_job.rb
76
+ - app/jobs/active_admin_excel_upload/excel_parser_job.rb
77
+ - app/mailers/active_admin_excel_upload/application_mailer.rb
78
+ - app/models/active_admin_excel_upload/application_record.rb
79
+ - app/views/admin/excel/excel_result.html.erb
80
+ - app/views/admin/excel/upload_excel.html.erb
81
+ - app/views/layouts/active_admin_excel_upload/application.html.erb
82
+ - config/routes.rb
83
+ - lib/active_admin_excel_upload.rb
84
+ - lib/active_admin_excel_upload/authenticable.rb
85
+ - lib/active_admin_excel_upload/dsl.rb
86
+ - lib/active_admin_excel_upload/engine.rb
87
+ - lib/active_admin_excel_upload/excel_parsable.rb
88
+ - lib/active_admin_excel_upload/railtie.rb
89
+ - lib/active_admin_excel_upload/version.rb
90
+ - lib/tasks/active_admin_excel_upload_tasks.rake
91
+ homepage: https://github.com/shivgarg5676/active_admin_excel_upload
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Excel upload for active admin resources using background jobs and action
115
+ cable
116
+ test_files: []