Last active
March 13, 2019 11:03
-
-
Save a-suenami/ff0313003d15bbdc53bca0575d7731d9 to your computer and use it in GitHub Desktop.
インピーダンスを解決したいの会
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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