・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction ・ Introduction of Bootstrap3 -Login function implementation ・ Implementation of posting function
Terminal
$ rails g model Coupon user_id:integer is_valid:boolean limit:integer
~__create_coupons.rb
class CreateCoupons < ActiveRecord::Migration[5.2]
def change
create_table :coupons do |t|
t.integer :user_id
t.boolean :is_valid, default: true # 「default:Added "true"
t.integer :limit
t.timestamps
end
end
end
Terminal
$ rails db:migrate
user.rb
#Postscript
has_many :coupons, dependent: :destroy
coupon.rb
class Coupon < ApplicationRecord
belongs_to :user
enum is_valid: { 'Effectiveness': true, 'Invalid': false }
def self.coupon_create(user)
coupon = Coupon.new(user_id: user.id, limit: 1)
coupon.save
end
def self.coupon_destroy
time = Time.now
coupons = Coupon.all
coupons.each do |coupon|
if coupon.created_at + coupon.limit.days < time && coupon.is_valid == 'Effectiveness'
coupon.is_valid = 'Invalid'
coupon.save
end
end
end
end
enum is_valid: { 'Effectiveness': true, 'Invalid': false }
def self.coupon_create(user)
coupon = Coupon.new(user_id: user.id, limit: 1)
coupon.save
end
def self.coupon_destroy
time = Time.now
coupons = Coupon.all
coupons.each do |coupon|
if coupon.created_at + coupon.limit.days < time && coupon.is_valid == 'Effectiveness'
coupon.is_valid = 'Invalid'
coupon.save
end
end
end
** ◎ If 24 hours have passed since the coupon was created and the coupon status is valid, change it to invalid and save it. ** **
if coupon.created_at + coupon.limit.minutes < time && coupon.is_valid == 'Effectiveness'
coupon.is_valid = 'Invalid'
coupon.save
end
coupons_controller.rb
Terminal
$ rails g controller coupons index
coupons_controller.rb
class CouponsController < ApplicationController
def index
@coupons = Coupon.where(user_id: current_user.id, is_valid: 'Effectiveness')
end
end
books_controller.rb
This time, we will implement a coupon to be issued when the book is posted successfully.
books_controller.rb
def create
@book = Book.new(book_params)
@book.user_id = current_user.id
if @book.save
Coupon.coupon_create(current_user) #Postscript
redirect_to books_path
else
@books = Book.all
render 'index'
end
end
application.rb
module Bookers2Debug
class Application < Rails::Application
config.load_defaults 5.2
config.time_zone = 'Tokyo' #Postscript
end
end
Terminal
$ touch config/initializers/time_formats.rb
time_formats.rb
Time::DATE_FORMATS[:datetime_jp] = '%Y/%m/%d/%H:%M'
slim:coupons/index.html.slim
.row
.col-xs-3
.col-xs-6
table.table
thead
tr
th
|Coupon number
th
|title
tbody
- @coupons.each.with_index(1) do |coupon, index|
tr
td
= index
td
- limit = coupon.created_at + coupon.limit.minutes
= limit.to_s(:datetime_jp)
.col-xs-3
5
.- limit = coupon.created_at + coupon.limit.minutes
= limit.to_s(:datetime_jp)
Gemfile
#Postscript
gem 'whenever', require: false
Terminal
$ bundle
" schedule.rb "
Terminal
$ bundle exec wheneverize .
config/schedule.rb
env :PATH, ENV['PATH'] #Specify a relative path from an absolute path
set :output, 'log/cron.log' #Set the log output destination file
set :environment, :development #Set environment
every 1.minute do
runner 'Coupon.coupon_destroy'
end
cron
Terminal
$ bundle exec whenever --update-crontab
crontab -e
➡︎ Edit cron on the terminal
$ bundle exec whenever
➡︎ Check cron settings
$ bundle exec whenever --update-crontab
➡︎ Reflect cron
$ bundle exec whenever --clear-crontab
➡︎ Remove cron
Recommended Posts