Skip to content

Instantly share code, notes, and snippets.

@a-suenami
Last active March 13, 2019 11:03
Show Gist options
  • Save a-suenami/ff0313003d15bbdc53bca0575d7731d9 to your computer and use it in GitHub Desktop.
Save a-suenami/ff0313003d15bbdc53bca0575d7731d9 to your computer and use it in GitHub Desktop.
インピーダンスを解決したいの会
class ProductMapper
def attributes
[:id, :name, :price]
end
def self.map(relation, limit: nil)
mapping_type = Struct.new(attributes)
relation.limit(limit).pluck(attributes).map do |record|
mapping_type.new(record)
end
end
end
class ProductsController < ApplicationController
def index
rel = Product.available
@products = ProductMapper.map(rel, limit: 30)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment