comma-heaven 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,7 @@ module CommaHeaven
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def joins
|
11
|
-
case parent
|
11
|
+
sql = case parent
|
12
12
|
when HasManyColumns
|
13
13
|
if parent.options[:by] == 'row'
|
14
14
|
<<-EOS
|
@@ -33,7 +33,11 @@ module CommaHeaven
|
|
33
33
|
ON #{parent.parent.table_alias}.#{model.primary_key} = #{table_alias}.#{association.primary_key_name}
|
34
34
|
EOS
|
35
35
|
else ''
|
36
|
-
end
|
36
|
+
end
|
37
|
+
|
38
|
+
sql << " AND #{table_alias}.#{association.klass.primary_key} IN (SELECT #{association.klass.primary_key} FROM #{association.quoted_table_name} WHERE #{model.send(:sanitize_sql, association.options[:conditions])})" if parent.respond_to?(:association) && association.options[:conditions]
|
39
|
+
|
40
|
+
sql.gsub(/\n/, '').squeeze(' ').strip
|
37
41
|
end
|
38
42
|
|
39
43
|
def sql_as
|
@@ -32,12 +32,12 @@ module CommaHeaven
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def table_alias(method = :pluralize)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
t = case self
|
36
|
+
when HasManyColumns, BelongsToColumns, HasOneColumns then association.name.to_s.send(method)
|
37
|
+
else table.to_s.send(method)
|
38
|
+
end
|
39
|
+
|
40
|
+
return prefix + [((parent && parent.parent) ? parent.table_alias(method) : nil), t, index].compact.join('_')
|
41
41
|
end
|
42
42
|
|
43
43
|
def prefix
|
@@ -243,5 +243,20 @@ Ulivo,150,0
|
|
243
243
|
Pero (marzo 2011),10,
|
244
244
|
EOS
|
245
245
|
end
|
246
|
-
|
246
|
+
|
247
|
+
it "should allow export of multiple relationships referring to same table" do
|
248
|
+
export = {
|
249
|
+
:name => {0 => {:include => '1', :as => ''}},
|
250
|
+
:age => {1 => {:include => '0', :as => ''}},
|
251
|
+
:leafs => {2 => {:export => {
|
252
|
+
:position => {3 => {:include => '1', :as => ''}}}, :limit => 3}},
|
253
|
+
:matching_o_leafs => {4 => {:export => {
|
254
|
+
:position => {5 => {:include => '1', :as => ''}}}, :limit => 3}}}
|
255
|
+
|
256
|
+
Tree.to_comma_heaven(:export => export).to_csv.should == <<-EOS
|
257
|
+
tree_name,leaf_0_position,leaf_1_position,leaf_2_position,matching_o_leaf_0_position,matching_o_leaf_1_position,matching_o_leaf_2_position
|
258
|
+
Olmo,top,middle,bottom,top,,bottom
|
259
|
+
Ulivo,0,5,,,,
|
260
|
+
EOS
|
261
|
+
end
|
247
262
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -71,6 +71,7 @@ Spec::Runner.configure do |config|
|
|
71
71
|
class Tree < ActiveRecord::Base
|
72
72
|
belongs_to :gardener
|
73
73
|
has_many :leafs, :dependent => :destroy
|
74
|
+
has_many :matching_o_leafs, :class_name => 'Leaf', :conditions => ['position LIKE ?', '%o%']
|
74
75
|
|
75
76
|
named_scope :that_begins_with_o, {:conditions => ['name LIKE ?', 'o%']}
|
76
77
|
end
|
data/spec/sqler/columns_spec.rb
CHANGED
@@ -192,4 +192,24 @@ describe "Columns" do
|
|
192
192
|
Gardener.scoped(:joins => columns.joins).count.should == 2
|
193
193
|
Gardener.scoped(:joins => columns.joins, :select => columns.select).first.attributes.to_a.length.should == 16
|
194
194
|
end
|
195
|
+
|
196
|
+
it "should allow export of multiple relationships referring to same table" do
|
197
|
+
export = {
|
198
|
+
:name => {0 => {:include => '1', :as => ''}},
|
199
|
+
:age => {1 => {:include => '0', :as => ''}},
|
200
|
+
:leafs => {2 => {:export => {
|
201
|
+
:position => {3 => {:include => '1', :as => ''}}}, :limit => 3}},
|
202
|
+
:matching_o_leafs => {4 => {:export => {
|
203
|
+
:position => {5 => {:include => '1', :as => ''}}}, :limit => 3}}}
|
204
|
+
|
205
|
+
columns = CommaHeaven::Sqler::Columns.new(Tree, export)
|
206
|
+
|
207
|
+
columns.joins.should =~ /_leafs_0/
|
208
|
+
columns.joins.should =~ /_leafs_1/
|
209
|
+
columns.joins.should =~ /_leafs_2/
|
210
|
+
|
211
|
+
columns.joins.should =~ /_matching_o_leafs_0/
|
212
|
+
columns.joins.should =~ /_matching_o_leafs_1/
|
213
|
+
columns.joins.should =~ /_matching_o_leafs_2/
|
214
|
+
end
|
195
215
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: comma-heaven
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.6.
|
5
|
+
version: 0.6.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Silvano Stralla
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-17 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|