Created
May 11, 2023 04:00
-
-
Save thanhtungka91/81b71dfc5e6b588ae5d52afba5df97dc to your computer and use it in GitHub Desktop.
create a custom item by batch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# `bundle exec rails runner "P20230510GenItemCustom::Patch.execute"` | |
class P20230510GenItemCustom::Patch | |
class << self | |
def execute | |
# gen CustomerCustomItem for Customer | |
Rails.logger.info("======== Start P20220518SetCustomReportConditionsUserIdsIfAllSelect #{Time.current} ==========") | |
Audits::AuditSession.set_skip_audit(1) | |
ActiveRecord::Base.record_timestamps = false | |
begin | |
p 'start' | |
domain = Domain.find(1) | |
items = [] | |
options = [] | |
ActiveRecord::Base.transaction do | |
100.times do |i| | |
item_type = rand(1..8) | |
item = CustomerCustomItem.new( | |
name: "Customer_custom_time_is_create_by_batch_#{i}", | |
item_type: item_type, | |
domain_id: 1, | |
order_num: 10 + i, | |
created_at: Time.current, | |
updated_at: Time.current | |
) | |
item.save! | |
if item_type == CustomerCustomItem::ItemType::RADIO | |
value = rand(1..2) | |
option1 = CustomerCustomItemOption.new( | |
value:1, | |
label: "label_radio_1", | |
domain: domain, | |
customer_custom_item_id: item.id, | |
created_at: Time.current, | |
updated_at: Time.current | |
) | |
option2 = CustomerCustomItemOption.new( | |
value:2, | |
label: "label_radio_2", | |
domain: domain, | |
customer_custom_item_id: item.id, | |
created_at: Time.current, | |
updated_at: Time.current | |
) | |
options << option1 | |
options << option2 | |
end | |
if item_type == CustomerCustomItem::ItemType::CHECK | |
option1 = CustomerCustomItemOption.new( | |
value:1, | |
label: "label_check_1", | |
domain: domain, | |
customer_custom_item_id: item.id, | |
created_at: Time.current, | |
updated_at: Time.current | |
) | |
option2 = CustomerCustomItemOption.new( | |
value:2, | |
label: "label_check_2", | |
domain: domain, | |
customer_custom_item_id: item.id, | |
created_at: Time.current, | |
updated_at: Time.current | |
) | |
options << option1 | |
options << option2 | |
end | |
items << item | |
end | |
# Save the CustomerCustomItem objects | |
CustomerCustomItemOption.import options, validate:false | |
end | |
rescue StandardError => e | |
binding.pry | |
Rails.logger.error("======== Error P20220622UpdateCustomItemLimitOnDomain domain_id: #{e.messages}\n#{e.backtrace} ==========") | |
# Ignored | |
end | |
Audits::AuditSession.set_skip_audit(0) | |
Rails.logger.info("======== End P20220518SetCustomReportConditionsUserIdsIfAllSelect #{Time.current} ==========") | |
end | |
def destroy_all | |
Rails.logger.info("======== Start P20220518SetCustomReportConditionsUserIdsIfAllSelect #{Time.current} ==========") | |
Audits::AuditSession.set_skip_audit(1) | |
ActiveRecord::Base.record_timestamps = false | |
begin | |
CustomerCustomItem.destroy_all | |
rescue StandardError => e | |
binding.pry | |
Rails.logger.error("======== Error P20220622UpdateCustomItemLimitOnDomain domain_id: #{e.messages}\n#{e.backtrace} ==========") | |
# Ignored | |
end | |
Audits::AuditSession.set_skip_audit(0) | |
Rails.logger.info("======== End P20220518SetCustomReportConditionsUserIdsIfAllSelect #{Time.current} ==========") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment