Skip to content

Instantly share code, notes, and snippets.

View duderman's full-sized avatar
🤘

Nick Norkin duderman

🤘
  • London
  • 00:24 (UTC +01:00)
View GitHub Profile
print(view.scope_name(view.sel()[0].begin()))
SHOPS
| id | name | address |
-------------------------------
| 1 | 7Eleven | 1, some st |
| 2 | Wallmart | 2, other st |
Goods
| id | shop_id | name |
-------------------------
| 1 | 1 | Pepsi |
@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) }
@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
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)
!!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 } }
{
"in_process_packages":
[
],
"installed_packages":
[
"Alignment",
"All Autocomplete",
"AngularJS",
"AngularJS (CoffeeScript)",
@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
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)
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 },