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 3 years have passed since last update.

クラスを自作してオブジェクト指向を学ぶ

Last updated at Posted at 2021-09-22

##経緯
習作としてQiitaを模した簡易版Webアプリを作成途中、オブジェクト指向を学ぶべく自作のクラスを作ることになったのでそのリファクタリングの経過を残す

##前提
記事(Article)クラスが存在

##環境
MacOS BigSur Ver11.5.2
Ruby 3.0.2
Rails 6.1.4.1

##広告クラス作り方

  1. viewsにとりあえずの完成形を表示させてみる
  2. controllersにコードを移して表示させてみる
  3. modelsにコードを移して表示させてみる
  4. adモデルを作ってarticleモデルから独立させてみる
    (articleモデルの中に広告に関するメソッドがあるのは今後見づらいので)

①viewsに画像リンクベタ貼り

広告となる画像を5つ用意

app/views/articles/show.html.erb
<img src="https://image.itmedia.co.jp/news/articles/2107/19/l_dy_cb_01.jpg">
<img src="https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcTj0ph_lpduhRyThoqjOH_VKAU">
<img src="https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQQnu9xtX6vLjd9YbQyNthp6WsKkSqBm">
<img src="https://www20.a8.net/svt/bgt?aid=180313000005&wid=126&eno=01&mid=s000&mc=1">
<img src="https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQeTfxVxPVijk2pzmPKvCAU>

②-1controllersにもコードを分けて書いてみる

app/controllers/articles_controller.rb
def show
  @image1 = "https://image.itmedia.co.jp/news/articles/2107/19/l_dy_cb_01.jpg"
  @image2 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcTj0ph_lpduhRyThoqjOH_VKAU"
  @image3 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQQnu9xtX6vLjd9YbQyNthp6WsKkSqBm"
  @image4 = "https://www20.a8.net/svt/bgt?aid=180313000005&wid=126&eno=01&mid=s000&mc=1"
  @image5 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQeTfxVxPVijk2pzmPKvCAU"
end
app/views/articles/show.html.erb
<%= image_tag @image1 %>
<%= image_tag @image2 %>
<%= image_tag @image3 %>
<%= image_tag @image4 %>
<%= image_tag @image5 %>

②-2 リファクタリング

app/controllers/articles_controller.rb
def show
  image1 = "https://image.itmedia.co.jp/news/articles/2107/19/l_dy_cb_01.jpg"
  image2 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcTj0ph_lpduhRyThoqjOH_VKAU"
  image3 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQQnu9xtX6vLjd9YbQyNthp6WsKkSqBm"
  image4 = "https://www20.a8.net/svt/bgt?aid=180313000005&wid=126&eno=01&mid=s000&mc=1"
  image5 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQeTfxVxPVijk2pzmPKvCAU"
  @images = [image1, image2, image3, image4, image5]
end
app/views/articles/show.html.erb
<% @images.each do |image| %>
  <%= image_tag image %>
<% end %>

③modelsにもコードを分けて書いてみる

show_adsメソッドを作る

app/models/article.rb
class Article < ApplicationRecord
  def show_ads
     image1 = "https://image.itmedia.co.jp/news/articles/2107/19/l_dy_cb_01.jpg"
     image2 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcTj0ph_lpduhRyThoqjOH_VKAU"
     image3 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQQnu9xtX6vLjd9YbQyNthp6WsKkSqBm"
     image4 = "https://www20.a8.net/svt/bgt?aid=180313000005&wid=126&eno=01&mid=s000&mc=1"
     image5 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQeTfxVxPVijk2pzmPKvCAU"
     @images = [image1, image2, image3, image4, image5]
  end
end
app/controllers/articles_controller.rb
def show
  ads = Article.new
  @images = ads.show_ads
end
app/views/articles/show.html.erb
<% @images.each do |image| %>
  <%= image_tag image %>
<% end %>

④adモデルを作ってモデルを独立させる

  • advertisementモデル作成
  • articleとのアソシエーションを定義
app/models/advertisement.rb
class Advertisement < ApplicationRecord
  belongs_to :article
  
  def show_ads
   image1 = "https://image.itmedia.co.jp/news/articles/2107/19/l_dy_cb_01.jpg"
   image2 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcTj0ph_lpduhRyThoqjOH_VKAU"
   image3 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQQnu9xtX6vLjd9YbQyNthp6WsKkSqBm"
   image4 = "https://www20.a8.net/svt/bgt?aid=180313000005&wid=126&eno=01&mid=s000&mc=1"
   image5 = "https://encrypted-tbn0.gstatic.com/imagesq=tbn:ANd9GcQeTfxVxPVijk2pzmPKvCAU"
   @images = [image1, image2, image3, image4, image5]
  end
end
app/controllers/articles_controller.rb
def show
  ads = Advertisement.new
  @images = ads.show_ads
end
app/views/articles/show.html.erb
<% @images.each do |image| %>
  <%= image_tag image %>
<% end %>

##参考

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?