active_record_to_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .*.sw*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2-p0@active_record_to_csv
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_record_to_csv.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+
6
+ desc 'Run rspec tests'
7
+ task :test do
8
+ sh 'rspec spec/'
9
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_record_to_csv/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "active_record_to_csv"
7
+ s.version = ActiveRecordToCsv::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jared Ning"]
10
+ s.email = ["jared@redningja.com"]
11
+ s.homepage = "https://github.com/ordinaryzelig/active_record_to_csv"
12
+ s.summary = %q{Simple ActiveRecordModel#to_csv class method that preserves scopes}
13
+ s.description = %q{Simple ActiveRecordModel#to_csv class method that preserves scopes}
14
+
15
+ s.rubyforge_project = "active_record_to_csv"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activerecord', '>= 3.0.0'
23
+
24
+ s.add_development_dependency 'rspec', '2.5.0'
25
+ s.add_development_dependency 'sqlite3-ruby', '1.2.5'
26
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordToCsv
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'csv'
2
+
3
+ module ActiveRecordToCSV
4
+ # Return full CSV content with headers as string.
5
+ # Defined as class method which will have chained scopes applied.
6
+ def to_csv
7
+ csv_columns = column_names - %w{id created_at updated_at}
8
+ header_row = csv_columns.to_csv
9
+ records_rows = all.map do |record|
10
+ csv_columns.map do |column|
11
+ record[column]
12
+ end.to_csv
13
+ end.join
14
+ header_row + records_rows
15
+ end
16
+ end
17
+
18
+ ActiveRecord::Base.extend ActiveRecordToCSV
19
+
20
+ # Define Relation#to_csv so that method_missing will not
21
+ # delegate to array.
22
+ class ActiveRecord::Relation
23
+ def to_csv
24
+ scoping do
25
+ @klass.to_csv
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecordToCSV do
4
+
5
+ before :all do
6
+ [
7
+ 'Black Swan',
8
+ 'Inception',
9
+ 'The Fighter',
10
+ "The King's Speech",
11
+ 'The Kids Are All Right'
12
+ ].each_with_index do |title, i|
13
+ Movie.create! :title => title, :director_id => i
14
+ end
15
+ end
16
+
17
+ it 'should convert records to csv' do
18
+ Movie.to_csv.should eq(content_of(fixtures_file('movies.csv')))
19
+ end
20
+
21
+ it' should preserve scope' do
22
+ Movie.bad.to_csv.should eq(content_of(fixtures_file('movies_bad.csv')))
23
+ end
24
+
25
+ it 'should not include id and timestamp fields' do
26
+ Movie.to_csv.lines.first.should eq("title,director_id\n")
27
+ end
28
+
29
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'sqlite3'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+
5
+ ActiveRecord::Schema.define do
6
+ create_table :movies, :force => true do |t|
7
+ t.string :title
8
+ t.integer :director_id
9
+ t.timestamps
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'pathname'
2
+ require 'active_record'
3
+
4
+ require File.join(Pathname(__FILE__).dirname.expand_path, '../lib/active_record_to_csv')
5
+
6
+ # load schema.
7
+ require 'schema'
8
+
9
+ # require support .rb files.
10
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.include Macros
14
+ end
@@ -0,0 +1,6 @@
1
+ title,director_id
2
+ Black Swan,0
3
+ Inception,1
4
+ The Fighter,2
5
+ The King's Speech,3
6
+ The Kids Are All Right,4
@@ -0,0 +1,2 @@
1
+ title,director_id
2
+ The Kids Are All Right,4
@@ -0,0 +1,11 @@
1
+ module Macros
2
+
3
+ def content_of(file)
4
+ file.read
5
+ end
6
+
7
+ def fixtures_file(file_name)
8
+ File.open(File.join(Pathname(__FILE__).dirname.expand_path, 'fixtures', file_name))
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ class Movie < ActiveRecord::Base
2
+ scope :bad, where(:title => 'The Kids Are All Right')
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_to_csv
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jared Ning
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-26 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.5.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3-ruby
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.2.5
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: Simple ActiveRecordModel#to_csv class method that preserves scopes
50
+ email:
51
+ - jared@redningja.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - .rvmrc
61
+ - Gemfile
62
+ - Rakefile
63
+ - active_record_to_csv.gemspec
64
+ - lib/active_record_to_csv.rb
65
+ - lib/active_record_to_csv/version.rb
66
+ - spec/active_record_to_csv_spec.rb
67
+ - spec/schema.rb
68
+ - spec/spec_helper.rb
69
+ - spec/support/fixtures/movies.csv
70
+ - spec/support/fixtures/movies_bad.csv
71
+ - spec/support/macros.rb
72
+ - spec/support/models/movie.rb
73
+ has_rdoc: true
74
+ homepage: https://github.com/ordinaryzelig/active_record_to_csv
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project: active_record_to_csv
97
+ rubygems_version: 1.5.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Simple ActiveRecordModel#to_csv class method that preserves scopes
101
+ test_files:
102
+ - spec/active_record_to_csv_spec.rb
103
+ - spec/schema.rb
104
+ - spec/spec_helper.rb
105
+ - spec/support/fixtures/movies.csv
106
+ - spec/support/fixtures/movies_bad.csv
107
+ - spec/support/macros.rb
108
+ - spec/support/models/movie.rb