Skip to content

Instantly share code, notes, and snippets.

View nakaearth's full-sized avatar

Nakamura shinichirou nakaearth

View GitHub Profile
@nakaearth
nakaearth / error_user_decorator.rb
Last active August 29, 2015 14:03
active_decoratorの場合エラーになる
module UserDecorator
def short_name
name[0, 5]
end
# 以下のメソッドはUserモデルにもある。この状態でView側でcreated_atを使おうとすると
# SystemStackErrorが発生する
def created_at
created_at.strftime("%Y-%m-%d %H:%M")
end
end
@nakaearth
nakaearth / show.html.erb
Created July 2, 2014 06:25
draperを使った場合のshow.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @user.name %>(<%= @user.short_name %>)
</p>
<p>
<strong>Age:</strong>
<%= @user.age %>
@nakaearth
nakaearth / app_views_users_index.html.erb
Created June 29, 2014 22:17
draperを使った場合のapp/views/users/index.html.erb
<h1>Listing users</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
@nakaearth
nakaearth / app_views_users_show.html.erb
Created June 24, 2014 11:01
active_decoratorを使った場合のshow.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @user.name %>(<%= @user.short_name %>)
</p>
<p>
<strong>Age:</strong>
<%= @user.age %>
@nakaearth
nakaearth / app_views_users_index.html.erb
Created June 24, 2014 10:59
active_decoratorを使ったindex.html.erb
<h1>Listing users</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th colspan="3"></th>
</tr>
</thead>
@nakaearth
nakaearth / app_decorator_UserDecorator.rb
Last active August 29, 2015 14:02
active_decoratorを使った場合
module UserDecorator
def short_name
name[0, 5]
end
end
module UserDecorator
end
class UserDecorator < Draper::Decorator
delegate_all
decorates_finders
def short_name
object.name[0, 5]
end
end
Draper::CollectionDecorator.delegate :current_page, :total_pages, :limit_value, :total_count
@nakaearth
nakaearth / controller.rb
Last active August 29, 2015 14:02
詳細表示で使うshowメソッドの修正
def show
@user = UserDecorator.find(params[:id])
end