comma-heaven 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -64,6 +64,21 @@ What you obtain is:
64
64
 
65
65
  The export hash explains what to export and how.
66
66
 
67
+ The @:by => 'row'@ option gives the ability to denormalize table contents by rows. Using:
68
+
69
+ Tree.to_comma_heaven(:export => { "name" => {"1" => {"as" => "", "include" => "1"} },
70
+ "leafs" => {"2" => {"export" => { "position" => {"3" => {"as" => "", "include" => '1'} },
71
+ "height_from_ground" => {"4" => {'as' => '', :include => '1'} } }, 'by' => 'row' } } }).to_csv
72
+
73
+ You obtain:
74
+
75
+ tree_name,leaf_position,leaf_height_from_ground
76
+ Olmo,top,,
77
+ Olmo,middle,,
78
+ Olmo,bottom,,
79
+ Ulivo,0,1.0
80
+ Ulivo,5,2.0
81
+
67
82
  == Opinions
68
83
 
69
84
  * CSV export is a common request and still hard to do
@@ -10,11 +10,18 @@ module CommaHeaven
10
10
  def joins
11
11
  case parent
12
12
  when HasManyColumns
13
- <<-EOS
14
- LEFT JOIN #{quote(table)} AS #{table_alias}
15
- ON #{parent.parent.table_alias}.#{model.primary_key} = #{table_alias}.#{association.primary_key_name}
16
- AND #{table_alias}.#{association.klass.primary_key} = (SELECT #{association.klass.primary_key} FROM #{association.quoted_table_name} WHERE #{association.primary_key_name} = #{parent.parent.table_alias}.#{model.primary_key} LIMIT #{index}, 1)
17
- EOS
13
+ if parent.options[:by] == 'row'
14
+ <<-EOS
15
+ LEFT JOIN #{quote(table)} AS #{table_alias}
16
+ ON #{parent.parent.table_alias}.#{model.primary_key} = #{table_alias}.#{association.primary_key_name}
17
+ EOS
18
+ else
19
+ <<-EOS
20
+ LEFT JOIN #{quote(table)} AS #{table_alias}
21
+ ON #{parent.parent.table_alias}.#{model.primary_key} = #{table_alias}.#{association.primary_key_name}
22
+ AND #{table_alias}.#{association.klass.primary_key} = (SELECT #{association.klass.primary_key} FROM #{association.quoted_table_name} WHERE #{association.primary_key_name} = #{parent.parent.table_alias}.#{model.primary_key} LIMIT #{index}, 1)
23
+ EOS
24
+ end
18
25
  when BelongsToColumns
19
26
  <<-EOS
20
27
  LEFT JOIN #{quote(table)} AS #{table_alias}
@@ -7,18 +7,18 @@ module CommaHeaven
7
7
  @model = model
8
8
  @export = export
9
9
  @options = options
10
-
10
+
11
11
  # Fill the array
12
12
  fill!
13
-
13
+
14
14
  # Sort by position
15
15
  # sort! { |a, b| a.position <=> b.position }
16
16
  end
17
-
17
+
18
18
  def select
19
19
  map(&:select).reject { |e| e.empty? }.join(', ')
20
20
  end
21
-
21
+
22
22
  def joins
23
23
  map(&:joins).compact.uniq.join(" ").gsub(/\n/, '').squeeze(' ')
24
24
  end
@@ -57,7 +57,6 @@ module CommaHeaven
57
57
  opts = opts.merge(:on => self.options[:on])
58
58
 
59
59
  export = opts.delete(:export)
60
-
61
60
  case
62
61
  when @model.column_names.include?(column_or_association.to_s)
63
62
  as = opts[:as]
@@ -80,7 +79,7 @@ module CommaHeaven
80
79
 
81
80
  unless opts[:include] == '0'
82
81
  association = @model.reflect_on_association(column_or_association.to_sym)
83
- if association && association.macro == :has_many
82
+ if association && association.macro == :has_many && opts[:by] != 'row'
84
83
 
85
84
  limit = case opts[:limit]
86
85
  when "" then 1
@@ -198,7 +198,37 @@ EOS
198
198
  tree_name,gardener_name,gardener_surname,gardener_birthdate
199
199
  Olmo,Alice,,#{Gardener.find_by_name('Alice').birthdate.strftime('%d/%m/%Y %H:%M')}
200
200
  Ulivo,Bob,,#{Gardener.find_by_name('Bob').birthdate.strftime('%d/%m/%Y %H:%M')}
201
+ EOS
202
+ end
203
+
204
+ it "should allow denormalized export (also called 'by row')" do
205
+ Tree.to_comma_heaven(:export => {:name => {0 => {:include => '1', :as => ''}}, :age => {1 => {:include => '0', :as => ''}}, :leafs => {2 => {:export => {:position => {4 => {:include => '1', :as => ''}}}, :by => 'row', :limit => 3}}}).to_csv.should == <<-EOS
206
+ tree_name,leaf_position
207
+ Olmo,top
208
+ Olmo,middle
209
+ Olmo,bottom
210
+ Ulivo,0
211
+ Ulivo,5
201
212
  EOS
202
213
  end
203
214
 
215
+ it "should allow denormalized export (also called 'by row')" do
216
+ Tree.to_comma_heaven(:export => {:name => {0 => {:include => '1', :as => ''}}, :age => {1 => {:include => '0', :as => ''}}, :leafs => {2 => {:export => {:position => {4 => {:include => '1', :as => ''}}}, :by => 'row', :limit => 3}}}).to_csv.should == <<-EOS
217
+ tree_name,leaf_position
218
+ Olmo,top
219
+ Olmo,middle
220
+ Olmo,bottom
221
+ Ulivo,0
222
+ Ulivo,5
223
+ EOS
224
+
225
+ Tree.to_comma_heaven(:export => {:name => {0 => {}}, :gardener => {1 => {:export => {:name => {2 => {}}, :surname => {3 => {}}}}}, :leafs => {4 => {:export => {:position => {5 => {}}}, :by => 'row', :limit => 2}}}).to_csv.should == <<-EOS
226
+ tree_name,gardener_name,gardener_surname,leaf_position
227
+ Olmo,Alice,,top
228
+ Olmo,Alice,,middle
229
+ Olmo,Alice,,bottom
230
+ Ulivo,Bob,,0
231
+ Ulivo,Bob,,5
232
+ EOS
233
+ end
204
234
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comma-heaven
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 1
10
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Silvano Stralla
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-14 00:00:00 +01:00
18
+ date: 2011-01-18 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency