Skip to content

Instantly share code, notes, and snippets.

View duderman's full-sized avatar
🤘

Nick Norkin duderman

🤘
  • London
  • 15:36 (UTC +01:00)
View GitHub Profile
@duderman
duderman / load_missed_migrations.rb
Last active August 29, 2015 14:09
Loads all migrations
files = Dir.glob("db/migrate/*")
timestamps = files.collect{|f| f.split("/").last.split("_").first}
timestamps.count
migrations = ActiveRecord::Base.connection.execute("SELECT version FROM schema_migrations", as: :array).to_a.flatten
needs = timestamps - migrations
files_needs = files.select { |f| needs.include? f.split("/").last.split("_").first }
files_needs.sort.each do |f|
begin
require "#{Rails.root}/#{f}"
f.split('/').last.split('_')[1..-1].join('_').split('.').first.camelize.constantize.new.up
@duderman
duderman / load_supplier_products.rb
Last active August 29, 2015 14:14
Loads supplier products from csv file
def import_supplier_products(path)
fail unless File.file? path
supplier_mappings = {
'224' => '372bd2a4-258d-48f3-9f84-598e1f09b657',
'445' => '0235c6b5-b399-40ad-91f3-cbe7b02ca565'
}
imported, failed = [], []
CSV.foreach(path) do |row|
manufacturer = Registry::Manufacturer.find_or_create_by(name: row[3])
sp = SupplierProduct.new(
def import_retail_chain_products(path)
fail unless File.file? path
imported, failed = [], []
retail_chain_id = 'ed45a234-1d88-481b-8bbd-1ff1ab8ff7a8'
mappings = {
code: { index: 0, type: :string },
full_name: { index: 1, type: :string },
short_name: { index: 2, type: :string },
ru_international_name: { index: 6, type: :string },
retail_chain_registry_concern_id: { index: 4, type: :registry, model: RetailChainRegistry::Concern },
require 'open-uri'
require 'nokogiri'
invoices = Invoice.where(contract_id: nil)
invoices.each do |invoice|
next if invoice.reload.contract
begin
xml_file = open(invoice.archive_file.file_url)
xml = Nokogiri::XML(xml_file)
@duderman
duderman / get_permitted_params.rb
Created July 8, 2015 16:38
method returns accessible attributes with inheritance and descendants
def permitted_params(klass)
self_attrs = klass.accessible_attributes.reject { |attr| attr =~ /_attributes$/ }.map(&:to_sym)
nested_attrs = klass.nested_attributes_options.keys.map do |nested_klass|
nested_const_klass = nested_klass.to_s.singularize.classify.constantize
{ "#{nested_klass}_attributes".to_sym => permitted_params(nested_const_klass) }
end
types_attrs = klass.descendants.map { |type_class| permitted_params(type_class) }
(self_attrs + (types_attrs || []) + nested_attrs).flatten.uniq.compact
end
{
"in_process_packages":
[
],
"installed_packages":
[
"Alignment",
"All Autocomplete",
"AngularJS",
"AngularJS (CoffeeScript)",
!!cus = CloudUnit.where(slug: 'images', created_at: dates).joins(:items).group('cloud_units.id').having('COUNT(cloud_items.*) <= 1').to_a
!!cus.each { |unit| Resque.enqueue(Jobs::CloudImageCreator, unit.id) if unit.needs_processing? }
!!Component.where(cloud_unit_id: cus.map(&:id)).each { |c| c.box.boxings.each { |bb| bb.boxable.roulette_me } }
class Hash
def deep_diff(other)
(self.keys + other.keys).uniq.inject({}) do |memo, key|
left = self[key]
right = other[key]
next memo if left == right
if left.respond_to?(:deep_diff) && right.respond_to?(:deep_diff)
memo[key] = left.deep_diff(right)
@duderman
duderman / mysql_adapter.rb
Created March 11, 2016 12:57
Fix primary_key NULL for rails 2 and mysql 5.7
class ActiveRecord::ConnectionAdapters::MysqlAdapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
@duderman
duderman / rewind_days.rb
Created June 6, 2016 10:28
Rewinds old days to current period
d = Day.maximum(:date)
7.times { |i| Day.where(date: d - i.days).update_all(date: 7.days.from_now.to_date - i.days) }