lib/pluck_map/presenter.rb in pluck_map-1.0.0 vs lib/pluck_map/presenter.rb in pluck_map-2.0.0.rc1
- old
+ new
@@ -7,24 +7,25 @@
module PluckMap
class Presenter
include CsvPresenter, HashPresenter, JsonPresenter
- attr_reader :model, :attributes
+ attr_reader :model, :attributes, :query
- def initialize(model, attributes)
+ def initialize(model, attributes, query)
+ unless query.model <= model
+ raise ArgumentError, "Query for #{query.model} but #{model} expected"
+ end
+
@model = model
@attributes = attributes
+ @query = query
end
protected
- def pluck(query)
- unless query.model <= model
- raise ArgumentError, "Query for #{query.model} but #{model} expected"
- end
-
+ def pluck
# puts "\e[95m#{query.select(*selects).to_sql}\e[0m"
results = benchmark("pluck(#{query.table_name})") { query.pluck(*selects) }
return results unless block_given?
attributes.each do |attribute|
attribute.preload!(results)
@@ -68,9 +69,10 @@
# they are used in select statements.
select = select.as("__pluckmap_#{index}") if select.respond_to?(:as)
# On Rails 4.2, `pluck` can't accept Arel nodes
select = Arel.sql(select.to_sql) if ActiveRecord.version.segments.take(2) == [4,2] && select.respond_to?(:to_sql)
+
select
}
end
def attributes_by_id