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 CHANGED
@@ -1,3 +1,7 @@
1
+ = 0.2.2 / 2011-03-21
2
+
3
+ * Removed `piggyback_sql` method in favor of `piggyback_attributes`.
4
+
1
5
  = 0.2.1 / 2011-03-21
2
6
 
3
7
  * Added `piggyback_sql` method for getting the SQL generated for the SELECT
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rake'
2
2
  require 'echoe'
3
3
 
4
- Echoe.new('piggyback', '0.2.1') do |p|
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 piggyback_select(*assoc_names)
91
- assoc_names.map do |assoc_name|
92
- association = piggyback_associations[assoc_name]
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 != mapped_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.flatten.join(', ')
105
- end
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{piggyback}
5
- s.version = "0.2.1"
5
+ s.version = "0.2.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andreas Korth"]
@@ -75,16 +75,8 @@ describe Piggyback do
75
75
  end
76
76
  end
77
77
 
78
- it "should generate SQL for selecting piggybacked attributes" do
79
- Essential.piggyback_select(:detail).split(', ').should =~ [
80
- %("details"."first_name"),
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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: piggyback
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andreas Korth