to_csv-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +15 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/to_csv-rails.rb +24 -0
- data/to_csv-rails.gemspec +19 -0
- metadata +62 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
== to_csv on Rails
|
2
|
+
|
3
|
+
This simple plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
@posts = Post.all
|
9
|
+
|
10
|
+
#
|
11
|
+
# default are export header and all fileds
|
12
|
+
#
|
13
|
+
|
14
|
+
@posts.to_csv
|
15
|
+
@posts.to_csv(:only => [:title, :body])
|
16
|
+
@posts.to_csv(:except => [:id])
|
17
|
+
@posts.to_csv(:header => false)
|
18
|
+
|
19
|
+
|
20
|
+
== Example
|
21
|
+
|
22
|
+
class PostsController < ApplicationController
|
23
|
+
def index
|
24
|
+
@posts = Post.all
|
25
|
+
|
26
|
+
respond_to do |format|
|
27
|
+
format.csv { send_data(@posts.to_csv) }
|
28
|
+
#format.csv {
|
29
|
+
# filename = "posts-#{Time.now.strftime("%Y%m%d%H%M%S")}.csv"
|
30
|
+
# send_data(@posts.to_csv, :type => "text/csv; charset=utf-8; header=present", :filename => filename)
|
31
|
+
#}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
== Install
|
38
|
+
|
39
|
+
gem install to_csv-rails
|
40
|
+
|
41
|
+
gem 'to_csv-rails'
|
42
|
+
bundle install
|
43
|
+
|
44
|
+
git clone http://github.com/wenke/to_csv-rails.git
|
45
|
+
|
46
|
+
== Note
|
47
|
+
|
48
|
+
Copyright (c) 2010 liangwenke8@gmail.com, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the to_csv-rails plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'to_csv-rails'
|
data/lib/to_csv-rails.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def to_csv(options = {})
|
4
|
+
return '' if self.empty?
|
5
|
+
|
6
|
+
#columns = self.first.class.content_columns # not include the ID column
|
7
|
+
if options[:only]
|
8
|
+
columns = Array(options[:only]).map(&:to_sym)
|
9
|
+
else
|
10
|
+
columns = self.first.class.column_names.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
|
11
|
+
end
|
12
|
+
|
13
|
+
return '' if columns.empty?
|
14
|
+
|
15
|
+
data = []
|
16
|
+
# header
|
17
|
+
data << columns.map(&:to_s).map(&:humanize).join(', ') unless options[:header] == false
|
18
|
+
self.each do |obj|
|
19
|
+
data << columns.map{ |column| obj.send(column) }.join(', ')
|
20
|
+
end
|
21
|
+
data.join("\n")
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
version = File.read(File.expand_path("../VERSION",__FILE__)).strip
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'to_csv-rails'
|
7
|
+
s.version = version
|
8
|
+
s.author = "LiangWenKe"
|
9
|
+
s.email = "liangwenke8@gmail.com"
|
10
|
+
s.homepage = "http://github.com/wenke/to_csv-rails"
|
11
|
+
s.summary = "Export data to csv on Rails"
|
12
|
+
s.description = "This simple plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include."
|
13
|
+
|
14
|
+
s.files = Dir["lib/**/*", "[a-zA-Z]*", "init.rb"] - ["Gemfile.lock"]
|
15
|
+
s.require_path = "lib"
|
16
|
+
|
17
|
+
s.rubyforge_project = s.name
|
18
|
+
s.required_rubygems_version = ">= 1.3.4"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_csv-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- LiangWenKe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-20 00:00:00 +08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: This simple plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.
|
18
|
+
email: liangwenke8@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/to_csv-rails.rb
|
27
|
+
- init.rb
|
28
|
+
- MIT-LICENSE
|
29
|
+
- Rakefile
|
30
|
+
- README.rdoc
|
31
|
+
- to_csv-rails.gemspec
|
32
|
+
- VERSION
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/wenke/to_csv-rails
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.4
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: to_csv-rails
|
57
|
+
rubygems_version: 1.6.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Export data to csv on Rails
|
61
|
+
test_files: []
|
62
|
+
|