acts_as_csv 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/acts_as_csv.rb +90 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 03ca915b35100447d0baaae3bf8ee07383667522
|
4
|
+
data.tar.gz: 1284522dc83ba81e16fec2396001ad696890717c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 64c7490c1168dd24cfdc914a22e118daeb8acd58b753880947fc3fe99d409e9654ad1dffbae8630526d2b0854354626d6f3eebd0d847b6bdae8e8bb1bdd6cff8
|
7
|
+
data.tar.gz: dcc1701cf4a2007e9a6287cc85c16500163a6b545de332492d3f586264fba76cb90fe9c4e5f1bb57da2b0a655048c98b259c63cc1d35b7418212cf486aa13ece
|
data/lib/acts_as_csv.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
|
2
|
+
require 'active_record'
|
3
|
+
##
|
4
|
+
# Extend the ability to create csv from ActiveRecord models
|
5
|
+
|
6
|
+
module ActiveRecordCsvExtension
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
# Take an instance and turn it into an array using the csv columns specified at the class level
|
11
|
+
def to_csv
|
12
|
+
self.class.csv_columns.collect {|method| try(method) || try_association_methods(method) }
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Attempt to run a has_many count associated method by seeing if there is a chained method in the string with a '.'
|
17
|
+
# We use this to support the has_one, belongs_to and has_many column names that we are generating while giving
|
18
|
+
# the developer the flexibility to override those methods
|
19
|
+
def try_association_methods(method)
|
20
|
+
base_method = method
|
21
|
+
chained_method = method.scan(/\.\w{1,}/).try(:first)
|
22
|
+
return nil unless chained_method
|
23
|
+
base_method.gsub!(chained_method,'')
|
24
|
+
chained_method.gsub!('.','')
|
25
|
+
try(base_method).try(chained_method)
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# add your static(class) methods here
|
30
|
+
module ClassMethods
|
31
|
+
|
32
|
+
##
|
33
|
+
# Array of methods that we will export out of a model object. Also used as the header row for the CSV file
|
34
|
+
def csv_columns
|
35
|
+
columns = column_names + belongs_to_associations + has_one_associations + has_many_associations + optional_csv_attributes - filter_names
|
36
|
+
columns.compact!
|
37
|
+
columns.uniq!
|
38
|
+
columns.flatten!
|
39
|
+
columns
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Lets you filter out sensitive information
|
44
|
+
def filter_names
|
45
|
+
['ssn', 'dob', 'password', 'cc', 'credit_card', 'cvv', 'drivers_license_number', 'drivers_license_state']
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Lets us add additional csv_columns at the model level. Override in app/moderls/order.rb, etc
|
50
|
+
def optional_csv_attributes
|
51
|
+
[]
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Iterate over all the belongs_to associations and see if it has a name. We don't worry about _id
|
56
|
+
# because that by default is on the database table that ActiveRecord is interpreting
|
57
|
+
def belongs_to_associations
|
58
|
+
self.reflect_on_all_associations(:belongs_to).collect do |assoc|
|
59
|
+
klass = assoc.options[:class_name].try(:constantize) || assoc.name.to_s.classify.constantize
|
60
|
+
if klass.column_names.include?('name')
|
61
|
+
"#{assoc.name}.name"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Iterate over all the has_one associations and see if the object has a name attribute else print out
|
68
|
+
# the id
|
69
|
+
def has_one_associations
|
70
|
+
self.reflect_on_all_associations(:has_one).collect do |assoc|
|
71
|
+
klass = assoc.options[:class_name].try(:constantize) || assoc.name.to_s.classify.constantize
|
72
|
+
if klass.column_names.include?('name')
|
73
|
+
"#{assoc.name}.name"
|
74
|
+
else
|
75
|
+
"#{assoc.name}.id"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# count all the has_many associations methods
|
82
|
+
def has_many_associations
|
83
|
+
self.reflect_on_all_associations(:has_many).collect {|assoc| "#{assoc.name}.count"}
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# include the extension
|
90
|
+
ActiveRecord::Base.send(:include, ActiveRecordCsvExtension)
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordan Graft
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-02 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Extends default CSV behavior to ActiveRecord models
|
84
|
+
email: jordan@cratebind.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- lib/acts_as_csv.rb
|
90
|
+
homepage:
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Extends default CSV behavior to ActiveRecord models
|
114
|
+
test_files: []
|
115
|
+
has_rdoc:
|