to_csv_rails 0.2 → 0.3.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5105d7122e58daf4b55ac46da69f2131cc795121
4
+ data.tar.gz: 83a50deefae4a655f73a8d9827b6b6b61727a1b2
5
+ SHA512:
6
+ metadata.gz: 349317ef82b6444217c2f8b7b90bae13a5e0999decc30163ec39e04629fd95bc8df6fd2918750d27da591be3c3721600cb724fb672bceabad027928a879d320b
7
+ data.tar.gz: 5c94695c153188289f332d8e2a950d5c17301587d608dc14e64ecb009f0ebf4c3457d00dcd5d3671f01c0d860ef0957188a0f4bde5d5e181787c5f1a163d96ff
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.3.0] - 2017-11-01
8
+
9
+ ### Added
10
+ - Support to Rails 5
11
+ - Changelog to project
12
+
13
+ ### Removed
14
+ - Drop support to Ruby < 2
15
+
16
+ [0.3.0]: https://github.com/fernandomm/to_csv_rails/compare/v0.2...v0.3.0
17
+ [0.2]: https://github.com/fernandomm/to_csv_rails/compare/v0.1...v0.2
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
- to\_csv\_rails
2
- ============
1
+ # to\_csv\_rails
2
+
3
+ [![Build Status](https://api.travis-ci.org/fernandomm/to_csv_rails.png?branch=master)][travis]
4
+ [travis]: http://travis-ci.org/fernandomm/to_csv_rails
3
5
 
4
6
  to\_csv\_rails implements a to_csv method on your Arrays. It makes exporting records to csv a simple task, even when using responders.
5
7
 
@@ -59,3 +61,5 @@ To:
59
61
  format.csv { render csv: @users }
60
62
  end
61
63
  end
64
+
65
+ Accessing /users.csv will return your users in csv format.
data/lib/to_csv_rails.rb CHANGED
@@ -1,24 +1,15 @@
1
1
 
2
- if RUBY_VERSION >= '1.9'
3
- require 'csv'
4
- CSV_HANDLER = CSV
5
- else
6
- begin
7
- gem 'fastercsv'
8
- require 'fastercsv'
2
+ require 'csv'
3
+ require 'to_csv_rails/export'
4
+ require 'to_csv_rails/version'
9
5
 
10
- CSV_HANDLER = FasterCSV
11
- rescue LoadError => e
12
- raise "Error : FasterCSV not installed, please run: gem install fastercsv"
13
- end
6
+ [Array, ActiveRecord::Relation].each do |klass|
7
+ klass.prepend(ToCsvRails::Export)
14
8
  end
15
9
 
16
- require "to_csv_rails/version"
17
- require "to_csv_rails/array.rb"
18
-
19
10
  if defined?(ActionController::Renderers) && ActionController::Renderers.respond_to?(:add)
20
11
  ActionController::Renderers.add :csv do |csv, options|
21
- self.content_type ||= Mime::CSV
12
+ self.content_type ||= ActiveRecord::VERSION::MAJOR < 5 ? Mime::CSV : Mime[:csv]
22
13
  self.response_body = csv.respond_to?(:to_csv) ? csv.to_csv : csv
23
14
  end
24
15
  end
@@ -0,0 +1,36 @@
1
+ module ToCsvRails
2
+ module Export
3
+ def to_csv(options = {})
4
+ return '' unless self.any?
5
+
6
+ columns = self.first.attributes.keys.sort.map(&:to_sym)
7
+
8
+ if options[:only]
9
+ columns.delete_if{|column| !options[:only].include?(column)}
10
+ end
11
+
12
+ if options[:except]
13
+ columns.delete_if{|column| options[:except].include?(column)}
14
+ end
15
+
16
+ columns += Array(options[:methods])
17
+
18
+ # Keep id column always in first position, if present.
19
+ if columns.index(:id)
20
+ columns.delete(:id)
21
+ columns.unshift(:id)
22
+ end
23
+
24
+ output = CSV.generate(Hash.new(options[:csv_options])) do |csv|
25
+
26
+ csv << options[:headers] unless options[:headers].nil?
27
+
28
+ self.each do |row|
29
+ csv << columns.collect{|column| row.send(column) }
30
+ end
31
+ end
32
+
33
+ output
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module ToCsvRails
2
- VERSION = "0.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,16 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_csv_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Fernando Morgenstern
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-30 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2017-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
14
27
  description: An to_csv implementation focused on making it easier for Rails developers
15
28
  to export data to CSV format.
16
29
  email:
@@ -19,36 +32,32 @@ executables: []
19
32
  extensions: []
20
33
  extra_rdoc_files: []
21
34
  files:
22
- - lib/to_csv_rails/array.rb
23
- - lib/to_csv_rails/version.rb
24
- - lib/to_csv_rails.rb
25
- - Rakefile
35
+ - CHANGELOG.md
26
36
  - README.md
27
- - test/test_csv.rb
28
- - test/test_helper.rb
29
- - to_csv_rails.gemspec
37
+ - lib/to_csv_rails.rb
38
+ - lib/to_csv_rails/export.rb
39
+ - lib/to_csv_rails/version.rb
30
40
  homepage: http://fernandomarcelo.com/
31
41
  licenses: []
42
+ metadata: {}
32
43
  post_install_message:
33
44
  rdoc_options: []
34
45
  require_paths:
35
46
  - lib
36
47
  required_ruby_version: !ruby/object:Gem::Requirement
37
- none: false
38
48
  requirements:
39
- - - ! '>='
49
+ - - ">="
40
50
  - !ruby/object:Gem::Version
41
- version: '0'
51
+ version: '2.0'
42
52
  required_rubygems_version: !ruby/object:Gem::Requirement
43
- none: false
44
53
  requirements:
45
- - - ! '>='
54
+ - - ">="
46
55
  - !ruby/object:Gem::Version
47
56
  version: '0'
48
57
  requirements: []
49
58
  rubyforge_project:
50
- rubygems_version: 1.8.24
59
+ rubygems_version: 2.2.2
51
60
  signing_key:
52
- specification_version: 3
61
+ specification_version: 4
53
62
  summary: An to_csv implementation for Rails framework
54
63
  test_files: []
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'test'
5
- end
6
-
7
- desc "Run tests"
8
- task :default => :test
@@ -1,28 +0,0 @@
1
- class Array
2
- def to_csv(options = {})
3
- return '' unless self.any?
4
-
5
- columns = self.first.attributes.keys.map(&:to_sym)
6
-
7
- if options[:only]
8
- columns.delete_if{|column| !options[:only].include?(column)}
9
- end
10
-
11
- if options[:except]
12
- columns.delete_if{|column| options[:except].include?(column)}
13
- end
14
-
15
- columns += Array(options[:methods])
16
-
17
- output = CSV_HANDLER.generate(Hash.new(options[:csv_options])) do |csv|
18
-
19
- csv << options[:headers] unless options[:headers].nil?
20
-
21
- self.each do |row|
22
- csv << columns.collect{|column| row.send(column) }
23
- end
24
- end
25
-
26
- output
27
- end
28
- end
data/test/test_csv.rb DELETED
@@ -1,54 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CsvTest < Test::Unit::TestCase
4
- def teardown
5
- User.destroy_all
6
- end
7
-
8
- def test_simple_export
9
- user1 = User.create! :name => 'User 1', :email => 'user1@email.com'
10
- user2 = User.create! :name => 'User 2', :email => 'user2@email.com'
11
-
12
- data = User.all
13
-
14
- assert_equal data.to_csv,
15
- "#{user1.id},User 1,user1@email.com\n#{user2.id},User 2,user2@email.com\n"
16
- end
17
-
18
- def test_export_with_empty_data
19
- data = User.all
20
-
21
- assert_equal data.to_csv,
22
- ""
23
- end
24
-
25
- def test_export_with_only_option
26
- User.create! :name => 'User 1', :email => 'user1@email.com'
27
- User.create! :name => 'User 2', :email => 'user2@email.com'
28
-
29
- data = User.all
30
-
31
- assert_equal data.to_csv(:only => [:name]),
32
- "User 1\nUser 2\n"
33
- end
34
-
35
- def test_export_with_except_option
36
- User.create! :name => 'User 1', :email => 'user1@email.com'
37
- User.create! :name => 'User 2', :email => 'user2@email.com'
38
-
39
- data = User.all
40
-
41
- assert_equal data.to_csv(:except => [:id, :name]),
42
- "user1@email.com\nuser2@email.com\n"
43
- end
44
-
45
- def test_export_with_headers_option
46
- user1 = User.create! :name => 'User 1', :email => 'user1@email.com'
47
- user2 = User.create! :name => 'User 2', :email => 'user2@email.com'
48
-
49
- data = User.all
50
-
51
- assert_equal data.to_csv(:headers => [:id, :name, :email]),
52
- "id,name,email\n#{user1.id},User 1,user1@email.com\n#{user2.id},User 2,user2@email.com\n"
53
- end
54
- end
data/test/test_helper.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'rubygems'
2
- require 'active_record'
3
- require 'test/unit'
4
- require 'to_csv_rails'
5
-
6
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
7
-
8
- ActiveRecord::Schema.define(:version => 1) do
9
- create_table :users do |t|
10
- t.string :name
11
- t.string :email
12
- end
13
- end
14
-
15
- class User < ActiveRecord::Base
16
-
17
- end
data/to_csv_rails.gemspec DELETED
@@ -1,17 +0,0 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
- require "to_csv_rails/version"
3
-
4
- Gem::Specification.new do |s|
5
- s.name = "to_csv_rails"
6
- s.version = ToCsvRails::VERSION
7
- s.authors = ["Fernando Morgenstern"]
8
- s.email = ["contato@fernandomarcelo.com"]
9
- s.homepage = "http://fernandomarcelo.com/"
10
- s.summary = %q{An to_csv implementation for Rails framework}
11
- s.description = %q{An to_csv implementation focused on making it easier for Rails developers to export data to CSV format.}
12
- s.files = Dir['**/*'].select{|f| File.file?(f)}
13
-
14
- if RUBY_VERSION < "1.9.0"
15
- s.add_dependency('fastercsv')
16
- end
17
- end