collection2csv 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/.travis.yml +24 -6
- data/Appraisals +23 -0
- data/Gemfile +1 -1
- data/README.md +17 -5
- data/Rakefile +17 -2
- data/app/controllers/collection2csv_controller.rb +4 -22
- data/app/helpers/collection_to_csv_helper.rb +6 -3
- data/collection2csv.gemspec +4 -2
- data/gemfiles/rails_4.0.gemfile +9 -0
- data/gemfiles/rails_4.1.gemfile +9 -0
- data/gemfiles/rails_4.2.gemfile +9 -0
- data/gemfiles/rails_5.0.gemfile +9 -0
- data/lib/collection2csv.rb +3 -4
- data/lib/collection2csv/convertor.rb +104 -0
- data/lib/collection2csv/version.rb +1 -1
- metadata +28 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a03592303be2e3e37bc565307258beff0c0676d
|
4
|
+
data.tar.gz: 4f9aa3248e6ae78e160d63c02a6e8037ed1f23f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1294c69e6506a36223dc725ab176737fed4169d78aabfa7ccd773660321c9ca7122949f051377629142e776535682f04488afc38f394f4bb36666032853085c9
|
7
|
+
data.tar.gz: 9e3dcbee2cb5512aec40caeb3b85a26081c292c6888a887a572bbee523ab3c42933d9659513c47f63b9f31e6791c649e3b8ee252034a5682a9c212cb2bf60c33
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
CHANGED
@@ -1,8 +1,26 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
-
|
4
|
-
- 2.
|
5
|
-
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
- 2.2.5
|
4
|
+
- 2.3.1
|
5
|
+
- ruby-head
|
6
|
+
|
7
|
+
before_install: gem install bundler
|
8
|
+
|
9
|
+
before_script:
|
10
|
+
- cd spec/dummy_app
|
11
|
+
- bundle exec rake db:create db:migrate
|
12
|
+
- cd ../../
|
13
|
+
|
14
|
+
|
15
|
+
cache: bundler
|
16
|
+
|
17
|
+
|
18
|
+
gemfile:
|
19
|
+
- gemfiles/rails_4.0.gemfile
|
20
|
+
- gemfiles/rails_4.1.gemfile
|
21
|
+
- gemfiles/rails_4.2.gemfile
|
22
|
+
- gemfiles/rails_5.0.gemfile
|
23
|
+
|
24
|
+
matrix:
|
25
|
+
allow_failures:
|
26
|
+
- rvm: ruby-head
|
data/Appraisals
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
appraise "rails-4.0" do
|
2
|
+
gem "rails", "4.0"
|
3
|
+
gem "sqlite3"
|
4
|
+
gem "database_cleaner"
|
5
|
+
end
|
6
|
+
|
7
|
+
appraise "rails-4.1" do
|
8
|
+
gem "rails", "4.1"
|
9
|
+
gem "sqlite3"
|
10
|
+
gem "database_cleaner"
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise "rails-4.2" do
|
14
|
+
gem "rails", "4.2"
|
15
|
+
gem "sqlite3"
|
16
|
+
gem "database_cleaner"
|
17
|
+
end
|
18
|
+
|
19
|
+
appraise "rails-5.0" do
|
20
|
+
gem "rails", "5.0"
|
21
|
+
gem "sqlite3"
|
22
|
+
gem "database_cleaner"
|
23
|
+
end
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -26,14 +26,27 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
Use the collection_download helper like any other regular tag helper :
|
28
28
|
|
29
|
-
|
29
|
+
<%= collection_download(@activerecord_collection) %>
|
30
|
+
|
31
|
+
eg: <%= collection_download(@users) %>
|
30
32
|
|
31
33
|
In order to select a particular columns use column names as follows
|
32
34
|
|
33
|
-
|
35
|
+
<%= collection_download(@activerecord_collection, {columns: ['id','name']}) %>
|
36
|
+
|
37
|
+
eg: <%= collection_download(@users, {columns: ['id','name']}) %>
|
38
|
+
|
39
|
+
Download link text can be provided as follows
|
40
|
+
|
41
|
+
<%= collection_download(@activerecord_collection, {link_text: 'export'}) %>
|
34
42
|
|
35
|
-
|
43
|
+
eg: <%= collection_download(@users, {link_text: 'export'}) %>
|
36
44
|
|
45
|
+
Supports Associations
|
46
|
+
|
47
|
+
<%= collection_download(@activerecord_collection, {columns: ['id','name'], associations: {association_name: ['id','name']}}) %>
|
48
|
+
|
49
|
+
eg: <%= collection_download(@users, {columns: ['id','name'], associations: {book: ['name', 'author']}}) %>
|
37
50
|
|
38
51
|
## Contributing
|
39
52
|
|
@@ -42,5 +55,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ethira
|
|
42
55
|
|
43
56
|
## License
|
44
57
|
|
45
|
-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
46
|
-
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,4 +1,19 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
1
3
|
require "bundler/gem_tasks"
|
2
4
|
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
|
5
|
+
require "appraisal"
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
|
10
|
+
task :default do
|
11
|
+
system('bundle exec rake appraisal spec')
|
12
|
+
end
|
13
|
+
task :test do
|
14
|
+
system('bundle exec rake appraisal spec')
|
15
|
+
end
|
16
|
+
else
|
17
|
+
task :default => :spec
|
18
|
+
task :test => :spec
|
19
|
+
end
|
@@ -1,27 +1,9 @@
|
|
1
1
|
require 'csv'
|
2
|
+
require 'collection2csv/convertor'
|
2
3
|
class Collection2csvController < ApplicationController
|
4
|
+
|
3
5
|
def convert
|
4
|
-
|
5
|
-
if params[:column_names].present?
|
6
|
-
column_names = params[:column_names].split(',').to_a
|
7
|
-
else
|
8
|
-
column_names = klass.column_names
|
9
|
-
end
|
10
|
-
collection = klass.find(params[:ids])
|
11
|
-
respond_to do |format|
|
12
|
-
format.csv { send_data to_csv(collection,column_names) ,:filename => "#{klass}_#{DateTime.now.to_formatted_s(:db)}.csv"
|
13
|
-
}
|
14
|
-
end
|
6
|
+
send_data Collection2csv::Convertor.new(params).perform, filename: "#{params[:klass]}_#{DateTime.now.to_formatted_s(:db)}.csv"
|
15
7
|
end
|
16
8
|
|
17
|
-
|
18
|
-
|
19
|
-
def to_csv(collection,column_names)
|
20
|
-
CSV.generate do |csv|
|
21
|
-
csv << column_names.map(&:humanize)
|
22
|
-
collection.each do |product|
|
23
|
-
csv << product.attributes.values_at(*column_names)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
9
|
+
end
|
@@ -1,8 +1,11 @@
|
|
1
1
|
module CollectionToCsvHelper
|
2
|
-
def collection_download(collection,
|
2
|
+
def collection_download(collection,options={})
|
3
|
+
column_names = options[:columns].presence
|
4
|
+
associations = options[:associations].presence
|
5
|
+
link_text = options[:link_text].presence || 'Download'
|
3
6
|
klass = collection.try(:first).class
|
4
|
-
raise "#{collection} is not a ActiveRecord collection" unless klass.ancestors.include?(ActiveRecord::Base)
|
7
|
+
raise(RuntimeError, "#{collection} is not a ActiveRecord collection") unless klass.ancestors.include?(ActiveRecord::Base)
|
5
8
|
collection_ids = collection.map(&:id)
|
6
|
-
link_to
|
9
|
+
link_to link_text , collection2csv_path(ids: collection_ids,klass: klass,format: "csv",column_names: column_names,associations: associations)
|
7
10
|
end
|
8
11
|
end
|
data/collection2csv.gemspec
CHANGED
@@ -23,12 +23,14 @@ Gem::Specification.new do |spec|
|
|
23
23
|
end
|
24
24
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.test_files = `git ls-files -- Appraisals {spec,gemfiles}/*`.split("\n")
|
26
27
|
spec.bindir = "exe"
|
27
28
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
29
|
spec.require_paths = ["lib"]
|
29
|
-
spec.required_ruby_version = '>=
|
30
|
+
spec.required_ruby_version = '>= 2.0.0'
|
30
31
|
spec.add_development_dependency "bundler", "~> 1.10"
|
31
32
|
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
-
spec.add_development_dependency "rspec"
|
33
|
+
spec.add_development_dependency "rspec-rails"
|
34
|
+
spec.add_development_dependency "appraisal"
|
33
35
|
|
34
36
|
end
|
data/lib/collection2csv.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
require "collection2csv/version"
|
2
|
+
require "collection2csv/convertor"
|
2
3
|
|
3
4
|
module Collection2csv
|
4
5
|
class Engine < Rails::Engine
|
5
6
|
initializer "Collection2csv.load_app_instance_data" do |app|
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
7
|
+
config.app_root = app.root
|
8
|
+
end
|
10
9
|
end
|
11
10
|
end
|
12
11
|
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'csv'
|
2
|
+
module Collection2csv
|
3
|
+
class Convertor
|
4
|
+
def initialize(params)
|
5
|
+
@klass = params[:klass].constantize
|
6
|
+
@column_names_from_params = params[:column_names].presence
|
7
|
+
@associations_from_params = params[:associations].presence
|
8
|
+
@column_names = @column_names_from_params || @klass.column_names
|
9
|
+
@associations = @associations_from_params || {}
|
10
|
+
@association_class_names = {}
|
11
|
+
@association_column_names = []
|
12
|
+
@primary_ids = params[:ids]
|
13
|
+
end
|
14
|
+
|
15
|
+
def perform
|
16
|
+
validate
|
17
|
+
download
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def validate
|
23
|
+
validate_columns if @column_names_from_params.present?
|
24
|
+
validate_associations if @associations_from_params.present?
|
25
|
+
end
|
26
|
+
|
27
|
+
def download
|
28
|
+
@collections = @klass.includes(@associations.try(:keys)).find(@primary_ids)
|
29
|
+
to_csv(@collections,@column_names,@associations)
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate_columns
|
33
|
+
false_columns = @column_names - @klass.column_names
|
34
|
+
raise_error(false_columns,'column',@klass.name) unless false_columns.empty?
|
35
|
+
end
|
36
|
+
|
37
|
+
def raise_error(false_entities, pluralizer, source = nil)
|
38
|
+
msg = construct_error_msg(false_entities, pluralizer)
|
39
|
+
msg << ' in ' if source.present?
|
40
|
+
raise msg + source
|
41
|
+
end
|
42
|
+
|
43
|
+
def construct_error_msg(false_entities,pluralizer)
|
44
|
+
false_entities.join(', ') + ' ' + pluralizer.pluralize(false_entities.count) + ' not found'
|
45
|
+
end
|
46
|
+
|
47
|
+
def validate_associations
|
48
|
+
unless @associations.empty?
|
49
|
+
get_association_class_names
|
50
|
+
validate_association_column_names
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def validate_association_keys
|
55
|
+
non_associative_keys = @associations.keys - @klass.reflections.keys
|
56
|
+
raise_error(non_associative_keys,'association') unless non_associative_keys.empty?
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_association_class_names
|
60
|
+
@klass.reflect_on_all_associations.each do |association|
|
61
|
+
@association_class_names["#{association.name}"] = association.options[:class_name].presence || association.name.to_s.camelize
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def validate_association_column_names
|
66
|
+
@associations.each do |association_key , association_value|
|
67
|
+
false_columns = association_value - @association_class_names[association_key.to_s].constantize.column_names
|
68
|
+
raise_error(false_columns,'column',association_key) unless false_columns.empty?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def generate_header
|
73
|
+
@header = @column_names.map(&:camelize)
|
74
|
+
@header += association_column_names
|
75
|
+
end
|
76
|
+
|
77
|
+
def association_column_names
|
78
|
+
@associations.each do | association_key, association_value |
|
79
|
+
association_value.each { |column_name| @association_column_names << "#{association_key}_#{column_name}" }
|
80
|
+
end
|
81
|
+
@association_column_names.map(&:camelize)
|
82
|
+
end
|
83
|
+
|
84
|
+
def generate_column_data(collection)
|
85
|
+
data = collection.attributes.values_at(*@column_names)
|
86
|
+
data = @associations.empty? ? data : generate_column_data_for_association(collection, data)
|
87
|
+
end
|
88
|
+
|
89
|
+
def generate_column_data_for_association(collection, data)
|
90
|
+
@associations.each do |association_key , association_value |
|
91
|
+
obj = collection.public_send(association_key)
|
92
|
+
data += obj.present? ? obj.attributes.values_at(*association_value) : Array.new(association_value.count,"")
|
93
|
+
end
|
94
|
+
data
|
95
|
+
end
|
96
|
+
|
97
|
+
def to_csv(collections,column_names,associations)
|
98
|
+
CSV.generate do |csv|
|
99
|
+
csv << generate_header
|
100
|
+
@collections.each { |collection| csv << generate_column_data(collection) }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collection2csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ethiraj
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -39,7 +39,21 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: appraisal
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - ">="
|
@@ -60,7 +74,9 @@ extensions: []
|
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
76
|
- ".gitignore"
|
77
|
+
- ".rspec"
|
63
78
|
- ".travis.yml"
|
79
|
+
- Appraisals
|
64
80
|
- CODE_OF_CONDUCT.md
|
65
81
|
- Gemfile
|
66
82
|
- LICENSE.txt
|
@@ -72,7 +88,12 @@ files:
|
|
72
88
|
- bin/setup
|
73
89
|
- collection2csv.gemspec
|
74
90
|
- config/routes.rb
|
91
|
+
- gemfiles/rails_4.0.gemfile
|
92
|
+
- gemfiles/rails_4.1.gemfile
|
93
|
+
- gemfiles/rails_4.2.gemfile
|
94
|
+
- gemfiles/rails_5.0.gemfile
|
75
95
|
- lib/collection2csv.rb
|
96
|
+
- lib/collection2csv/convertor.rb
|
76
97
|
- lib/collection2csv/version.rb
|
77
98
|
homepage: https://github.com/ethirajsrinivasan/collection2csv
|
78
99
|
licenses:
|
@@ -87,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
108
|
requirements:
|
88
109
|
- - ">="
|
89
110
|
- !ruby/object:Gem::Version
|
90
|
-
version:
|
111
|
+
version: 2.0.0
|
91
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
113
|
requirements:
|
93
114
|
- - ">="
|
@@ -95,8 +116,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
116
|
version: '0'
|
96
117
|
requirements: []
|
97
118
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.
|
119
|
+
rubygems_version: 2.5.1
|
99
120
|
signing_key:
|
100
121
|
specification_version: 4
|
101
122
|
summary: Download ActiveRecord collection as csv
|
102
|
-
test_files:
|
123
|
+
test_files:
|
124
|
+
- Appraisals
|