cruj_cruj_cruj 0.0.5 → 0.0.6

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: 0e3e876f07fe44ab1c4fa16a524b2366e024e179
4
- data.tar.gz: 9cdf5f844983e0da852123982545c990bc4993a0
3
+ metadata.gz: 59c401f3ce5d93b0fd264bcd4e5943a8ba024b2b
4
+ data.tar.gz: cb2a5c1ba36d0c7312e744d9bb5306a6254ab754
5
5
  SHA512:
6
- metadata.gz: db4b466fce2f5452a8f7f377b3b1c23c36cbe7da579f394ab8eef89d6d040b3ad1cf557dc9ab287baab38842a355d086bd730493285d9b491da4208a1e31b021
7
- data.tar.gz: 7e6a1367e7927574fc6cae67e26ac45bee3c5f1d80c1a44a88b6615abaa0f06ad60ba5a64ba4edcfbeee3f2730486deaa9acf016c3eb30858ffcfb7d49031c96
6
+ metadata.gz: 623856476743b62b7ce74c0eea8f738b207f86e4d520627cf99d41c068d2b9e9c55fe66424720022b9c5dd6d7f719e90a4ad253eb4a264ae3d7443b5dcfa6406
7
+ data.tar.gz: 587da1a1e0088bb68264ae51c894e30a33e35bf5bb0e091b872d3f0d5162dfd22e11e127dd02d0fb12fc131927ac07cc3706939ec3c766b6b43681a3bba49394
@@ -13,6 +13,29 @@ class CrujCrujCrujController < ApplicationController
13
13
 
14
14
  def index; end
15
15
 
16
+ def import
17
+ if params[:file].blank?
18
+ redirect_to atribuicao_automaticas_url, {flash: {error: l(:no_file_to_import_error_message)}}
19
+ return
20
+ end
21
+
22
+ errors = CrujCrujCruj::Services::ImportRules.import(params[:file], form_fields, model_class)
23
+
24
+ if errors.blank?
25
+ redirect_to url_for(action: :index, controller: params[:controller]), notice: l(:import_success_message)
26
+ return
27
+ end
28
+ redirect_to atribuicao_automaticas_url, flash: { import_errors: errors.join('<br />') }
29
+ end
30
+
31
+ def export_template
32
+ @q = model_class.ransack(params[:q])
33
+ @q.sorts = default_sort if @q.sorts.blank?
34
+
35
+ filename = CrujCrujCruj::Services::ImportRules.export_template(form_fields, @q.result(distinct: true))
36
+ send_file(filename, filename: l(:export_template_filename), type: "application/vnd.ms-excel")
37
+ end
38
+
16
39
  protected
17
40
 
18
41
  def before_index; end
@@ -23,6 +46,10 @@ class CrujCrujCrujController < ApplicationController
23
46
  @resources = @q.result(distinct: true).page(params[:page])
24
47
  end
25
48
 
49
+ def form_fields
50
+ []
51
+ end
52
+
26
53
  def namespace_url
27
54
  []
28
55
  end
@@ -0,0 +1,16 @@
1
+ <div>
2
+ <div style="float: left;">
3
+ <%= link_to l("link_to_new_#{snake_case_model_name}"), url_for(action: :new, controller: params[:controller]) %>
4
+ </div>
5
+
6
+ <div style="float: right;">
7
+ <%= form_tag url_for(action: :export_template, controller: params[:controller], q: params[:q]), style: 'margin-right: 10px' do %>
8
+ <%= submit_tag l(:export_template_label) %>
9
+ <% end %>
10
+
11
+ <%= form_tag url_for(action: :import, controller: params[:controller]), multipart: true do %>
12
+ <%= file_field_tag :file %>
13
+ <%= submit_tag l(:import_template_label) %>
14
+ <% end %>
15
+ </div>
16
+ </div>
@@ -17,6 +17,9 @@ pt-BR:
17
17
  principal_firstname_or_principal_lastname_cont: 'Atribuído para contém'
18
18
  type_cont: Tipo contém
19
19
 
20
+ export_template_label: "Exportar Template"
21
+ import_template_label: "Importar Template"
22
+
20
23
  ransack:
21
24
  search: "pesquisar"
22
25
  predicate: "predicado"
@@ -0,0 +1,151 @@
1
+ #encoding: utf-8
2
+ module CrujCrujCruj
3
+ module Services
4
+ class ImportRules
5
+
6
+ def self.export_template(fields, data)
7
+ package = Axlsx::Package.new
8
+ workbook = package.workbook
9
+
10
+ parameters_sheet_name = 'PARAMETERS'
11
+
12
+ workbook.add_worksheet(name: 'TEMPLATE') do |sheet|
13
+ title = sheet.styles.add_style(bg_color: 'FF007E7A', fg_color: 'FFFFFFFF', sz: 12, b: true, border: {style: :thin, color: 'FF000000'})
14
+
15
+ fields.each_with_index do |field, idx|
16
+ validation = field[:data_validation][:validation]
17
+ allow_blank = field[:data_validation][:allow_blank]
18
+ add_data_validation(sheet, idx + 1, validation, allow_blank, parameters_sheet_name) if validation
19
+ end
20
+
21
+ sheet.add_row (([:id] |fields.map { |field| field[:field_name] }).map { |t| I18n.t("#{t}_label") }), style: title
22
+
23
+ data.map { |resource| [resource.id].concat(fields.map { |field| resource_field_value(resource, field) }) }.each do |row|
24
+ sheet.add_row row, types: row.map{ |_| :string }
25
+ end
26
+ end
27
+
28
+ create_validations(fields, workbook, parameters_sheet_name)
29
+
30
+ filename = "tmp/template_#{Time.zone.now.strftime('%Y%m%d%H%M%S')}.xlsx"
31
+ package.serialize(filename)
32
+ filename
33
+ end
34
+
35
+ def self.import(file, fields, clazz)
36
+ spreadsheet = Roo::Spreadsheet.open(file.path, extension: :xlsx)
37
+
38
+ errors = []
39
+
40
+ invalid_rows = validate_id(spreadsheet, clazz)
41
+ errors << I18n.t(:column_invalid_rows, column: :id, rows: invalid_rows.first(25).join(', ')) unless invalid_rows.blank?
42
+
43
+ fields.each_with_index do |field, idx|
44
+ col = idx + 1
45
+ unless field[:data_validation][:allow_blank]
46
+ invalid_rows = validate_not_blank(col, spreadsheet)
47
+ errors << I18n.t(:column_invalid_rows, column: field[:field_name], rows: invalid_rows.first(25).join(', ')) unless invalid_rows.blank?
48
+ end
49
+
50
+ if field[:data_validation][:validation]
51
+ invalid_rows = validate_in_array(col, field_validation_values(field), spreadsheet)
52
+ errors << I18n.t(:column_invalid_rows, column: field[:field_name], rows: invalid_rows.first(25).join(', ')) unless invalid_rows.blank?
53
+ end
54
+ end
55
+
56
+ return errors unless errors.blank?
57
+
58
+
59
+ (2..spreadsheet.last_row).each do |i|
60
+ row = spreadsheet.row(i)
61
+ id = row[0]
62
+ resource = id ? clazz.find(id) : clazz.new
63
+
64
+ fields.each_with_index do |field, idx|
65
+ resource.send("#{field[:field_name]}=", row_value(row, idx+1, field, spreadsheet))
66
+ resource.save!
67
+ end
68
+ end
69
+
70
+ {}
71
+ end
72
+
73
+ protected
74
+
75
+
76
+ def self.add_data_validation(sheet, column_number, validation, allowBlank, parameters_sheet_name)
77
+ column_letter = number2column(column_number)
78
+ values_size = validation.is_a?(Array) ? validation.size : validation[:clazz].all.count
79
+ sheet.add_data_validation("#{column_letter}2:#{column_letter}1048576", type: :list, allowBlank: allowBlank, formula1: "#{parameters_sheet_name}!$#{column_letter}$2:$#{column_letter}$#{values_size+1}")
80
+ end
81
+
82
+ def self.create_validations(fields, workbook, parameters_sheet_name)
83
+ workbook.add_worksheet(name: parameters_sheet_name) do |sheet|
84
+ sheet.add_row [:id] | fields.map { |field| field[:field_name] }
85
+
86
+ cols_values = [[]].concat(fields.map { |field| field_validation_values(field) })
87
+
88
+ cols_values.map { |values| values.size }.max.times do |i|
89
+ row = cols_values.map { |v| v[i] }
90
+ sheet.add_row row, types: row.map{ |_| :string }
91
+ end
92
+ end
93
+ end
94
+
95
+ def self.resource_field_value(resource, field)
96
+ if field[:data_validation][:validation] && !field[:data_validation][:validation].is_a?(Array)
97
+ relation = resource.send(field[:field_name])
98
+ relation ? relation.send(field[:data_validation][:validation][:field]) : nil
99
+ else
100
+ resource.send(field[:field_name])
101
+ end
102
+ end
103
+
104
+ def self.field_validation_values(field)
105
+ if field[:data_validation][:validation]
106
+ if field[:data_validation][:validation].is_a?(Array)
107
+ field[:data_validation][:validation]
108
+ else
109
+ field[:data_validation][:validation][:clazz].all.order(field[:data_validation][:validation][:field]).pluck(field[:data_validation][:validation][:field])
110
+ end
111
+ else
112
+ []
113
+ end
114
+ end
115
+
116
+ def self.number2column(number)
117
+ Hash.new {|hash,key| hash[key] = hash[key - 1].next }.merge({0 => "A"})[number]
118
+ end
119
+
120
+ def self.validate_id(spreadsheet, clazz)
121
+ validate_in_array(0, clazz.all.pluck(:id).map(&:to_s), spreadsheet)
122
+ end
123
+
124
+ def self.validate_not_blank(col, spreadsheet)
125
+ (2..spreadsheet.last_row)
126
+ .map { |i| {i => !spreadsheet.row(i)[col].blank? } }
127
+ .select { |e| !e.values.first }
128
+ .map { |e| e.keys.first }
129
+ end
130
+
131
+ def self.validate_in_array(col, array, spreadsheet)
132
+ (2..spreadsheet.last_row)
133
+ .map { |i| { i => (spreadsheet.row(i)[col].blank? || array.map(&:downcase).include?(spreadsheet.row(i)[col].downcase)) } }
134
+ .select { |e| !e.values.first }
135
+ .map { |e| e.keys.first }
136
+ end
137
+
138
+ def self.fetch(row, col, spreadsheet)
139
+ spreadsheet.celltype(row, col + 1) == :float ? spreadsheet.send(:cell_to_csv, row, col + 1, spreadsheet.default_sheet) : row[col]
140
+ end
141
+
142
+ def self.row_value(row, col, field, spreadsheet)
143
+ if field[:data_validation][:validation] && !field[:data_validation][:validation].is_a?(Array)
144
+ field[:data_validation][:validation][:clazz].send("find_by_#{field[:data_validation][:validation][:field]}", fetch(row, col, spreadsheet))
145
+ else
146
+ fetch(row, col, spreadsheet)
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -1,3 +1,3 @@
1
1
  module CrujCrujCruj
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,6 +1,9 @@
1
1
  require "cruj_cruj_cruj/engine"
2
+ require "cruj_cruj_cruj/services/import_rules"
2
3
 
3
4
  module CrujCrujCruj
4
5
  require "ransack"
5
6
  require 'kaminari'
7
+ require 'roo'
8
+ require 'axlsx'
6
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cruj_cruj_cruj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Campos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-24 00:00:00.000000000 Z
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: roo
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: axlsx
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: sqlite3
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -81,12 +109,14 @@ files:
81
109
  - app/controllers/cruj_cruj_cruj_controller.rb
82
110
  - app/helpers/search_helper.rb
83
111
  - app/views/cruj_cruj_cruj/_filters.html.erb
112
+ - app/views/cruj_cruj_cruj/_import_export_template.html.erb
84
113
  - app/views/cruj_cruj_cruj/_list_table.html.erb
85
114
  - app/views/layouts/cruj_cruj_cruj/application.html.erb
86
115
  - config/locales/pt-BR.yml
87
116
  - config/routes.rb
88
117
  - lib/cruj_cruj_cruj.rb
89
118
  - lib/cruj_cruj_cruj/engine.rb
119
+ - lib/cruj_cruj_cruj/services/import_rules.rb
90
120
  - lib/cruj_cruj_cruj/version.rb
91
121
  - lib/tasks/cruj_cruj_cruj_tasks.rake
92
122
  - test/cruj_cruj_cruj_test.rb