exportable 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +14 -0
  5. data/Gemfile.lock +144 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +104 -0
  8. data/Rakefile +31 -0
  9. data/bin/test +11 -0
  10. data/exportable.gemspec +27 -0
  11. data/lib/exportable.rb +23 -0
  12. data/lib/exportable/class_methods.rb +9 -0
  13. data/lib/exportable/export_methods.rb +14 -0
  14. data/lib/exportable/export_methods/csv_exporter.rb +33 -0
  15. data/lib/exportable/export_methods/xls_exporter.rb +44 -0
  16. data/lib/exportable/utils.rb +18 -0
  17. data/lib/exportable/version.rb +3 -0
  18. data/lib/tasks/exportable_tasks.rake +4 -0
  19. data/spec/dummy/Rakefile +6 -0
  20. data/spec/dummy/app/models/application_record.rb +3 -0
  21. data/spec/dummy/app/models/exportable_model.rb +4 -0
  22. data/spec/dummy/bin/bundle +3 -0
  23. data/spec/dummy/bin/rails +4 -0
  24. data/spec/dummy/bin/rake +4 -0
  25. data/spec/dummy/bin/setup +34 -0
  26. data/spec/dummy/bin/update +29 -0
  27. data/spec/dummy/config.ru +5 -0
  28. data/spec/dummy/config/application.rb +15 -0
  29. data/spec/dummy/config/boot.rb +5 -0
  30. data/spec/dummy/config/cable.yml +9 -0
  31. data/spec/dummy/config/database.yml +19 -0
  32. data/spec/dummy/config/environment.rb +5 -0
  33. data/spec/dummy/config/environments/test.rb +42 -0
  34. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  37. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/spec/dummy/config/initializers/inflections.rb +16 -0
  39. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  40. data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
  41. data/spec/dummy/config/initializers/session_store.rb +3 -0
  42. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/spec/dummy/config/locales/en.yml +23 -0
  44. data/spec/dummy/config/puma.rb +47 -0
  45. data/spec/dummy/config/routes.rb +3 -0
  46. data/spec/dummy/config/secrets.yml +22 -0
  47. data/spec/dummy/config/spring.rb +6 -0
  48. data/spec/dummy/db/migrate/20170107183658_create_exportable_models.rb +17 -0
  49. data/spec/dummy/db/schema.rb +30 -0
  50. data/spec/exportable/class_methods_spec.rb +15 -0
  51. data/spec/exportable/csv_exporter_spec.rb +69 -0
  52. data/spec/exportable/export_methods_spec.rb +31 -0
  53. data/spec/exportable/xls_exporter_spec.rb +56 -0
  54. data/spec/spec_helper.rb +128 -0
  55. data/spec/support/helper.rb +12 -0
  56. metadata +228 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8fd477c8ee9f17a65df411e7528b5b23f67bf71
4
+ data.tar.gz: 07c54ba2aed4aead783feb5aa3e078274ab5da0b
5
+ SHA512:
6
+ metadata.gz: 2c4db4467f13f0dd88789b19f7c7bfea31478f8cfe802a0ad7abe7415e32945257c49ef67623620e050b57909817dbac1eba546506205f7d28883548dc4ac71d
7
+ data.tar.gz: da18dc818b4616b0054a4cb72ba51fa3e8f0c451bc4fdd3ed434d10d207698bd3bc854b694d854928aec043b40d5a2cbbca678e73454c7cf04d9600abef1ab58
@@ -0,0 +1,7 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/db/*.sqlite3-journal
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in exportable.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
@@ -0,0 +1,144 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ exportabe (0.1.0)
5
+ activerecord (>= 4.0)
6
+ spreadsheet
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.0.1)
12
+ actionpack (= 5.0.1)
13
+ nio4r (~> 1.2)
14
+ websocket-driver (~> 0.6.1)
15
+ actionmailer (5.0.1)
16
+ actionpack (= 5.0.1)
17
+ actionview (= 5.0.1)
18
+ activejob (= 5.0.1)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.0.1)
22
+ actionview (= 5.0.1)
23
+ activesupport (= 5.0.1)
24
+ rack (~> 2.0)
25
+ rack-test (~> 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.0.1)
29
+ activesupport (= 5.0.1)
30
+ builder (~> 3.1)
31
+ erubis (~> 2.7.0)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
34
+ activejob (5.0.1)
35
+ activesupport (= 5.0.1)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.0.1)
38
+ activesupport (= 5.0.1)
39
+ activerecord (5.0.1)
40
+ activemodel (= 5.0.1)
41
+ activesupport (= 5.0.1)
42
+ arel (~> 7.0)
43
+ activesupport (5.0.1)
44
+ concurrent-ruby (~> 1.0, >= 1.0.2)
45
+ i18n (~> 0.7)
46
+ minitest (~> 5.1)
47
+ tzinfo (~> 1.1)
48
+ arel (7.1.4)
49
+ builder (3.2.2)
50
+ concurrent-ruby (1.0.4)
51
+ database_cleaner (1.5.3)
52
+ diff-lcs (1.2.5)
53
+ erubis (2.7.0)
54
+ globalid (0.3.7)
55
+ activesupport (>= 4.1.0)
56
+ i18n (0.7.0)
57
+ loofah (2.0.3)
58
+ nokogiri (>= 1.5.9)
59
+ mail (2.6.4)
60
+ mime-types (>= 1.16, < 4)
61
+ method_source (0.8.2)
62
+ mime-types (3.1)
63
+ mime-types-data (~> 3.2015)
64
+ mime-types-data (3.2016.0521)
65
+ mini_portile2 (2.1.0)
66
+ minitest (5.10.1)
67
+ nio4r (1.2.1)
68
+ nokogiri (1.7.0.1)
69
+ mini_portile2 (~> 2.1.0)
70
+ rack (2.0.1)
71
+ rack-test (0.6.3)
72
+ rack (>= 1.0)
73
+ rails (5.0.1)
74
+ actioncable (= 5.0.1)
75
+ actionmailer (= 5.0.1)
76
+ actionpack (= 5.0.1)
77
+ actionview (= 5.0.1)
78
+ activejob (= 5.0.1)
79
+ activemodel (= 5.0.1)
80
+ activerecord (= 5.0.1)
81
+ activesupport (= 5.0.1)
82
+ bundler (>= 1.3.0, < 2.0)
83
+ railties (= 5.0.1)
84
+ sprockets-rails (>= 2.0.0)
85
+ rails-dom-testing (2.0.2)
86
+ activesupport (>= 4.2.0, < 6.0)
87
+ nokogiri (~> 1.6)
88
+ rails-html-sanitizer (1.0.3)
89
+ loofah (~> 2.0)
90
+ railties (5.0.1)
91
+ actionpack (= 5.0.1)
92
+ activesupport (= 5.0.1)
93
+ method_source
94
+ rake (>= 0.8.7)
95
+ thor (>= 0.18.1, < 2.0)
96
+ rake (12.0.0)
97
+ rspec-core (3.5.4)
98
+ rspec-support (~> 3.5.0)
99
+ rspec-expectations (3.5.0)
100
+ diff-lcs (>= 1.2.0, < 2.0)
101
+ rspec-support (~> 3.5.0)
102
+ rspec-mocks (3.5.0)
103
+ diff-lcs (>= 1.2.0, < 2.0)
104
+ rspec-support (~> 3.5.0)
105
+ rspec-rails (3.5.2)
106
+ actionpack (>= 3.0)
107
+ activesupport (>= 3.0)
108
+ railties (>= 3.0)
109
+ rspec-core (~> 3.5.0)
110
+ rspec-expectations (~> 3.5.0)
111
+ rspec-mocks (~> 3.5.0)
112
+ rspec-support (~> 3.5.0)
113
+ rspec-support (3.5.0)
114
+ ruby-ole (1.2.12)
115
+ spreadsheet (1.1.4)
116
+ ruby-ole (>= 1.0)
117
+ sprockets (3.7.1)
118
+ concurrent-ruby (~> 1.0)
119
+ rack (> 1, < 3)
120
+ sprockets-rails (3.2.0)
121
+ actionpack (>= 4.0)
122
+ activesupport (>= 4.0)
123
+ sprockets (>= 3.0.0)
124
+ sqlite3 (1.3.13)
125
+ thor (0.19.4)
126
+ thread_safe (0.3.5)
127
+ tzinfo (1.2.2)
128
+ thread_safe (~> 0.1)
129
+ websocket-driver (0.6.4)
130
+ websocket-extensions (>= 0.1.0)
131
+ websocket-extensions (0.1.2)
132
+
133
+ PLATFORMS
134
+ ruby
135
+
136
+ DEPENDENCIES
137
+ database_cleaner
138
+ exportabe!
139
+ rails (>= 4.0)
140
+ rspec-rails
141
+ sqlite3
142
+
143
+ BUNDLED WITH
144
+ 1.12.5
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Sunil Antony
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # Exportable
2
+ Exportable will help you in exporting Rails ActiveRecord models in to different output formats including CSV, XLS etc with just a one liner hook in your model. This Gem is tested against Rails 4 and 5 versions. Support for more output formats will be coming soon.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'exportable'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install exportable
19
+ ```
20
+
21
+ ## Usage
22
+ Just add a one liner hook 'exportable' in your model and your model will be provided
23
+ with class level export methods.
24
+
25
+ ```ruby
26
+ class Article < ApplicationRecord
27
+ exportable
28
+ end
29
+ ```
30
+
31
+ You can then use export methods on the model with supporting formats (:csv, :xls) and field options.
32
+
33
+ ```ruby
34
+ Article.export(:csv)
35
+ Article.export(:csv, only: [:title, :user_id])
36
+ ```
37
+ You can also call export methods on model scopes or ActiveRecord::Relation Object.
38
+ ```ruby
39
+ Article.where(staus: 'published').export(:csv)
40
+ ```
41
+
42
+ There are also format specific export methods
43
+
44
+ ```ruby
45
+ Article.export_csv
46
+ Article.export_xls header: false
47
+ ```
48
+
49
+ These methods will provide a string in sepcified format which you can write directly to a file or stream through Rails controller.
50
+
51
+ ```ruby
52
+ File.open('output.xls', 'wb') {|f| f.write Article.export(:xls) }
53
+ ```
54
+ or in controller
55
+ ```ruby
56
+ send_data Article.export(:csv), filename: 'output.csv
57
+ ```
58
+
59
+
60
+ Following options are available for hook method and export methods. Please note that options in export methods will always take higher precedence.
61
+
62
+ ###:only
63
+
64
+ By default all the fields in model will be exported. You can controll the fields to export using 'only' option.
65
+
66
+ ```ruby
67
+ Article.export_csv only: [:title, :published_on, :status]
68
+ ```
69
+
70
+ ###:except
71
+
72
+ Omit exportable fields with 'except' option
73
+
74
+ ```ruby
75
+ Article.export_csv except: [:status]
76
+ ```
77
+
78
+ ###:header
79
+
80
+ By default exporting adds header row. Omit header by option 'header: false'
81
+
82
+ ###:methods
83
+
84
+ Exporting not only limited to model attributes. You can also add custom model methods to exportable fields.
85
+ ```ruby
86
+ Article.export methods: [:user_name]
87
+ ```
88
+ Note: *If your model method contains query to association please be carefull to eager load association.*
89
+ ```ruby
90
+ Article.includes(:user).export methods: [:user_name]
91
+ ```
92
+ ## Testing
93
+ This plugin uses Rspec for testing. Go to gem folder and run:
94
+
95
+ ```bash
96
+ $ rspec
97
+ ```
98
+
99
+
100
+ ## Contributing
101
+ More output formats are always welcome.
102
+
103
+ ## License
104
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,31 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Exportable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
19
+ load 'rails/tasks/engine.rake'
20
+
21
+
22
+ load 'rails/tasks/statistics.rake'
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+
29
+
30
+
31
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path(File.expand_path('../../test', __FILE__))
3
+
4
+ require 'bundler/setup'
5
+ require 'rails/test_unit/minitest_plugin'
6
+
7
+ Rails::TestUnitReporter.executable = 'bin/test'
8
+
9
+ Minitest.run_via[:rails] = true
10
+
11
+ require "active_support/testing/autorun"
@@ -0,0 +1,27 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "exportable/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = 'exportable'
9
+ s.version = Exportable::VERSION
10
+ s.authors = ['Sunil Antony']
11
+ s.email = ['chackoantonydaniel@gmail.com']
12
+ s.homepage = 'https://github.com/chackoantony/exportable'
13
+ s.summary = "Exporting Rails ActiveRecord models."
14
+ s.description = 'Exportable will help you in exporting ActiveRecord models in to different output formats including CSV, XLS etc.'
15
+ s.license = "MIT"
16
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ s.bindir = "exe"
18
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.test_files = Dir["spec/**/*"]
21
+ s.add_dependency 'activerecord', ['>= 4.0']
22
+ s.add_dependency 'spreadsheet'
23
+ s.add_development_dependency 'rails', ['>= 4.0']
24
+ s.add_development_dependency 'sqlite3'
25
+ s.add_development_dependency 'rspec-rails'
26
+ s.add_development_dependency 'database_cleaner'
27
+ end
@@ -0,0 +1,23 @@
1
+ # Core module for the Gem
2
+ module Exportable
3
+ extend ActiveSupport::Autoload
4
+ mattr_accessor :formats
5
+ self.formats = []
6
+
7
+ autoload :ClassMethods
8
+ autoload :ExportMethods
9
+ autoload :Utils
10
+
11
+ autoload_under 'export_methods' do
12
+ autoload :CsvExporter
13
+ autoload :XlsExporter
14
+ end
15
+
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+ end
20
+
21
+ ActiveSupport.on_load(:active_record) do
22
+ include Exportable
23
+ end
@@ -0,0 +1,9 @@
1
+ module Exportable
2
+ # This module holds Model level hook methods
3
+ module ClassMethods
4
+ def exportable(options = {})
5
+ @options = options
6
+ extend(ExportMethods)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Exportable
2
+ # This module serves as a container for export modules
3
+ module ExportMethods
4
+ include CsvExporter
5
+ include XlsExporter
6
+
7
+ def export(format, options = {})
8
+ unless Exportable.formats.include?(format.to_sym)
9
+ raise ArgumentError, "Unsupported output format '#{format}'"
10
+ end
11
+ send("export_#{format}", options)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ module Exportable
2
+ module ExportMethods
3
+ # Module to handle only CSV exporting
4
+ module CsvExporter
5
+ include Exportable::Utils
6
+
7
+ Exportable.formats << :csv
8
+
9
+ def export_csv(options = {})
10
+ export_options = get_export_options(self, @options.merge(options))
11
+ Exporter.new(self).export(export_options)
12
+ end
13
+
14
+ # Exporter class for CSV Exporter
15
+ class Exporter
16
+ require 'csv'
17
+
18
+ def initialize(model)
19
+ @model = model
20
+ end
21
+
22
+ def export(options)
23
+ CSV.generate do |csv|
24
+ csv << options[:fields].map(&:to_s) if options[:header]
25
+ @model.where(nil).find_each do |record|
26
+ csv << options[:fields].map { |attr| record.send(attr).to_s }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end