Skip to content

Commit

Permalink
fix: token collection items_count should except burnt item (#1665)
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Mar 5, 2024
1 parent 833cd1b commit 25cb885
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 17 additions & 11 deletions app/models/token_item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class TokenItem < ApplicationRecord
enum status: { normal: 1, burnt: 0 }

belongs_to :collection, class_name: "TokenCollection", counter_cache: :items_count
belongs_to :collection, class_name: "TokenCollection"
belongs_to :owner, class_name: "Address"
belongs_to :cell, class_name: "CellOutput", optional: true
belongs_to :type_script, optional: true
Expand All @@ -10,35 +10,41 @@ class TokenItem < ApplicationRecord
validates :token_id, uniqueness: { scope: :collection_id }

before_save :update_type_script
after_save :update_collection_holders
after_save :update_collection_holders, :update_items_count

def update_type_script
self.type_script_id = cell&.type_script_id
end

def update_collection_holders
holders_count = collection.items.normal.distinct.count(:owner_id)
collection.update(holders_count: holders_count)
collection.update(holders_count:)
end

def as_json(options = {})
# except burnt items
def update_items_count
items_count = collection.items.normal.count
collection.update(items_count:)
end

def as_json(_options = {})
{
id: id,
id:,
token_id: token_id.to_s,
owner: owner.address_hash,
standard: collection.standard,
cell: {
status: cell&.status,
tx_hash: cell&.tx_hash,
cell_index: cell&.cell_index,
data: cell&.data
data: cell&.data,
},
type_script: type_script&.as_json,
name: name,
metadata_url: metadata_url,
icon_url: icon_url,
created_at: created_at,
updated_at: updated_at
name:,
metadata_url:,
icon_url:,
created_at:,
updated_at:,
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace :migration do
progress_bar = ProgressBar.create({ total: total_count, format: "%e %B %p%% %c/%C" })

TokenCollection.find_each do |collection|
items_count = collection.items.count
TokenCollection.update_counters(collection.id, items_count: items_count)
items_count = collection.items.normal.count
TokenCollection.update_counters(collection.id, items_count:)
holders_count = collection.items.normal.distinct.count(:owner_id)
collection.update_column(:holders_count, holders_count)
progress_bar.increment
Expand Down

0 comments on commit 25cb885

Please sign in to comment.