mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
wip migration
This commit is contained in:
parent
af04137657
commit
a9389da76f
@ -7,6 +7,29 @@ module HasAttributes
|
||||
attr_accessor :custom_attribute_values
|
||||
after_save :update_custom_attributes
|
||||
|
||||
def self.create_default_attribute_categories(user)
|
||||
return [] if ['attribute_category', 'attribute_field'].include?(content_name)
|
||||
|
||||
categories = YAML.load_file(Rails.root.join('config', 'attributes', "#{content_name}.yml")).map do |category_name, details|
|
||||
category = user.attribute_categories.create!(
|
||||
entity_type: self.content_name,
|
||||
name: category_name.to_s,
|
||||
icon: details[:icon],
|
||||
label: details[:label]
|
||||
)
|
||||
|
||||
category.attribute_fields << details[:attributes].map do |field|
|
||||
af_field = category.attribute_fields.with_deleted.create!(
|
||||
old_column_source: field[:name],
|
||||
user: user,
|
||||
field_type: field[:field_type].presence || "text_area",
|
||||
label: field[:label].presence || 'Untitled field'
|
||||
)
|
||||
af_field
|
||||
end if details.key?(:attributes)
|
||||
end.compact
|
||||
end
|
||||
|
||||
def self.attribute_categories(user, show_hidden: false)
|
||||
return [] if ['attribute_category', 'attribute_field'].include?(content_name)
|
||||
# This doesn't work the way I think it does
|
||||
@ -71,6 +94,58 @@ module HasAttributes
|
||||
@cached_attribute_categories_for_this_content
|
||||
end
|
||||
|
||||
# Replacement method for the above that doesn't make create calls, for use after everyone is migrated
|
||||
# def self.attribute_categories(user, show_hidden: false)
|
||||
# return [] if ['attribute_category', 'attribute_field'].include?(content_name)
|
||||
|
||||
# # This doesn't work the way I think it does
|
||||
# #return @cached_attribute_categories_for_this_content if @cached_attribute_categories_for_this_content
|
||||
|
||||
# # Always include the flatfile categories (but create AR versions if they don't exist)
|
||||
# categories = YAML.load_file(Rails.root.join('config', 'attributes', "#{content_name}.yml")).map do |category_name, details|
|
||||
# category = AttributeCategory.with_deleted.find_by(
|
||||
# entity_type: self.content_name,
|
||||
# name: category_name.to_s,
|
||||
# user: user
|
||||
# )
|
||||
|
||||
# category.attribute_fields << details[:attributes].map do |field|
|
||||
# category.attribute_fields.with_deleted.find_by(
|
||||
# user: user,
|
||||
# old_column_source: field[:name],
|
||||
# field_type: field[:field_type].presence || "text_area"
|
||||
# )
|
||||
# end if details.key?(:attributes)
|
||||
|
||||
# if show_hidden
|
||||
# category
|
||||
# else
|
||||
# !!category.hidden ? nil : category
|
||||
# end
|
||||
# end.compact
|
||||
|
||||
# # Cache the result in case we call this function multiple times this request
|
||||
# @cached_attribute_categories_for_this_content = begin
|
||||
# if categories.first&.user&.present?
|
||||
# acceptable_hidden_values = show_hidden ? [true, false, nil] : [false, nil]
|
||||
# categories
|
||||
# .first
|
||||
# .user
|
||||
# .attribute_categories
|
||||
# .where(entity_type: self.content_name, hidden: acceptable_hidden_values)
|
||||
# .eager_load(attribute_fields: :attribute_values)
|
||||
# .order('attribute_categories.position, attribute_categories.created_at, attribute_categories.id')
|
||||
|
||||
# # We need to do something like this, but... not this.
|
||||
# #.eager_load(attribute_fields: :attribute_values) # .eager_load(:attribute_fields)
|
||||
# else
|
||||
# categories
|
||||
# end
|
||||
# end
|
||||
|
||||
# @cached_attribute_categories_for_this_content
|
||||
# end
|
||||
|
||||
def update_custom_attributes
|
||||
(self.custom_attribute_values || []).each do |attribute|
|
||||
field = AttributeField.includes(:attribute_category).find_by(
|
||||
|
||||
23
script/initialize_categories_on_old_accounts.rb
Normal file
23
script/initialize_categories_on_old_accounts.rb
Normal file
@ -0,0 +1,23 @@
|
||||
puts "Turning off SQL logs"
|
||||
old_logger = ActiveRecord::Base.logger
|
||||
ActiveRecord::Base.logger = nil
|
||||
|
||||
to_migrate = User.all.pluck(:id) - AttributeCategory.pluck(:user_id).uniq
|
||||
|
||||
users_migrated = 1
|
||||
to_migrate.first(100).each do |user_id|
|
||||
user = User.find(user_id)
|
||||
puts "Migrating user ##{user_id} #{user.email} (#{users_migrated}/#{neg_ids.count})"
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
Rails.application.config.content_types[:all].each do |content_type|
|
||||
puts "\tMigrating #{content_type.name}..."
|
||||
content_type.create_default_attribute_categories(user)
|
||||
end
|
||||
end
|
||||
|
||||
users_migrated += 1
|
||||
end
|
||||
|
||||
puts "Turning SQL logging back on"
|
||||
ActiveRecord::Base.logger = old_logger
|
||||
Loading…
Reference in New Issue
Block a user