Last active
April 16, 2018 08:09
-
-
Save absyah/617e5e777fd748e5e942baddf8e1b53f to your computer and use it in GitHub Desktop.
mass linking script
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
| csv_text = File.read('/home/ardian/Downloads/mass_link.csv') | |
| csv = CSV.parse(csv_text, :headers => true) | |
| csv.each do |row| | |
| hash = row.to_hash | |
| hash['SKU Elevenia'] | |
| hash['Master SKU Forstok'] | |
| MassLink.new(hash['Master SKU Forstok'], hash['SKU Elevenia']).link | |
| end |
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
| class MassLink | |
| # note: Please update channel id and profile id accordingly | |
| attr_reader :params | |
| def initialize(target_sku, elevenia_sku) | |
| target_variant = Item::ChannelAssociation::VariantAssociation | |
| .joins(:variant) | |
| .where(sku: target_sku, item_variants: {profile_id: 168}) | |
| .first.variant | |
| elevenia_variant = Item::ChannelAssociation::VariantAssociation | |
| .joins(:variant) | |
| .where(sku: elevenia_sku, item_variants: {profile_id: 168}, channel_id: Channel::ELEVENIA) | |
| .first.variant | |
| @params = { | |
| primary_variant_id: target_variant.id, | |
| variant_id: elevenia_variant.id | |
| } | |
| end | |
| def link | |
| return unless unlinked_assoc && primary_variant | |
| if unlinked_assoc.channel_id.in?(primary_variant.channel_associations.pluck(:channel_id)) | |
| return | |
| end | |
| begin | |
| Item::ChannelAssociation::VariantAssociation::VariantLinkForm.new( | |
| variant_id: primary_variant.id, | |
| variant_association: unlinked_assoc | |
| ).save! | |
| rescue => e | |
| puts params | |
| end | |
| end | |
| private | |
| def unlinked_assoc | |
| return @unlinked_assoc if defined? @unlinked_assoc | |
| @unlinked_assoc = Item::ChannelAssociation::VariantAssociation | |
| .joins(:variant) | |
| .where( | |
| item_variants: { profile_id: 168 }, | |
| variant_id: params[:variant_id], | |
| channel_id: Channel::ELEVENIA | |
| ) | |
| .first | |
| end | |
| def primary_variant | |
| return @primary_variant if defined? @primary_variant | |
| @primary_variant = Item::Variant.for_profile(168).find_by(id: params[:primary_variant_id]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment