0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Learning Rails 24 Decorators Pattern

0
Last updated at Posted at 2016-05-03
bash
vi app/models/user.rb

Add this line

user.rb
def past_5_days?
  created_at >= 5.days.ago
end

current user.rb

user.rb
class User < ActiveRecord::Base
  has_many :reviews
  has_many :user_items
  has_many :items, through: :user_items
 
  validates :name,
            presence: true,
            length: {minimum: 3}
 
  def past_5_days?
    created_at >= 5.days.ago
  end
end
bash
vi app/views/users/index.html.erb

Add this line

index.html.erb
<% if user.past_5_days? %>
<% end %>

current index.html.erb

index.html.erb
<h1>Users</h1>
 
<ul>
  <% @users.each do |user| %>
    <li>
      <% if user.past_5_days? %>
        <%= link_to user.name, user_path(user.id) %>:<%= user.approved%>;
        <%= link_to 'edit', edit_user_path(user.id) %>;
        <%= link_to 'delete', user_path(user.id), method: :delete, data: {confirm: "Are you sure?"} %>
        <%= link_to 'suspend', suspend_user_path(user.id), data: {confirm: "suspend?"} %>
        <%= link_to 'permit', permit_user_path(user.id), data: {confirm: "permit?"} %>
      <% end %>
    </li>
  <% end %>
</ul>
 
<p><%= link_to "Add New", new_user_path %></p>

past_5_days method is used only view file.
It it right?

We will refactor it

bash
mkdir app/decorators
bash
vi app/decorators/user_decorator.rb
user_decorator.rb
class UserDecorator
  attr_reader :user
 
  def initialize(user)
    @user = user
  end
 
  def past_5_days?
    user.created_at >= 5.days.ago
  end
end

Delete past_5days?
current user.rb

user.rb
class User < ActiveRecord::Base
  has_many :reviews
  has_many :user_items
  has_many :items, through: :user_items
 
  validates :name,
            presence: true,
            length: {minimum: 3}
end
bash
vi app/views/users/index.html.erb
index.html.erb
<% if UserDecorator.new(user).past_5_days? %>
<% end %>

current index.html.erb

index.html.erb
<h1>Users</h1>
 
<ul>
  <% @users.each do |user| %>
    <li>
      <% if UserDecorator.new(user).past_5_days? %>
        <%= link_to user.name, user_path(user.id) %>:<%= user.approved %>;
        <%= link_to 'edit', edit_user_path(user.id) %>;
        <%= link_to 'delete', user_path(user.id), method: :delete, data: {confirm: "Are you sure?"} %>
        <%= link_to 'suspend', suspend_user_path(user.id), data: {confirm: "suspend?"} %>
        <%= link_to 'permit', permit_user_path(user.id), data: {confirm: "permit?"} %>
      <% end %>
    </li>
  <% end %>
</ul>
 
<p><%= link_to "Add New", new_user_path %></p>

We will change show method in users_controller

bash
vi app/controllers/users_controller.rb
users_controller.rb
require 'user_decorator'
users_controller.rb
def show
  @user_decorator = UserDecorator.new(@user)
  @items = Item.all.pluck(:name, :id)
end
bash
vi app/views/users/show.html.erb
show.html.erb
<% if @user_decorator.past_5_days? %>
  <%= @user_decorator.name %>:<%= @user_decorator.approved %>
<% end %>

スクリーンショット 2016-05-03 22.54.14.png

Add this line

user_decorator.rb
def method_missing(method_name, *args, &block)
  user.send(method_name, *args, &block)
end
bash
ctrl + c
bash
rails c

We need to refactor Decorator

console
rails c
console
require '/srv/www/learning/app/decorators/user_decorator'
console
user_decorator = UserDecorator.new User.first
console
user_decorator.respond_to?(:name)

result is false. However result should be true.

スクリーンショット 2016-05-03 22.57.55.png

bash
vi app/decorators/user_decorator.rb
user_decorator.rb
def respond_to_missing?(method_name, include_private = false)
  user.respond_to?(method_name, include_private) || super
end
console
user_decorator.respond_to?(:name)
result is true now

スクリーンショット 2016-05-03 23.00.17.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?