excelizer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -45
- data/excelizer.gemspec +2 -2
- data/lib/excelizer/version.rb +1 -1
- data/lib/excelizer.rb +6 -6
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b390feb3d9c71d037f02d5281e16e4c2d8a1170
|
4
|
+
data.tar.gz: 514d9cb4db895a79dc5251bf6636cac2b2f5a505
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21ade4130753293027f9f7d71f1a5e9891b4f7ba257f253b6f71628a3a097cb65c83e8758e20897bca4d65d72127a1eaa1c9c68bf84eadabf63d126703e9aadb
|
7
|
+
data.tar.gz: b08af9f7a6f8f37425ac0022cf7adb005d9edb51ec1845295029656adae17f577f0491f55ccc60dd9c78c327f3d8a5297acb9e6cb898e3948e90731f510a9932
|
data/README.md
CHANGED
@@ -6,79 +6,99 @@ Excelizer handles Excel files generation for your Rails models with the [Spreads
|
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'excelizer'
|
11
|
+
```
|
10
12
|
|
11
13
|
And then execute:
|
12
14
|
|
13
|
-
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
14
18
|
|
15
19
|
Or install it yourself as:
|
16
20
|
|
17
|
-
|
21
|
+
```bash
|
22
|
+
$ gem install excelizer
|
23
|
+
```
|
18
24
|
|
19
25
|
You will need to add this line to the `config/initializers/mime_types.rb` file:
|
20
26
|
|
21
|
-
|
27
|
+
```ruby
|
28
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
29
|
+
```
|
22
30
|
|
23
31
|
## Usage
|
24
32
|
|
25
33
|
You should create a `downloaders` folder inside the `app` folder. You can define a downloader like this
|
26
34
|
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
```ruby
|
36
|
+
class UserDownloader < Excelizer::Base
|
37
|
+
attr_downloadable :name, :last_name, :email, :birth_date
|
38
|
+
end
|
39
|
+
```
|
30
40
|
|
31
|
-
It's possible to redefine attributes using the `@model` reference
|
41
|
+
It's possible to redefine attributes using the `@model` reference
|
32
42
|
|
33
|
-
|
34
|
-
|
43
|
+
```ruby
|
44
|
+
class UserDownloader < Excelizer::Base
|
45
|
+
attr_downloadable :name, :last_name, :email, :birth_date
|
35
46
|
|
36
|
-
|
37
|
-
|
38
|
-
|
47
|
+
def name
|
48
|
+
@model.name.titleize
|
49
|
+
end
|
39
50
|
|
40
|
-
|
51
|
+
end
|
52
|
+
```
|
41
53
|
|
42
|
-
Or even create new attributes
|
54
|
+
Or even create new attributes by defining them as methods. Keep in mind the declaration order of the attributes matters: `name` will be column `A` whereas `last_name` will be column `B`. In this example, `phone_number` will go before `birth_date` while `favorite_color` will go after it.
|
43
55
|
|
44
|
-
|
45
|
-
|
56
|
+
```ruby
|
57
|
+
class UserDownloader < Excelizer::Base
|
58
|
+
attr_downloadable :name, :last_name, :email, :phone_number, :birth_date
|
46
59
|
|
47
|
-
|
48
|
-
|
49
|
-
|
60
|
+
def phone_number
|
61
|
+
'51' + @model.mobile_phone
|
62
|
+
end
|
50
63
|
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
def favorite_color
|
65
|
+
@model.favorite_color || 'orange'
|
66
|
+
end
|
54
67
|
|
55
|
-
|
68
|
+
end
|
69
|
+
```
|
56
70
|
|
57
|
-
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
|
71
|
+
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 output, you can do this:
|
58
72
|
|
59
|
-
|
73
|
+
```ruby
|
74
|
+
output = UserDownloader.new.build_xls
|
75
|
+
```
|
60
76
|
|
61
77
|
You can optionally pass a collection as a parameter for scoped results:
|
62
|
-
|
63
|
-
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
output = UserDownloader.new.build_xls(User.where(name: 'James'))
|
81
|
+
```
|
64
82
|
|
65
83
|
## ActiveAdmin
|
66
84
|
|
67
85
|
The recommended way to use this gem along ActiveAdmin is using an `action_item` and a `collection_action`. Future releases won't need this ;)
|
68
86
|
|
69
|
-
|
87
|
+
```ruby
|
88
|
+
ActiveAdmin.register User do
|
70
89
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
90
|
+
collection_action :download_xls do
|
91
|
+
send_data UserDownloader.new.build_xls,
|
92
|
+
type: 'application/vnd.ms-excel',
|
93
|
+
filename: "user_report.xls"
|
94
|
+
end
|
76
95
|
|
77
|
-
|
78
|
-
|
79
|
-
|
96
|
+
action_item only: [:index] do
|
97
|
+
link_to "Download Excel", download_xls_admin_users_path
|
98
|
+
end
|
80
99
|
|
81
|
-
|
100
|
+
end
|
101
|
+
```
|
82
102
|
|
83
103
|
|
84
104
|
## Contributing
|
@@ -89,18 +109,12 @@ The recommended way to use this gem along ActiveAdmin is using an `action_item`
|
|
89
109
|
4. Push to the branch (`git push origin my-new-feature`)
|
90
110
|
5. Create new Pull Request
|
91
111
|
|
92
|
-
## Acknowledgments
|
93
|
-
|
94
|
-
Brought to you by Arturo Puente and the team at [Phantasia](http://www.phantasia.pe/).
|
95
|
-
|
96
|
-
[![Phantasia](http://www.phantasia.pe/wp-content/themes/phantasia/images/logo-phantasia-header.png)](http://www.phantasia.pe/)
|
97
|
-
|
98
112
|
## Changelog
|
99
113
|
|
100
114
|
0.1.0 Custom header support.
|
101
115
|
0.0.9 Adds support for Rails 4.
|
102
116
|
0.0.8 Safer attribute initialization.
|
103
|
-
0.0.7 First release.
|
117
|
+
0.0.7 First release.
|
104
118
|
|
105
119
|
## License
|
106
120
|
|
data/excelizer.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
-
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
|
24
|
-
spec.add_dependency "spreadsheet"
|
24
|
+
spec.add_dependency "spreadsheet", "~> 1.0"
|
25
25
|
end
|
data/lib/excelizer/version.rb
CHANGED
data/lib/excelizer.rb
CHANGED
@@ -12,12 +12,12 @@ module Excelizer
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.attr_headers(*attrs)
|
15
|
-
|
15
|
+
@@headers = attrs.to_a
|
16
16
|
end
|
17
17
|
|
18
18
|
def build_xls(collection=model_class.all)
|
19
|
-
if
|
20
|
-
headers =
|
19
|
+
if defined?(@@headers)
|
20
|
+
headers = @@headers
|
21
21
|
else
|
22
22
|
headers = *methods.select { |m| self.method(m).owner == self.class }.map { |m| m.to_s.titleize }
|
23
23
|
end
|
@@ -49,12 +49,12 @@ module Excelizer
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def model_class
|
52
|
-
Object.const_get self.class.name.gsub "Downloader", ""
|
52
|
+
Object.const_get self.class.name.demodulize.gsub "Downloader", ""
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def model_clean_attributes(model_class_ref)
|
56
56
|
explicit_attribute_keys = model_class_ref.new.attributes.keys
|
57
|
-
|
57
|
+
|
58
58
|
if model_class_ref.respond_to?(:protected_attributes)
|
59
59
|
explicit_attribute_keys - model_class_ref.protected_attributes.to_a
|
60
60
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: excelizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arturo Puente
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,30 +28,30 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
33
|
+
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
40
|
+
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: spreadsheet
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
47
|
+
version: '1.0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
54
|
+
version: '1.0'
|
55
55
|
description: An Excel helper for Rails project. It integrates with ActiveRecord models
|
56
56
|
and other space magic.
|
57
57
|
email:
|
@@ -89,9 +89,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.
|
92
|
+
rubygems_version: 2.4.7
|
93
93
|
signing_key:
|
94
94
|
specification_version: 4
|
95
95
|
summary: An Excel helper for Rails project
|
96
96
|
test_files:
|
97
97
|
- test/spec.rb
|
98
|
+
has_rdoc:
|