active_admin_excel_upload 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 337bcac793c6bdd8aa517b13808502d69139bb9b
4
- data.tar.gz: e43dc70e3611487c96f18feeaa3a74ac68a850cd
3
+ metadata.gz: 2aa53db46cafcd3a4577834b6d3d2a5c0ba19a24
4
+ data.tar.gz: 9df562b957c30174e9db058e02b47a034bcd275a
5
5
  SHA512:
6
- metadata.gz: 7a1ba5e0ab1ab2914d46e949bafe303a9a1fec291b47bd788ea3aa5edeafc10edab702bbb4774d9c954ff7ca6326fb921e00820ce0b7ab722a5aa3f67c08b4a2
7
- data.tar.gz: 0ecc1def5227899b7147cb14238d2a34268a197a2ca4ab84bf077bcb2a54c5e56da550404bc08d2fd442427f4eb7adcb95d0acd06a95afb4f9d31843dde83a49
6
+ metadata.gz: f775b0a480e53e0e0991acc37bd0d7a89b3e01438410e63a0f51d41f618dea29752b985bf9297ce0d10cb854f0673e6aebebd467e35cdddbae7194299c5a6222
7
+ data.tar.gz: 86e39b7be4890ebbb035991dbf4834b4d372d958c052fafc2e926c667c6df35de35648da94089c932d7ddd2cbf45d5b8072fcfe5ad8ee3d187779f4fc6eaf85d
data/README.md CHANGED
@@ -1,8 +1,14 @@
1
- # ActiveAdminExcelUpload
2
- Short description and motivation.
1
+ # ActiveAdminExcelUpload (active_admin_excel_upload)
2
+ A gem for excel upload for active admin. It is designed for large excel sheets that consume too much time on normal process. It uses active job to process sheets on a different queue and action cables to show progress of your upload. Active admin excel upload brings rails convention for excel uploads.
3
3
 
4
- ## Usage
5
- How to use my plugin.
4
+
5
+ ## prerequisites
6
+ ```ruby
7
+ active-admin,
8
+ Devise,
9
+ Rails 5,
10
+ redis
11
+ ```
6
12
 
7
13
  ## Installation
8
14
  Add this line to your application's Gemfile:
@@ -21,6 +27,81 @@ Or install it yourself as:
21
27
  $ gem install active_admin_excel_upload
22
28
  ```
23
29
 
30
+ ##Config generator
31
+ ```bash
32
+ $rails g active_admin_excel_upload:install
33
+ ```
34
+
35
+ ## Usage
36
+ configure your active admin resource to use it.
37
+
38
+ ```ruby
39
+ # RAILS_ROOT/app/admin/post.rb
40
+ ActiveAdmin.register Post do
41
+ excel_importable
42
+ end
43
+ ```
44
+ set your queue backend in application.rb
45
+ ```ruby
46
+ config.active_job.queue_adapter = :sidekiq
47
+ ```
48
+ You can use any queuing backend e.g resque etc.
49
+
50
+ You may need redis adapter in cable.yml to work it in dev mode. By default rails uses redis adaper for production mode.
51
+ ```ruby
52
+ development:
53
+ adapter: redis
54
+ ```
55
+
56
+
57
+ ## Convention
58
+ 1. By default it searches for sheet in your excel file with the same name as of the table in the db for current model.
59
+ 2. First row in the sheet is considered as header. Header names has to match either column names in the table or humanized version of column names.
60
+ 3. It tries it create record every time for every row. If record is not created due to some error or validation failue they are shown live and process moves to next row.
61
+
62
+
63
+ ## configuration
64
+ if You are using your own authentication method for action cables. You can pass the identified by to the active_admin_excel_upload.
65
+
66
+ config/initializers/active_admin_excel_upload.rb
67
+
68
+ ```ruby
69
+ ActiveAdminExcelUpload.configure do |config|
70
+ config.use_default_connecion_authentication = false
71
+ config.connection_identifier = :current_admin_user
72
+ end
73
+ ```
74
+
75
+ ## Overrides
76
+
77
+ ## Override Parsing method.
78
+ Active admin excel upload defines a method in ActiveRecord::Base class. if you want custom behaviour for your row you can define a method in your model as follows
79
+
80
+ ```ruby
81
+ def excel_create_record(row, index, header,channel_name)
82
+ ActionCable.server.broadcast channel_name, message: "processing for #{row}"
83
+ object = Hash[header.zip row]
84
+ record = self.new(object)
85
+ if record.save
86
+ ActionCable.server.broadcast channel_name, message: "Successfully cureated record for #{row}, id: #{record.id}"
87
+ else
88
+ ActionCable.server.broadcast channel_name, message: "Could not create record for #{row}, error: #{record.errors.messages}"
89
+ end
90
+ end
91
+ ```
92
+ active_admin_excel_upload send 4 arguments to this method. First is the array for the current processing row in the excel. Second is the index for the row. Third is header of the sheet. Fourth is the channel_name to send messages on.
93
+
94
+ ```ruby
95
+ ActionCable.server.broadcast channel_name, message: "Successfully cureated record for #{row}, id: #{record.id}"
96
+ ```
97
+ This particular line is used to broadcast messsages on particular channel.
98
+
99
+ By default it sends string as message but you can render anuthing you want. e.g
100
+
101
+ ``` ruby
102
+ ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
103
+ ```
104
+
24
105
  ## Contributing
25
106
  Contribution directions go here.
26
107
 
@@ -7,13 +7,17 @@ App.room = App.cable.subscriptions.create "ActiveAdminExcelUpload::ExcelChannel"
7
7
 
8
8
  received: (data) ->
9
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);
10
+ searchParams = new URLSearchParams(window.location.search);
11
+ model = searchParams.get("model")
12
+ modelInMessage = data.message.match("\\[(\\w+\)]")[1]
13
+ if model == modelInMessage
14
+ para = document.createElement("P");
15
+ t = document.createTextNode(data["message"].match("\\[(\\w+\)](.*)")[2]);
16
+ para.appendChild(t);
17
+ para.style.borderBottom = "1px solid lightgrey";
18
+ para.style.margin = "0px";
19
+ para.style.padding= "10px";
20
+ document.getElementById("show_status").appendChild(para);
17
21
 
18
22
  speak: ->
19
23
  @perform 'speak'
@@ -4,6 +4,7 @@ require "active_admin_excel_upload/dsl"
4
4
  require "active_admin_excel_upload/railtie"
5
5
  require "active_admin_excel_upload/authenticable"
6
6
  require "active_admin_excel_upload/excel_parsable"
7
+ require "roo"
7
8
  module ActiveAdminExcelUpload
8
9
  class << self
9
10
  attr_accessor :configuration
@@ -25,4 +26,14 @@ module ActiveAdminExcelUpload
25
26
  @connection_identifier = :current_admin_user
26
27
  end
27
28
  end
29
+
30
+
31
+ # find a better place for this method
32
+ def self.move_file_to_rails_tmp(params)
33
+ file_name = DateTime.now.to_s + params[:dump][:file].original_filename
34
+ tmp = params[:dump][:file].tempfile
35
+ final_path = Rails.root.join('tmp', file_name)
36
+ FileUtils.move tmp.path, final_path
37
+ return final_path
38
+ end
28
39
  end
@@ -11,14 +11,10 @@ module ActiveAdminExcelUpload
11
11
  collection_action :excel_upload_result do
12
12
  render "admin/excel/excel_result"
13
13
  end
14
-
15
14
  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
15
+ final_path = ActiveAdminExcelUpload.move_file_to_rails_tmp(params)
20
16
  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!"
17
+ redirect_to :action => :excel_upload_result,:model => self.resource_class.to_s
22
18
  end
23
19
  end
24
20
  end
@@ -3,31 +3,38 @@ module ActiveAdminExcelUpload
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  class_methods do
6
+ def publish_to_channel(channel_name,message)
7
+ message = "[#{self.to_s}]#{message}"
8
+ ActionCable.server.broadcast channel_name, message: message
9
+ end
10
+
6
11
  def excel_create_record(row, index, header,channel_name)
7
- ActionCable.server.broadcast channel_name, message: "processing for #{row}"
12
+ self.publish_to_channel(channel_name, "processing for #{row}")
8
13
  object = Hash[header.zip row]
9
14
  record = self.new(object)
10
15
  if record.save
11
- ActionCable.server.broadcast channel_name, message: "Successfully cureated record for #{row}, id: #{record.id}"
16
+ self.publish_to_channel(channel_name,"Successfully cureated record for #{row}, id: #{record.id}")
12
17
  else
13
- ActionCable.server.broadcast channel_name, message: "Could not create record for #{row}, error: #{record.errors.messages}"
18
+ self.publish_to_channel(channel_name,"Could not create record for #{row}, error: #{record.errors.messages}")
14
19
  end
15
20
  end
21
+
22
+
16
23
  def excel_process_sheet(sheet,current_admin_user)
17
24
  xlsx = Roo::Spreadsheet.open(sheet)
18
25
  sheet = xlsx.sheet(xlsx.sheets.index(self.table_name))
19
26
  header = sheet.row(1)
20
27
  channel_name = "excel_channel_#{current_admin_user.id}"
21
28
  header_downcase = header.map(&:parameterize).map(&:underscore)
22
- ActionCable.server.broadcast channel_name, message: "Start processing sheet #{self.table_name}"
29
+ self.publish_to_channel(channel_name,"Start processing sheet #{self.table_name}")
23
30
  sheet.parse.each_with_index do |row, index|
24
31
  begin
25
32
  self.excel_create_record(row,index,header_downcase,channel_name)
26
33
  rescue StandardError => e
27
- ActionCable.server.broadcast channel_name, message: "Exception while processing #{row}, Exception: #{e.message}"
34
+ self.publish_to_channel(channel_name,"Exception while processing #{row}, Exception: #{e.message}")
28
35
  end
29
36
  end
30
- ActionCable.server.broadcast channel_name, message: "End processing sheet #{self.table_name}"
37
+ self.publish_to_channel(channel_name, "End processing sheet #{self.table_name}")
31
38
  end
32
39
  end
33
40
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveAdminExcelUpload
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -0,0 +1,14 @@
1
+ module ActiveAdminExcelUpload
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Creates ActiveAdminExcelUpload initializer for your application"
6
+
7
+ def copy_initializer
8
+ template "active_admin_excel_uplaod_initializer.rb", "config/initializers/active_admin_excel_uplaod.rb"
9
+
10
+ puts "Install complete! Truly Outrageous!"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ ActiveAdminExcelUpload.configure do |config|
2
+ config.use_default_connecion_authentication = true
3
+ config.connection_identifier = :current_admin_user
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_admin_excel_upload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shiv Garg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-08 00:00:00.000000000 Z
11
+ date: 2018-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coffee-rails
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
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
41
  description: active_admin_excel_upload gem brings convention over configuration for
56
42
  your excel uploads. This gem is designed to process your excel sheet on a active
57
43
  job and display the results live using action cables
@@ -87,6 +73,8 @@ files:
87
73
  - lib/active_admin_excel_upload/excel_parsable.rb
88
74
  - lib/active_admin_excel_upload/railtie.rb
89
75
  - lib/active_admin_excel_upload/version.rb
76
+ - lib/generators/active_admin_excel_upload/install_generator.rb
77
+ - lib/generators/templates/active_admin_excel_uplaod_initializer.rb
90
78
  - lib/tasks/active_admin_excel_upload_tasks.rake
91
79
  homepage: https://github.com/shivgarg5676/active_admin_excel_upload
92
80
  licenses:
@@ -108,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
96
  version: '0'
109
97
  requirements: []
110
98
  rubyforge_project:
111
- rubygems_version: 2.5.2
99
+ rubygems_version: 2.6.14
112
100
  signing_key:
113
101
  specification_version: 4
114
102
  summary: Excel upload for active admin resources using background jobs and action