In this article, we will use a gem called Impressionist to implement PV (page view) number acquisition and PV number ranking of posts.
・ Ruby: 2.6.2 Rails: 6.0.3 ・ OS: windows
A set of posting functions has been created with the Tweet model.
Add the following line to the gemfile.
Gemfile
gem 'impressionist'
Terminal
$ bundle install
Next, create a table that counts the number of PVs.
Terminal
$ rails g impressionist
Terminal
$ rails db:migrate
You will have the following imperssions table.
schema.rb
create_table "impressions", force: :cascade do |t|
t.string "impressionable_type"
t.integer "impressionable_id"
t.integer "user_id"
t.string "controller_name"
t.string "action_name"
t.string "view_name"
t.string "request_hash"
t.string "ip_address"
t.string "session_hash"
t.text "message"
t.text "referrer"
t.text "params"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index"
t.index ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index"
t.index ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index"
t.index ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index"
t.index ["impressionable_type", "impressionable_id", "params"], name: "poly_params_request_index"
t.index ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index"
t.index ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index"
t.index ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index"
t.index ["user_id"], name: "index_impressions_on_user_id"
end
Terminal
$ rails g migration AddImpressionsCountToTweets impressions_count:integer
Add default: 0
to the following migration file.
~_add_impressions_count_to_tweets.rb
class AddImpressionsCountToTweets < ActiveRecord::Migration[6.0]
def change
# 「default:Added "0"
add_column :users, :impressions_count, :integer, default: 0
end
end
Terminal
$ rails db:migrate
tweet.rb
#Postscript
is_impressionable counter_cache: true
is_impressionable
➡︎ Enables ʻimpressionist` to be used in Tweet models.
counter_cache: true
➡︎ Make sure the impressions_count column is updated.
tweets_controller.rb
def index
@tweets = Tweet.all
@rank_tweets = Tweet.order(impressions_count: 'DESC') #Added sorting function
end
def show
@tweet = User.find(params[:id])
impressionist(@tweet, nil, unique: [:ip_address]) #Postscript
end
Tweet.order(impressions_count: 'DESC')
➡︎ Sort the tweet list in descending order of the number of PVs.
impressionist(@user, nil, unique: [:ip_address])
➡︎ When you access the tweet details page, the number of PVs increases by one.
** * This time, the number of PV will be counted by ip_address so that the number of PV cannot be increased voluntarily. ** **
** * If you try rails s on localhost, ip_address will have the default :: 1, so the number of PV will not exceed 1. However, please be assured that we have confirmed that it works properly after deployment. ** **
erb:app/tweets/index.html.erb
<h3>Post list</h3>
<% @tweets.each do |t| %>
<%= t.~ %>
#Number of PV
<%= t.impressions_count %>
<% end %>
<h3>PV number ranking</h3>
<% @rank_tweets.each do |t| %>
<%= t.~ %>
#Number of PV
<%= t.impressions_count %>
<% end %>
Recommended Posts