excelizer 0.2.0 → 1.0.0

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: 5b390feb3d9c71d037f02d5281e16e4c2d8a1170
4
- data.tar.gz: 514d9cb4db895a79dc5251bf6636cac2b2f5a505
3
+ metadata.gz: 20e271109d89c81cc1a53ee587aab79878088715
4
+ data.tar.gz: 2761d53f88cfb62e8f92bbfdca86496c772cf3db
5
5
  SHA512:
6
- metadata.gz: 21ade4130753293027f9f7d71f1a5e9891b4f7ba257f253b6f71628a3a097cb65c83e8758e20897bca4d65d72127a1eaa1c9c68bf84eadabf63d126703e9aadb
7
- data.tar.gz: b08af9f7a6f8f37425ac0022cf7adb005d9edb51ec1845295029656adae17f577f0491f55ccc60dd9c78c327f3d8a5297acb9e6cb898e3948e90731f510a9932
6
+ metadata.gz: 367d3ec30e8b1ca0a3f78fff0e9b33dfcdaa3c55f4af3daf681f7355f0ae8b4a021adfc04be9717ece013fc0036257451ea57b805c34c6389a65d405c89de30b
7
+ data.tar.gz: 7dea94a5908af21ab691d39b3ebb43cf6c1ede4bb38f29cff4a7a19b7191346317da47a853f82a69821a065aa0c31bb7a6138e07f80e995d3af699b31196ede8
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ --require spec_helper
3
+ --require rspec/legacy_formatters
4
+ --format=doc
5
+ --format=Nc
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.("routing/#{m[1]}_routing"),
51
+ rspec.spec.("controllers/#{m[1]}_controller"),
52
+ rspec.spec.("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/README.md CHANGED
@@ -34,50 +34,68 @@ You should create a `downloaders` folder inside the `app` folder. You can define
34
34
 
35
35
  ```ruby
36
36
  class UserDownloader < Excelizer::Base
37
- attr_downloadable :name, :last_name, :email, :birth_date
37
+ attribute :name
38
+ attribute :last_name
39
+ attribute :email
40
+ attribute :token
38
41
  end
39
42
  ```
40
43
 
41
- It's possible to redefine attributes using the `@model` reference
44
+ It's possible to redefine the attributes using the `object` reference
42
45
 
43
46
  ```ruby
44
47
  class UserDownloader < Excelizer::Base
45
- attr_downloadable :name, :last_name, :email, :birth_date
48
+ attribute :token
46
49
 
47
- def name
48
- @model.name.titleize
50
+ def token
51
+ object.token + rand(100)
49
52
  end
50
-
51
53
  end
52
54
  ```
53
55
 
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.
56
+ Or even create new attributes by defining them as methods:
55
57
 
56
58
  ```ruby
57
59
  class UserDownloader < Excelizer::Base
58
- attr_downloadable :name, :last_name, :email, :phone_number, :birth_date
60
+ attribute :full_name
59
61
 
60
- def phone_number
61
- '51' + @model.mobile_phone
62
+ def full_name
63
+ "#{object.name} #{object.last_name}"
62
64
  end
65
+ end
66
+ ```
67
+
68
+ And you can redefine the object variable by calling the `instance` method:
63
69
 
64
- def favorite_color
65
- @model.favorite_color || 'orange'
70
+ ```ruby
71
+ class UserDownloader < Excelizer::Base
72
+ instance :user
73
+ attribute :full_name
74
+
75
+ def full_name
76
+ "#{user.name} #{user.last_name}"
66
77
  end
78
+ end
79
+ ```
80
+
81
+ You can redefine the column name by passing the `header` option:
67
82
 
83
+ ```ruby
84
+ class UserDownloader < Excelizer::Base
85
+ attribute :full_name, header: "Nombre Completo"
68
86
  end
69
87
  ```
70
88
 
71
89
  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:
72
90
 
73
91
  ```ruby
74
- output = UserDownloader.new.build_xls
92
+ output = UserDownloader.new(User.all).download
75
93
  ```
76
94
 
77
95
  You can optionally pass a collection as a parameter for scoped results:
78
96
 
79
97
  ```ruby
80
- output = UserDownloader.new.build_xls(User.where(name: 'James'))
98
+ output = UserDownloader.new(User.where(name: "Jaime")).download
81
99
  ```
82
100
 
83
101
  ## ActiveAdmin
@@ -86,9 +104,8 @@ The recommended way to use this gem along ActiveAdmin is using an `action_item`
86
104
 
87
105
  ```ruby
88
106
  ActiveAdmin.register User do
89
-
90
107
  collection_action :download_xls do
91
- send_data UserDownloader.new.build_xls,
108
+ send_data UserDownloader.new(User.all).download,
92
109
  type: 'application/vnd.ms-excel',
93
110
  filename: "user_report.xls"
94
111
  end
@@ -96,11 +113,9 @@ ActiveAdmin.register User do
96
113
  action_item only: [:index] do
97
114
  link_to "Download Excel", download_xls_admin_users_path
98
115
  end
99
-
100
116
  end
101
117
  ```
102
118
 
103
-
104
119
  ## Contributing
105
120
 
106
121
  1. Fork it
@@ -111,10 +126,12 @@ end
111
126
 
112
127
  ## Changelog
113
128
 
114
- 0.1.0 Custom header support.
115
- 0.0.9 Adds support for Rails 4.
116
- 0.0.8 Safer attribute initialization.
117
- 0.0.7 First release.
129
+ - 1.0.0 Introduces brand new API
130
+ - 0.2.0 Fix custom header bug
131
+ - 0.1.0 Custom header support
132
+ - 0.0.9 Adds support for Rails 4
133
+ - 0.0.8 Safer attribute initialization
134
+ - 0.0.7 First release
118
135
 
119
136
  ## License
120
137
 
data/excelizer.gemspec CHANGED
@@ -20,6 +20,11 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.3"
24
+ spec.add_development_dependency "guard", "~> 2.13"
25
+ spec.add_development_dependency "guard-rspec", "~> 4.6"
26
+ spec.add_development_dependency "rspec-nc", "~> 0.2"
27
+ spec.add_development_dependency "rspec-legacy_formatters", "~> 1.0"
23
28
 
24
29
  spec.add_dependency "spreadsheet", "~> 1.0"
25
30
  end
data/lib/excelizer.rb CHANGED
@@ -1,66 +1,4 @@
1
1
  require "excelizer/version"
2
2
  require "excelizer/writer"
3
-
4
- module Excelizer
5
- class Base
6
- def self.attr_downloadable(*attrs)
7
- attrs.each do |attr|
8
- define_method(attr) do
9
- self.instance_variable_get("@#{attr}")
10
- end
11
- end
12
- end
13
-
14
- def self.attr_headers(*attrs)
15
- @@headers = attrs.to_a
16
- end
17
-
18
- def build_xls(collection=model_class.all)
19
- if defined?(@@headers)
20
- headers = @@headers
21
- else
22
- headers = *methods.select { |m| self.method(m).owner == self.class }.map { |m| m.to_s.titleize }
23
- end
24
- records = get_collection_data(collection)
25
-
26
- Excelizer::Writer.write headers, records
27
- end
28
-
29
- def get_collection_data(collection=model_class.all)
30
- model_methods = model_clean_attributes model_class
31
-
32
- # Gets the methods in the class definition and attr_downloadable
33
- own_methods = methods.select { |m| self.method(m).owner == self.class }
34
-
35
- # If the model class has the same method as the one defined in
36
- # attr_downloadble, that method will be called.
37
- collection.map do |model_instance|
38
- @model = model_instance
39
- own_methods.map do |attr|
40
- # Checks if the model has a method with the same name and
41
- # if the class definition overrides the model method
42
- reciever = self
43
- if model_methods.include?(attr.to_s) && reciever.send(attr).nil?
44
- reciever = model_instance
45
- end
46
- reciever.send(attr)
47
- end.compact
48
- end
49
- end
50
-
51
- def model_class
52
- Object.const_get self.class.name.demodulize.gsub "Downloader", ""
53
- end
54
-
55
- def model_clean_attributes(model_class_ref)
56
- explicit_attribute_keys = model_class_ref.new.attributes.keys
57
-
58
- if model_class_ref.respond_to?(:protected_attributes)
59
- explicit_attribute_keys - model_class_ref.protected_attributes.to_a
60
- else
61
- explicit_attribute_keys
62
- end
63
- end
64
-
65
- end
66
- end
3
+ require "excelizer/attribute"
4
+ require "excelizer/base"
@@ -0,0 +1,23 @@
1
+ module Excelizer
2
+ class Attribute
3
+
4
+ attr_reader :name
5
+
6
+ def initialize(name, options)
7
+ self.name = name
8
+ self.header = options[:header] unless options[:header].nil?
9
+ end
10
+
11
+ def header
12
+ (@header || default_header)
13
+ end
14
+
15
+ private
16
+
17
+ attr_writer :name, :header
18
+
19
+ def default_header
20
+ name.to_s.split("_").map(&:capitalize).join(" ")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,55 @@
1
+ module Excelizer
2
+ class Base
3
+
4
+ attr_reader :collection
5
+
6
+ def initialize(collection = [])
7
+ self.collection = collection
8
+ end
9
+
10
+ def download
11
+ Excelizer::Writer.write self.class.headers, records
12
+ end
13
+
14
+ def records
15
+ collection.map do |item|
16
+ @record = item
17
+ self.class.attributes.map do |attribute|
18
+ method = attribute.name
19
+ (respond_to?(method) ? send(method) : item.send(method)).to_s
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_writer :collection
27
+
28
+ class << self
29
+ def instance(instance_method)
30
+ self.instance_method = instance_method
31
+ define_method(instance_method) { @record }
32
+ end
33
+
34
+ def instance_method
35
+ @instance_method ||= :object
36
+ end
37
+
38
+ def attribute(name, options = {})
39
+ attributes << Attribute.new(name, options)
40
+ end
41
+
42
+ def attributes
43
+ @attributes ||= []
44
+ end
45
+
46
+ def headers
47
+ attributes.flat_map(&:header)
48
+ end
49
+
50
+ private
51
+
52
+ attr_writer :instance_method
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module Excelizer
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,16 +1,16 @@
1
1
  module Excelizer
2
2
  class Writer
3
- require 'spreadsheet' unless defined?(Spreadsheet)
3
+ require "spreadsheet" unless defined?(Spreadsheet)
4
4
 
5
5
  def self.write(headers, records)
6
6
  book = Spreadsheet::Workbook.new
7
7
  sheet = book.create_worksheet
8
-
8
+
9
9
  sheet.row(0).push *headers
10
10
  records.each_with_index do |record, index|
11
11
  sheet.row(index + 1).push *record
12
12
  end
13
-
13
+
14
14
  self.persist book
15
15
  end
16
16
 
@@ -0,0 +1,94 @@
1
+ require "spec_helper"
2
+
3
+ describe Excelizer::Base do
4
+ let(:downloader_class) { Class.new(described_class) }
5
+ let(:collection) do
6
+ 3.times.map do |i|
7
+ OpenStruct.new(id: i, name: "User #{i}", last_name: "Last Name #{i}")
8
+ end
9
+ end
10
+ let(:downloader) { downloader_class.new(collection) }
11
+
12
+ describe "#initialize" do
13
+ it "receives a collection" do
14
+ expect(downloader.collection).to eq(collection)
15
+ end
16
+ end
17
+
18
+ describe ".instance" do
19
+ context "without .instance call" do
20
+ it "sets instance to :object" do
21
+ expect(downloader_class.instance_method).to eq(:object)
22
+ end
23
+ end
24
+
25
+ it "sets the instance to the value passed" do
26
+ downloader_class.instance :user
27
+ expect(downloader_class.instance_method).to eq(:user)
28
+ end
29
+ end
30
+
31
+ describe "#instance" do
32
+ before do
33
+ downloader_class.attribute(:email)
34
+
35
+ downloader_class.instance :user
36
+ downloader_class.send(:define_method, :email) do
37
+ "#{user.id}@apple.com"
38
+ end
39
+ end
40
+
41
+ it "should include the user instance method in the email method" do
42
+ expect(downloader.records.first).to eq(["0@apple.com"])
43
+ end
44
+ end
45
+
46
+ describe ".attribute" do
47
+ it "pushes an attribute to a set of attributes" do
48
+ downloader_class.attribute :first_name
49
+ expect(downloader_class.attributes.size).to eq(1)
50
+ expect(downloader_class.attributes.first.name).to eq(:first_name)
51
+ end
52
+
53
+ context "when using a header option" do
54
+ it "pushes the attribute along with the header to the set of attributes" do
55
+ downloader_class.attribute :first_name, header: "Primer nombre"
56
+ expect(downloader_class.attributes.first.header).to eq("Primer nombre")
57
+ end
58
+ end
59
+ end
60
+
61
+ describe ".headers" do
62
+ before do
63
+ downloader_class.attribute(:id)
64
+ downloader_class.attribute(:last_name)
65
+ end
66
+
67
+ it "returns the attributes names" do
68
+ expect(downloader_class.headers).to eq(["Id", "Last Name"])
69
+ end
70
+
71
+ context "when using a custom header" do
72
+ before do
73
+ downloader_class.attribute(:username, header: "Usuario")
74
+ end
75
+
76
+ it "should assign the header to the attribute" do
77
+ expect(downloader_class.headers).to eq(["Id", "Last Name", "Usuario"])
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "#build_records" do
83
+ before do
84
+ downloader_class.attribute(:id)
85
+ downloader_class.attribute(:name)
86
+ downloader_class.attribute(:last_name)
87
+ end
88
+
89
+ it "should generate a collection of writable records" do
90
+ expect(downloader.records.size).to eq(3)
91
+ expect(downloader.records.last).to eq(["2", "User 2", "Last Name 2"])
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe Excelizer::Writer do
4
+
5
+ it "should work" do
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require "rubygems"
2
+
3
+ require_relative "../lib/excelizer"
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excelizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arturo Puente
@@ -38,6 +38,76 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '4.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '4.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-nc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '0.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '0.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-legacy_formatters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
41
111
  - !ruby/object:Gem::Dependency
42
112
  name: spreadsheet
43
113
  requirement: !ruby/object:Gem::Requirement
@@ -61,14 +131,20 @@ extensions: []
61
131
  extra_rdoc_files: []
62
132
  files:
63
133
  - .gitignore
134
+ - .rspec
64
135
  - Gemfile
136
+ - Guardfile
65
137
  - LICENSE.txt
66
138
  - README.md
67
139
  - excelizer.gemspec
68
140
  - lib/excelizer.rb
141
+ - lib/excelizer/attribute.rb
142
+ - lib/excelizer/base.rb
69
143
  - lib/excelizer/version.rb
70
144
  - lib/excelizer/writer.rb
71
- - test/spec.rb
145
+ - spec/excelizer/base_spec.rb
146
+ - spec/excelizer/writer_spec.rb
147
+ - spec/spec_helper.rb
72
148
  homepage: https://github.com/arturopuente/excelizer
73
149
  licenses:
74
150
  - MIT
@@ -94,5 +170,7 @@ signing_key:
94
170
  specification_version: 4
95
171
  summary: An Excel helper for Rails project
96
172
  test_files:
97
- - test/spec.rb
173
+ - spec/excelizer/base_spec.rb
174
+ - spec/excelizer/writer_spec.rb
175
+ - spec/spec_helper.rb
98
176
  has_rdoc:
data/test/spec.rb DELETED
File without changes