zh_kostev_ext 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
|
|
1
|
+
module ControllerExtensions
|
2
|
+
module ExportToExcel
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
private
|
6
|
+
#This is method to create excel file with data and send it to user.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# class PermissionSetsController < ApplicationController
|
11
|
+
# def export_to_excel
|
12
|
+
# records = User.where(:super_admin => true).all #find record to export
|
13
|
+
# export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name, last_name))
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# You can specify file_name to provide name of excel file
|
17
|
+
#
|
18
|
+
# export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name, last_name), :file_name => "Super Admin excel export")
|
19
|
+
#
|
20
|
+
# You can specify worksheet_name to provide name of worksheet in excel file
|
21
|
+
#
|
22
|
+
# export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name, last_name), :file_name => "Super Admin excel export", :worksheet_name => "super admins")
|
23
|
+
#
|
24
|
+
def export_to_excel_prototype(records, column_names, model_column_names, options)
|
25
|
+
spreadsheet = StringIO.new
|
26
|
+
book = create_spreadsheet_for_export(records, column_names, model_column_names, options)
|
27
|
+
book.write spreadsheet
|
28
|
+
send_data spreadsheet.string, :filename => (options[:file_name].presence || "export_#{Time.now.to_s.underscore}.xls"), :type => "application/vnd.ms-excel"
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
#create workbook and write data to it
|
33
|
+
def create_spreadsheet_for_export(records, column_names, model_column_names, options = {})
|
34
|
+
book = Spreadsheet::Workbook.new #create new book for writing
|
35
|
+
sheet = book.create_worksheet :name => (options[:worksheet_name].presence || options[:file_name].presence || 'Excel export') #create new sheet in book
|
36
|
+
sheet.row(0).concat column_names #write first row(column names)
|
37
|
+
records.each_with_index do |record, index|
|
38
|
+
write_record_to_sheet(sheet, record, model_column_names, index + 1) #index+1 - begin from second line(first line is column names)
|
39
|
+
end
|
40
|
+
|
41
|
+
book
|
42
|
+
end
|
43
|
+
|
44
|
+
#write to worksheet one line of data
|
45
|
+
def write_record_to_sheet(sheet, record, model_column_names, row_index)
|
46
|
+
model_column_names.each_with_index do |name, column_index|
|
47
|
+
sheet[row_index, column_index] = record.send(name.underscore.to_sym)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -2,22 +2,31 @@ module ControllerExtensions
|
|
2
2
|
module GeneralMethods
|
3
3
|
def self.included(base)
|
4
4
|
base.class_eval do
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#class PermissionSetsController < ApplicationController
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
5
|
+
#Load resource or redirect to 404 if can't load resource. Used as before filter.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
#
|
9
|
+
# class PermissionSetsController < ApplicationController
|
10
|
+
# before_filter :find_resource_or_404, :only => [:edit,:show, :destroy, :update] #it will find model as PermissionSet.find(params[:id])
|
11
|
+
#
|
12
|
+
# You can specify DB_MODEL constant in controller if controller name doesn't connects with model which it used.
|
13
|
+
#
|
14
|
+
# DB_MODEL = :contact
|
15
|
+
#
|
16
|
+
# You can specify DB_SCOPE constant in controller if you need to find model in special scope.
|
17
|
+
#
|
18
|
+
# DB_SCOPE = :sent
|
19
|
+
#
|
20
|
+
# You can specify DB_SCOPE and DB_MODEL together
|
21
|
+
#
|
22
|
+
# class PermissionSetsController < ApplicationController
|
23
|
+
# DB_MODEL = :contact
|
24
|
+
# DB_SCOPE = :sent
|
25
|
+
# before_filter :find_resource_or_404, :only => [:edit,:show, :destroy, :update] #it will find model as Contact.sent.find(params[:id])
|
26
|
+
#
|
27
|
+
# Valid Options:
|
28
|
+
#
|
29
|
+
# * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified.
|
21
30
|
def find_resource_or_404
|
22
31
|
model_name = defined?(self.class::DB_MODEL) ? self.class::DB_MODEL : controller_name.singularize
|
23
32
|
model_to_find = defined?(self.class::DB_SCOPE) ? model_name.to_s.classify.constantize.send(self.class::DB_SCOPE) : model_name.to_s.classify.constantize
|
data/lib/zh_kostev_ext.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "controller_extensions/general_methods"
|
2
|
+
require "controller_extensions/export_to_excel"
|
2
3
|
class ActionController::Base
|
3
4
|
include ControllerExtensions::GeneralMethods
|
5
|
+
include ControllerExtensions::ExportToExcel
|
4
6
|
end
|
5
7
|
|
6
8
|
require "ruby_classes_extensions/hash_extensions"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zh_kostev_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: spreadsheet
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: shoulda
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -99,6 +115,7 @@ extra_rdoc_files:
|
|
99
115
|
- LICENSE.txt
|
100
116
|
- README.rdoc
|
101
117
|
files:
|
118
|
+
- lib/controller_extensions/export_to_excel.rb
|
102
119
|
- lib/controller_extensions/general_methods.rb
|
103
120
|
- lib/ruby_classes_extensions/hash_extensions.rb
|
104
121
|
- lib/zh_kostev_ext.rb
|
@@ -119,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
136
|
version: '0'
|
120
137
|
segments:
|
121
138
|
- 0
|
122
|
-
hash: -
|
139
|
+
hash: -764461171
|
123
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
141
|
none: false
|
125
142
|
requirements:
|