LoginSignup
0
1

More than 1 year has passed since last update.

ポイント機能・レベルアップ機能 もらったいいね数をマイページで表示させたい! 非同期通信のいいね

Last updated at Posted at 2021-08-26

初めに

ポイント機能というかレベルアップ機能というか・・・
Qiitaでもコントリビューションが増えるとやる気につながるので、
そういった機能を作成したいと思いました。
そんなわけで、自分の記事についたいいね数をマイページで出せるようにしていきます!!
やり方は通知機能と似ているなーと感じました。

スクリーンショット 2021-08-26 18.03.23.png
(TEUっていうのは、コンテナの数え方です。マニアック・・・)
ついでにこんなのも作れます!

スクリーンショット 2021-08-26 18.06.44.png

マイグレーションファイル、モデルファイル作成

00000000_create_points.rb
class CreatePoints < ActiveRecord::Migration[5.2]
  def change
    create_table :points do |t|
      t.integer :giver_id, null: false
      t.integer :getter_id, null: false
      t.integer :post_id, null: false
      t.timestamps
    end
  end
end
/app/models/point.rb
class Point < ApplicationRecord
  belongs_to :post
  belongs_to :giver, class_name: 'User', foreign_key: 'giver_id', optional: true
  belongs_to :getter, class_name: 'User', foreign_key: 'getter_id', optional: true

  scope :created_today, -> { where(created_at: Time.zone.now.all_day) }
  scope :created_yesterday, -> { where(created_at: 1.day.ago.all_day) }
  scope :created_2days_ago, -> { where(created_at: 2.days.ago.all_day) }
  scope :created_3days_ago, -> { where(created_at: 3.days.ago.all_day) }
  scope :created_4days_ago, -> { where(created_at: 4.days.ago.all_day) }
  scope :created_5days_ago, -> { where(created_at: 5.days.ago.all_day) }
  scope :created_6days_ago, -> { where(created_at: 6.days.ago.all_day) }

scope :created_today等に関する記事

user.rb
 # ポイント ポイント与える側、もらう側で2通り
  has_many :active_points, class_name: 'Point', foreign_key: 'giver_id', dependent: :destroy
  has_many :passive_points, class_name: 'Point', foreign_key: 'getter_id', dependent: :destroy

①ポイントを送るユーザーと、
②ポイントを受け取るユーザーの2種類いますのでこのように分けて定義します。
今回だと、active_points
passive_pointsです。それぞれ分けて書いてるけど、
結局はPointとのアソシエーションなので、
class_name: 'Point',をつけてあげます。

post.rb
  has_many :points, dependent: :destroy

コントローラーで、いいねがついたら、ポイントがたまるようにする

controllers/favorite_controller.rb
  def create
    post = Post.find(params[:post_id])
    favorite = current_user.favorites.new(post_id: post.id)
    favorite.save
    @post = Post.find(params[:post_id])

    unless @post.user == current_user
      @post.point_by(current_user)
    end
  end

既に作成済のいいね機能に書き加えました!(自分の記事に「いいね」するのはOKにしてます)
ただし、自分の記事に「いいね」をしてもポイントは貯まらないようにしています!!!
また、「いいね」を外されても、ポイントは減らないようにしています。
(減るのは悲しいので!)

モデルファイルでポイントを保存

コントローラーから、@post.point_by(current_user)で、
current_userを引数として渡されています。

post.rb
 # ポイントの
  def point_by(current_user)
    point = current_user.active_points.new(
      post_id: id,
      getter_id: user_id
    )
    point.save if point.valid?
  end

ここで気づいたのですが、giver_idどこにも使ってない・・・ということです。。
要らなかったああーと思い、マイグレーションファイルやら、
アソシエーション消したら動かなくなりました。。
giverありきのgetterだからでしょうか。

マイページで表示する

users_controller.rb
 def show
    @user = User.find(params[:id])
    @posts = @user.posts.where(status: true).order('created_at DESC').page(params[:page]).per(20)
    @point = @user.passive_points.all
    @posts_tag = @user.tags.order('created_at DESC').limit(15)
  end
app/views/users/show.html.erb
<strong><%= @point.length %></strong> TEU</h3>

以上です!!

いいねポイントが10になればLevel1とかそういう記述をすれば、
レベルアップ機能にもなるかな〜と思います!!!

獲得したいいね数をグラフにしたい方は以下の記事から

0
1
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
1