piggyback 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +4 -0
- data/Rakefile +1 -1
- data/lib/piggyback.rb +19 -14
- data/piggyback.gemspec +1 -1
- data/spec/piggyback_spec.rb +3 -11
- metadata +1 -1
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'echoe'
|
3
3
|
|
4
|
-
Echoe.new('piggyback', '0.2.
|
4
|
+
Echoe.new('piggyback', '0.2.2') do |p|
|
5
5
|
p.description = "Piggyback attributes from associated models with ActiveRecord"
|
6
6
|
p.url = "http://github.com/juni0r/piggyback"
|
7
7
|
p.author = "Andreas Korth"
|
data/lib/piggyback.rb
CHANGED
@@ -3,6 +3,8 @@ module Piggyback
|
|
3
3
|
|
4
4
|
class Association #:nodoc:
|
5
5
|
|
6
|
+
attr_reader :reflection, :mappings
|
7
|
+
|
6
8
|
def initialize(reflection, mappings = {})
|
7
9
|
@reflection = reflection
|
8
10
|
@mappings = mappings
|
@@ -16,6 +18,10 @@ module Piggyback
|
|
16
18
|
@mappings.map(&block)
|
17
19
|
end
|
18
20
|
|
21
|
+
def attributes
|
22
|
+
@mappings.keys
|
23
|
+
end
|
24
|
+
|
19
25
|
def has_attribute?(attr_name)
|
20
26
|
@mappings.has_key? attr_name
|
21
27
|
end
|
@@ -86,28 +92,27 @@ module Piggyback
|
|
86
92
|
end
|
87
93
|
end
|
88
94
|
end
|
89
|
-
|
90
|
-
def
|
91
|
-
|
92
|
-
|
95
|
+
|
96
|
+
def piggyback_attributes(assoc_name)
|
97
|
+
piggyback_associations[assoc_name].attributes
|
98
|
+
end
|
99
|
+
|
100
|
+
def piggyback(*assoc_names)
|
101
|
+
columns = piggyback_associations.values_at(*assoc_names).map do |association|
|
93
102
|
association.map do |attr_name, mapped_name|
|
94
|
-
if attr_name
|
103
|
+
if attr_name == mapped_name
|
104
|
+
"#{association.quoted_table_name}.#{connection.quote_column_name(attr_name)}"
|
105
|
+
else
|
95
106
|
if mapped_name.is_a? Arel::Nodes::SqlLiteral
|
96
107
|
"#{mapped_name} AS #{attr_name}"
|
97
108
|
else
|
98
109
|
"#{association.quoted_table_name}.#{connection.quote_column_name(mapped_name)} AS #{attr_name}"
|
99
110
|
end
|
100
|
-
else
|
101
|
-
"#{association.quoted_table_name}.#{connection.quote_column_name(attr_name)}"
|
102
111
|
end
|
103
112
|
end
|
104
|
-
end
|
105
|
-
|
106
|
-
|
107
|
-
def piggyback(*assoc_names)
|
108
|
-
selection = piggyback_select(*assoc_names)
|
109
|
-
selection = "#{quoted_table_name}.*, #{selection}" unless scoped.select_values.any?
|
110
|
-
select(selection).joins(*assoc_names)
|
113
|
+
end
|
114
|
+
columns.unshift("#{quoted_table_name}.*") unless scoped.select_values.any?
|
115
|
+
select(columns.flatten.join(', ')).joins(*assoc_names)
|
111
116
|
end
|
112
117
|
end
|
113
118
|
end
|
data/piggyback.gemspec
CHANGED
data/spec/piggyback_spec.rb
CHANGED
@@ -75,16 +75,8 @@ describe Piggyback do
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
it "should
|
79
|
-
Essential.
|
80
|
-
%
|
81
|
-
%("details"."last_name"),
|
82
|
-
%("details"."count"),
|
83
|
-
%("details"."checked"),
|
84
|
-
%("details"."birthday"),
|
85
|
-
%("details"."baggage"),
|
86
|
-
%(details.first_name || ' ' || details.last_name AS name),
|
87
|
-
%("details"."updated_at" AS detail_updated_at)
|
88
|
-
]
|
78
|
+
it "should return the column names for a given association name" do
|
79
|
+
Essential.piggyback_attributes(:detail).should =~
|
80
|
+
%w{ first_name last_name name count checked birthday baggage detail_updated_at }
|
89
81
|
end
|
90
82
|
end
|