Created
June 7, 2016 20:13
-
-
Save nicolasmarcal/d8ffc021a3d062ca41da79e0163fe528 to your computer and use it in GitHub Desktop.
This file contains 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
def show_attribute_diff(version, attr_name) | |
changeset = version.changeset.fetch(attr_name, "Sem Alterações") | |
if no_diff?(changeset) | |
"Sem Alterações" | |
else | |
"DE:#{default_string_when_nil(changeset[0])} PARA:#{default_string_when_nil(changeset[1])}".html_safe | |
end | |
end | |
def show_price_diff(version) | |
version.previous_price == version.updated_price ? "Sem Alterações" : "DE:#{default_string_when_nil(version.previous_price)} PARA:#{default_string_when_nil(version.updated_price)}".html_safe | |
end | |
def show_original_price_diff(version) | |
version.previous_original_price == version.updated_original_price ? "Sem Alterações" : "DE:#{default_string_when_nil(version.previous_original_price)} PARA:#{default_string_when_nil(version.updated_original_price)}".html_safe | |
end | |
def default_string_when_nil(str) | |
str || "VAZIO" | |
end | |
def no_diff?(changeset) | |
changeset == "Sem Alterações" || changeset.first == changeset.second | |
end | |
File.open("changelog_report.csv", "w") do |file| | |
file.puts "GRE;Produto;ATIVO?;NOME;PREÇO PRINCIPAL (POR);PREÇO SEM DESCONTO (DE);DISPONÍVEL EM;USUÁRIO;QUANDO" | |
PaperTrail::Version.where(item_type: "Spree::Product").where("created_at >= '2016-01-01'").group_by(&:item).each do |product, versions| | |
versions.each do |version| | |
if product && version.whodunnit.present? | |
file.puts "#{product.variants.map(&:sku).join(',')};#{product.name};#{(product.available_on.present? ? 'SIM' : 'NAO')};#{show_attribute_diff(version, 'name')};#{show_price_diff(version)};#{show_original_price_diff(version)};#{show_attribute_diff(version, 'available_on')};#{Spree::User.find_by(id: version.whodunnit).login};#{version.created_at.to_formatted_s(:long)}" | |
end | |
end | |
end;nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment