datashift 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/thor/export.thor CHANGED
@@ -106,6 +106,71 @@ module Datashift
106
106
  end
107
107
 
108
108
  end
109
+
110
+ desc "db", "Export every Active Record model"
111
+
112
+ method_option :result, :aliases => '-r', :required => true, :desc => "Path in which to create excel files"
113
+ method_option :csv, :aliases => '-c', :desc => "Export to CSV instead - Excel is default."
114
+ method_option :prefix, :aliases => '-p', :desc => "For namespaced tables/models specify the table prefix e.g spree_"
115
+ method_option :module, :aliases => '-m', :desc => "For namespaced tables/models specify the Module name e.g Spree"
116
+ method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
117
+ method_option :exclude, :aliases => '-e', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
118
+
119
+ def db()
120
+
121
+ require File.expand_path('config/environment.rb')
122
+
123
+ require 'excel_exporter'
124
+ require 'csv_exporter'
125
+
126
+ exporter = options[:csv] ? DataShift::CsvExporter.new(nil) : DataShift::ExcelExporter.new(nil)
127
+
128
+ ext = options[:csv] ? '.csv' : '.xls'
129
+
130
+ # Hmmm not many models appear - Rails uses autoload !
131
+ #ActiveRecord::Base.send(:subclasses).each do |model|
132
+ # puts model.name
133
+ #end
134
+
135
+ parent = options[:module] ? Object.const_get(options[:module]) : Object
136
+
137
+ ActiveRecord::Base.connection.tables.each do |table|
138
+
139
+ table.sub!(options[:prefix],'') if(options[:prefix])
140
+
141
+ @result = File.join(options[:result], "#{table}#{ext}")
142
+
143
+ begin
144
+ @klass = parent.const_get(table.classify)
145
+ rescue => e
146
+ puts e.inspect
147
+ puts "WARNING: Could not find an AR model for Table #{table}"
148
+ next
149
+ end
150
+
151
+ puts "Datashift: Start export to #{@result}"
152
+
153
+ exporter.filename = @result
154
+
155
+ raise "ERROR: No such Model [#{@klass}] found - check valid model supplied via -model <Class>" if(@klass.nil?)
156
+
157
+ begin
158
+
159
+ if(options[:assoc])
160
+ opts = (options[:exclude]) ? {:exclude => options[:exclude]} : {}
161
+ logger.info("Datashift: Exporting with associations")
162
+ exporter.export_with_associations(@klass, @klass.all, opts)
163
+ else
164
+ exporter.export(@klass.all, :sheet_name => @klass.name)
165
+ end
166
+ rescue => e
167
+ puts e
168
+ puts e.backtrace
169
+ puts "Warning: Error during export, data may be incomplete"
170
+ end
171
+ end
172
+ end
173
+
109
174
  end
110
175
 
111
176
  end
@@ -41,8 +41,6 @@ module Datashift
41
41
  # TODO - We're assuming run from a rails app/top level dir...
42
42
  # ...can we make this more robust ? e.g what about when using active record but not in Rails app,
43
43
  require File.expand_path('config/environment.rb')
44
-
45
-
46
44
 
47
45
  model = options[:model]
48
46
  result = options[:result]
@@ -63,7 +61,7 @@ module Datashift
63
61
  gen = DataShift::ExcelGenerator.new(result)
64
62
 
65
63
  opts = { :remove => options[:remove],
66
- :remove_rails => options[:remove_rails]
64
+ :remove_rails => options[:remove_rails]
67
65
  }
68
66
 
69
67
  if(options[:assoc])
@@ -88,7 +86,7 @@ module Datashift
88
86
  method_option :model, :aliases => '-m', :required => true, :desc => "The active record model to export"
89
87
  method_option :result, :aliases => '-r', :required => true, :desc => "Create template of model in supplied file"
90
88
  method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
91
- method_option :exclude, :aliases => '-e', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
89
+ method_option :exclude, :aliases => '-x', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
92
90
 
93
91
  def csv()
94
92
 
@@ -130,6 +128,72 @@ module Datashift
130
128
  end
131
129
 
132
130
  end
131
+
132
+ desc "db", "Generate a template for every Active Record model"
133
+
134
+ method_option :result, :aliases => '-r', :required => true, :desc => "Path in which to create excel files"
135
+ method_option :csv, :aliases => '-c', :desc => "Export to CSV instead - Excel is default."
136
+ method_option :prefix, :aliases => '-p', :desc => "For namespaced tables/models specify the table prefix e.g spree_"
137
+ method_option :module, :aliases => '-m', :desc => "For namespaced tables/models specify the Module name e.g Spree"
138
+ method_option :assoc, :aliases => '-a', :type => :boolean, :desc => "Include all associations in the template"
139
+ method_option :exclude, :aliases => '-x', :type => :array, :desc => "Use with -a : Exclude association types. Any from #{DataShift::MethodDetail::supported_types_enum.to_a.inspect}"
140
+ method_option :remove, :aliases => '-e', :type => :array, :desc => "Don't generate the user supplied fields"
141
+ method_option :remove_rails, :type => :boolean, :desc => "Don't generate the standard Rails fields: #{DataShift::GeneratorBase::rails_columns.inspect}"
142
+
143
+ def db()
144
+
145
+ require File.expand_path('config/environment.rb')
146
+
147
+ require 'excel_exporter'
148
+ require 'csv_exporter'
149
+
150
+ exporter = options[:csv] ? DataShift::CsvGenerator.new(nil) : DataShift::ExcelGenerator.new(nil)
151
+
152
+ ext = options[:csv] ? '.csv' : '.xls'
153
+
154
+ parent = options[:module] ? Object.const_get(options[:module]) : Object
155
+
156
+ ActiveRecord::Base.connection.tables.each do |table|
157
+
158
+ table.sub!(options[:prefix],'') if(options[:prefix])
159
+
160
+ @result = File.join(options[:result], "#{table}#{ext}")
161
+
162
+ begin
163
+ @klass = parent.const_get(table.classify)
164
+ rescue => e
165
+ puts e.inspect
166
+ puts "WARNING: Could not find an AR model for Table #{table}"
167
+ next
168
+ end
169
+
170
+ puts "Datashift: Start template generation to #{@result}"
171
+
172
+ raise "ERROR: No such Model [#{@klass}] found - check valid model supplied via -model <Class>" if(@klass.nil?)
173
+
174
+ begin
175
+ opts = { :filename => @result,
176
+ :remove => options[:remove],
177
+ :remove_rails => options[:remove_rails],
178
+ :sheet_name => @klass.name
179
+ }
180
+
181
+ if(options[:assoc])
182
+ opts[:exclude] = options[:exclude]
183
+ logger.info("Datashift: Generating with associations")
184
+ exporter.generate_with_associations(@klass, opts)
185
+ else
186
+ exporter.generate(@klass, opts)
187
+ end
188
+ rescue => e
189
+ puts e
190
+ puts e.backtrace
191
+ puts "Warning: Error during export, data may be incomplete"
192
+ end
193
+ end
194
+ end
195
+
196
+
133
197
  end
134
198
 
135
199
  end
data/spec/Gemfile CHANGED
@@ -1,11 +1,22 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+
4
+ # DEFINE WHICH VERSIONS ON VITAL GEMS WE WANT TO TEST WITH
5
+
6
+ gem 'rails', '4.0.4'
7
+
8
+ gem 'spreadsheet'
9
+ gem 'paperclip'
10
+
11
+
3
12
  gem 'rspec' # Behavior Driven Development (BDD) for Ruby
4
13
  gem 'rspec-core' # RSpec runner and example groups.
5
14
  gem 'rspec-expectations' # RSpec matchers for should and should_not.
6
15
  gem 'rspec-mocks' # RSpec test double framework with stubbing and mocking.
7
16
  gem 'rspec-rails' # RSpec version 2.x for Rails version 3.x.
8
17
 
18
+ gem 'sqlite3'
19
+
9
20
  # we need both, for JRuby testing of Excel and non JRuby csv
10
21
  platform :jruby do
11
22
  gem 'jruby-openssl'
@@ -13,13 +24,6 @@ platform :jruby do
13
24
  end
14
25
 
15
26
  platform :ruby do
16
- gem 'sqlite3'
27
+ #gem 'activerecord-sqlite3-adapter'
17
28
  end
18
-
19
- # DEFINE WHICH VERSIONS WE WANT TO TEST WITH
20
-
21
- gem 'rails', '3.2.8'
22
-
23
- gem 'spreadsheet'
24
- gem 'paperclip'
25
29
 
data/spec/Gemfile.lock CHANGED
@@ -1,115 +1,115 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- actionmailer (3.2.8)
5
- actionpack (= 3.2.8)
6
- mail (~> 2.4.4)
7
- actionpack (3.2.8)
8
- activemodel (= 3.2.8)
9
- activesupport (= 3.2.8)
10
- builder (~> 3.0.0)
4
+ actionmailer (4.0.4)
5
+ actionpack (= 4.0.4)
6
+ mail (~> 2.5.4)
7
+ actionpack (4.0.4)
8
+ activesupport (= 4.0.4)
9
+ builder (~> 3.1.0)
11
10
  erubis (~> 2.7.0)
12
- journey (~> 1.0.4)
13
- rack (~> 1.4.0)
14
- rack-cache (~> 1.2)
15
- rack-test (~> 0.6.1)
16
- sprockets (~> 2.1.3)
17
- activemodel (3.2.8)
18
- activesupport (= 3.2.8)
19
- builder (~> 3.0.0)
20
- activerecord (3.2.8)
21
- activemodel (= 3.2.8)
22
- activesupport (= 3.2.8)
23
- arel (~> 3.0.2)
24
- tzinfo (~> 0.3.29)
25
- activerecord-jdbc-adapter (1.2.2)
26
- activerecord-jdbcsqlite3-adapter (1.2.2)
27
- activerecord-jdbc-adapter (~> 1.2.2)
11
+ rack (~> 1.5.2)
12
+ rack-test (~> 0.6.2)
13
+ activemodel (4.0.4)
14
+ activesupport (= 4.0.4)
15
+ builder (~> 3.1.0)
16
+ activerecord (4.0.4)
17
+ activemodel (= 4.0.4)
18
+ activerecord-deprecated_finders (~> 1.0.2)
19
+ activesupport (= 4.0.4)
20
+ arel (~> 4.0.0)
21
+ activerecord-deprecated_finders (1.0.3)
22
+ activerecord-jdbc-adapter (1.3.7)
23
+ activerecord (>= 2.2)
24
+ activerecord-jdbcsqlite3-adapter (1.3.7)
25
+ activerecord-jdbc-adapter (~> 1.3.7)
28
26
  jdbc-sqlite3 (~> 3.7.2)
29
- activeresource (3.2.8)
30
- activemodel (= 3.2.8)
31
- activesupport (= 3.2.8)
32
- activesupport (3.2.8)
33
- i18n (~> 0.6)
34
- multi_json (~> 1.0)
35
- arel (3.0.2)
36
- bouncy-castle-java (1.5.0146.1)
37
- builder (3.0.3)
38
- cocaine (0.3.1)
39
- diff-lcs (1.1.3)
27
+ activesupport (4.0.4)
28
+ i18n (~> 0.6, >= 0.6.9)
29
+ minitest (~> 4.2)
30
+ multi_json (~> 1.3)
31
+ thread_safe (~> 0.1)
32
+ tzinfo (~> 0.3.37)
33
+ arel (4.0.2)
34
+ bouncy-castle-java (1.5.0147)
35
+ builder (3.1.4)
36
+ climate_control (0.0.3)
37
+ activesupport (>= 3.0)
38
+ cocaine (0.5.4)
39
+ climate_control (>= 0.0.3, < 1.0)
40
+ diff-lcs (1.2.5)
40
41
  erubis (2.7.0)
41
- hike (1.2.1)
42
- i18n (0.6.1)
43
- jdbc-sqlite3 (3.7.2)
44
- journey (1.0.4)
45
- jruby-openssl (0.7.7)
46
- bouncy-castle-java (>= 1.5.0146.1)
47
- json (1.7.5)
48
- json (1.7.5-java)
49
- mail (2.4.4)
50
- i18n (>= 0.4.0)
42
+ hike (1.2.3)
43
+ i18n (0.6.9)
44
+ jdbc-sqlite3 (3.7.2.1)
45
+ jruby-openssl (0.9.4)
46
+ bouncy-castle-java (>= 1.5.0147)
47
+ mail (2.5.4)
51
48
  mime-types (~> 1.16)
52
49
  treetop (~> 1.4.8)
53
- mime-types (1.19)
54
- multi_json (1.3.6)
55
- paperclip (2.7.0)
56
- activerecord (>= 2.3.0)
57
- activesupport (>= 2.3.2)
58
- cocaine (>= 0.0.2)
50
+ mime-types (1.25.1)
51
+ minitest (4.7.5)
52
+ multi_json (1.10.0)
53
+ paperclip (4.1.1)
54
+ activemodel (>= 3.0.0)
55
+ activesupport (>= 3.0.0)
56
+ cocaine (~> 0.5.3)
59
57
  mime-types
60
- polyglot (0.3.3)
61
- rack (1.4.1)
62
- rack-cache (1.2)
63
- rack (>= 0.4)
64
- rack-ssl (1.3.2)
65
- rack
66
- rack-test (0.6.1)
58
+ polyglot (0.3.4)
59
+ rack (1.5.2)
60
+ rack-test (0.6.2)
67
61
  rack (>= 1.0)
68
- rails (3.2.8)
69
- actionmailer (= 3.2.8)
70
- actionpack (= 3.2.8)
71
- activerecord (= 3.2.8)
72
- activeresource (= 3.2.8)
73
- activesupport (= 3.2.8)
74
- bundler (~> 1.0)
75
- railties (= 3.2.8)
76
- railties (3.2.8)
77
- actionpack (= 3.2.8)
78
- activesupport (= 3.2.8)
79
- rack-ssl (~> 1.3.2)
62
+ rails (4.0.4)
63
+ actionmailer (= 4.0.4)
64
+ actionpack (= 4.0.4)
65
+ activerecord (= 4.0.4)
66
+ activesupport (= 4.0.4)
67
+ bundler (>= 1.3.0, < 2.0)
68
+ railties (= 4.0.4)
69
+ sprockets-rails (~> 2.0.0)
70
+ railties (4.0.4)
71
+ actionpack (= 4.0.4)
72
+ activesupport (= 4.0.4)
80
73
  rake (>= 0.8.7)
81
- rdoc (~> 3.4)
82
- thor (>= 0.14.6, < 2.0)
83
- rake (0.9.2.2)
84
- rdoc (3.12)
85
- json (~> 1.4)
86
- rspec (2.11.0)
87
- rspec-core (~> 2.11.0)
88
- rspec-expectations (~> 2.11.0)
89
- rspec-mocks (~> 2.11.0)
90
- rspec-core (2.11.1)
91
- rspec-expectations (2.11.3)
92
- diff-lcs (~> 1.1.3)
93
- rspec-mocks (2.11.3)
94
- rspec-rails (2.11.0)
74
+ thor (>= 0.18.1, < 2.0)
75
+ rake (10.3.2)
76
+ rspec (2.14.1)
77
+ rspec-core (~> 2.14.0)
78
+ rspec-expectations (~> 2.14.0)
79
+ rspec-mocks (~> 2.14.0)
80
+ rspec-core (2.14.8)
81
+ rspec-expectations (2.14.5)
82
+ diff-lcs (>= 1.1.3, < 2.0)
83
+ rspec-mocks (2.14.6)
84
+ rspec-rails (2.14.2)
95
85
  actionpack (>= 3.0)
86
+ activemodel (>= 3.0)
96
87
  activesupport (>= 3.0)
97
88
  railties (>= 3.0)
98
- rspec (~> 2.11.0)
99
- ruby-ole (1.2.11.4)
100
- spreadsheet (0.7.3)
89
+ rspec-core (~> 2.14.0)
90
+ rspec-expectations (~> 2.14.0)
91
+ rspec-mocks (~> 2.14.0)
92
+ ruby-ole (1.2.11.7)
93
+ spreadsheet (0.9.7)
101
94
  ruby-ole (>= 1.0)
102
- sprockets (2.1.3)
95
+ sprockets (2.12.1)
103
96
  hike (~> 1.2)
97
+ multi_json (~> 1.0)
104
98
  rack (~> 1.0)
105
99
  tilt (~> 1.1, != 1.3.0)
106
- sqlite3 (1.3.6)
107
- thor (0.16.0)
108
- tilt (1.3.3)
109
- treetop (1.4.10)
100
+ sprockets-rails (2.0.1)
101
+ actionpack (>= 3.0)
102
+ activesupport (>= 3.0)
103
+ sprockets (~> 2.8)
104
+ sqlite3 (1.3.9)
105
+ thor (0.19.1)
106
+ thread_safe (0.3.3)
107
+ thread_safe (0.3.3-java)
108
+ tilt (1.4.1)
109
+ treetop (1.4.15)
110
110
  polyglot
111
111
  polyglot (>= 0.3.1)
112
- tzinfo (0.3.33)
112
+ tzinfo (0.3.39)
113
113
 
114
114
  PLATFORMS
115
115
  java
@@ -119,7 +119,7 @@ DEPENDENCIES
119
119
  activerecord-jdbcsqlite3-adapter
120
120
  jruby-openssl
121
121
  paperclip
122
- rails (= 3.2.8)
122
+ rails (= 4.0.4)
123
123
  rspec
124
124
  rspec-core
125
125
  rspec-expectations
@@ -13,7 +13,7 @@ require 'csv_exporter'
13
13
  describe 'CSV Exporter' do
14
14
 
15
15
  before(:all) do
16
-
16
+
17
17
  # load our test model definitions - Project etc
18
18
  require ifixture_file('test_model_defs')
19
19
 
@@ -26,26 +26,41 @@ describe 'CSV Exporter' do
26
26
  end
27
27
 
28
28
  before(:each) do
29
- MethodDictionary.clear
30
- MethodDictionary.find_operators( Project )
29
+ DataShift::MethodDictionary.clear
30
+ DataShift::MethodDictionary.find_operators( Project )
31
31
 
32
32
  db_clear() # todo read up about proper transactional fixtures
33
+
34
+ Project.create( :value_as_string => 'Value as String', :value_as_boolean => true, :value_as_double => 75.672)
35
+ Project.create( :value_as_string => 'Another Value as String', :value_as_boolean => false, :value_as_double => 12)
36
+
33
37
  end
34
38
 
35
39
  it "should be able to create a new CSV exporter" do
36
- generator = CsvExporter.new( 'rspec_csv_empty.csv' )
40
+ exporter = DataShift::CsvExporter.new( 'rspec_csv_empty.csv' )
41
+
42
+ exporter.should_not be_nil
43
+ end
44
+
45
+ it "should throw if not active record objects" do
46
+ exporter = DataShift::CsvExporter.new( 'rspec_csv_empty.csv' )
37
47
 
38
- generator.should_not be_nil
48
+ expect{ exporter.export([123.45]) }.to raise_error(ArgumentError)
39
49
  end
40
50
 
41
- it "should export a model to csv file" do
51
+
52
+ it "should export collection of model objects to .xls file" do
42
53
 
43
54
  expect = result_file('project_export_spec.csv')
44
55
 
45
- exporter = CsvExporter.new( expect )
56
+ exporter = DataShift::CsvExporter.new( expect )
46
57
 
58
+ count = Project.count
59
+
47
60
  Project.create( :value_as_string => 'Value as String', :value_as_boolean => true, :value_as_double => 75.672)
48
61
 
62
+ Project.count.should == count + 1
63
+
49
64
  exporter.export(Project.all)
50
65
 
51
66
  File.exists?(expect).should be_true
@@ -57,14 +72,37 @@ describe 'CSV Exporter' do
57
72
  count.should == Project.count + 1
58
73
  end
59
74
 
75
+ it "should handle bad params to export" do
76
+
77
+ expect = result_file('project_first_export_spec.csv')
78
+
79
+ exporter = DataShift::CsvExporter.new( expect )
80
+
81
+ expect{ exporter.export(nil) }.not_to raise_error
82
+
83
+ expect{ exporter.export([]) }.not_to raise_error
84
+
85
+ puts "Can manually check file @ #{expect}"
86
+ end
87
+
88
+ it "should export a model object to csv file" do
89
+
90
+ expect = result_file('project_first_export_spec.csv')
91
+
92
+ exporter = DataShift::CsvExporter.new( expect )
93
+
94
+ exporter.export(Project.all[0])
95
+
96
+ File.exists?(expect).should be_true
97
+
98
+ puts "Can manually check file @ #{expect}"
99
+ end
100
+
60
101
  it "should export a model and result of method calls on it to csv file" do
61
102
 
62
103
  expect = result_file('project_with_methods_export_spec.csv')
63
104
 
64
- exporter = CsvExporter.new( expect )
65
-
66
- Project.create( :value_as_string => 'Value as String', :value_as_boolean => true, :value_as_double => 75.672)
67
- Project.create( :value_as_string => 'Another Value as String', :value_as_boolean => false, :value_as_double => 12)
105
+ exporter = DataShift::CsvExporter.new( expect )
68
106
 
69
107
  exporter.export(Project.all, {:methods => [:multiply]})
70
108
 
@@ -85,7 +123,7 @@ describe 'CSV Exporter' do
85
123
 
86
124
  expect= result_file('project_plus_assoc_export_spec.csv')
87
125
 
88
- gen = CsvExporter.new(expect)
126
+ gen = DataShift::CsvExporter.new(expect)
89
127
 
90
128
  gen.export_with_associations(Project, Project.all)
91
129