excelizer 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +73 -2
- data/README_ES.md +1 -1
- data/lib/excelizer.rb +1 -1
- data/lib/excelizer/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Excelizer
|
2
2
|
|
3
|
-
Excelizer handles Excel files generation for your Rails
|
3
|
+
Excelizer handles Excel files generation for your Rails models with the [Spreadsheet](https://github.com/zdavatz/spreadsheet) gem, using an API similar to the awesome `active_model_serializers`.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -16,9 +16,69 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install excelizer
|
18
18
|
|
19
|
+
You will need to add this line to the `config/initializers/mime_types.rb` file:
|
20
|
+
|
21
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
22
|
+
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
|
25
|
+
You should create a `downloaders` folder inside the `app` folder. You can define a downloader like this
|
26
|
+
|
27
|
+
class UserDownloader < Excelizer::Base
|
28
|
+
attr_downloadable :name, :last_name, :email, :birth_date
|
29
|
+
end
|
30
|
+
|
31
|
+
It's possible to redefine attributes using the `@model` reference
|
32
|
+
|
33
|
+
class UserDownloader < Excelizer::Base
|
34
|
+
attr_downloadable :name, :last_name, :email, :birth_date
|
35
|
+
|
36
|
+
def name
|
37
|
+
@model.name.titleize
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Or even create new attributes. Keep in mind new attributes will be added at the end of the row, but you can explicitly define the position where they should be. In this example, `phone_number` will go before `birth_date` while `favorite_color` will go after it.
|
42
|
+
|
43
|
+
class UserDownloader < Excelizer::Base
|
44
|
+
attr_downloadable :name, :last_name, :email, :phone_number, :birth_date
|
45
|
+
|
46
|
+
def phone_number
|
47
|
+
'51' + @model.mobile_phone
|
48
|
+
end
|
49
|
+
|
50
|
+
def favorite_color
|
51
|
+
@model.favorite_color || 'orange'
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
Now that we have a downloader, how do we actually use it? If you want to learn how to use it along ActiveAdmin, skip to the next section, if you just want to use the raw data, you can do this:
|
57
|
+
|
58
|
+
raw_data = UserDownloader.new.build_xls
|
59
|
+
|
60
|
+
You can optionally pass a collection as a parameter for scoped results:
|
61
|
+
|
62
|
+
raw_data = UserDownloader.new.build_xls(User.where(name: 'James'))
|
63
|
+
|
64
|
+
## ActiveAdmin
|
65
|
+
|
66
|
+
The recommended way to use this gem along ActiveAdmin is using an `action_item` and a `collection_action`. Future releases won't need this ;)
|
67
|
+
|
68
|
+
ActiveAdmin.register User do
|
69
|
+
|
70
|
+
collection_action :download_xls do
|
71
|
+
send_data UserDownloader.new.build_xls,
|
72
|
+
type: 'application/vnd.ms-excel',
|
73
|
+
filename: "user_report.xls"
|
74
|
+
end
|
75
|
+
|
76
|
+
action_item only: [:index] do
|
77
|
+
link_to "Download Excel", download_xls_admin_users_path
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
22
82
|
|
23
83
|
## Contributing
|
24
84
|
|
@@ -27,3 +87,14 @@ TODO: Write usage instructions here
|
|
27
87
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
88
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
89
|
5. Create new Pull Request
|
90
|
+
|
91
|
+
## Acknowledgments
|
92
|
+
|
93
|
+
Brought to you by Arturo Puente and the team at [Phantasia](http://www.phantasia.pe/).
|
94
|
+
|
95
|
+
[![Phantasia](http://www.phantasia.pe/wp-content/themes/phantasia/images/logo-phantasia-header.png)](http://www.phantasia.pe/)
|
96
|
+
|
97
|
+
## Changelog
|
98
|
+
|
99
|
+
0.0.8 Safer attribute initialization.
|
100
|
+
0.0.7 First release. Codename: Bond
|
data/README_ES.md
CHANGED
@@ -1 +1 @@
|
|
1
|
-
#
|
1
|
+
# Pronto
|
data/lib/excelizer.rb
CHANGED
data/lib/excelizer/version.rb
CHANGED