Skip to content

Instantly share code, notes, and snippets.

View annikoff's full-sized avatar
💭
| (• ◡•)| (❍ᴥ❍ʋ)

Yakov annikoff

💭
| (• ◡•)| (❍ᴥ❍ʋ)
View GitHub Profile
# В соответствии с соглашениями название должно быть BasketsController
# насколько понимаю, мы находимся в контексте Rails приложение, значит контроллер должен наследоваться от ApplicationController
class BasketController
# на мой взгляд add_item — это более удачное название метода
def add_to_basket
# Правильно будет искать по id товара
# при этом поиск товара лучше вынести в отдельный метод
# def item
# @item ||= Item.find params[:id]
# end
@annikoff
annikoff / prepend_text_to_files.sh
Last active December 1, 2016 18:32
Добавление текста в начало файлов
for f in **/*.@(rb) ;do
sed -i -e '1i# frozen_string_literal: true\' $f
done
@annikoff
annikoff / append_text_to_files.sh
Created December 1, 2016 18:33
This adds \n at the end of the file only if it doesn’t already end with a newline.
for f in **/*.@(rb) ;do
sed -i -e '$a\' $f
done
@annikoff
annikoff / custom_array_each_method.rb
Created January 16, 2019 20:09
Custom array each method
class Array
def each(sym = nil)
return enum_for(:each) if !block_given? && sym.nil?
raise SyntaxError if block_given? && !sym.nil?
i = 0
while i < self.length
sym.nil? ? yield(self[i]) : self[i].send(sym)
i += 1
end
self
@annikoff
annikoff / bundle_patch.rb
Created May 13, 2019 09:13
Prevent specific gem from locking
require 'bundler/lockfile_generator'
module GitPatch
def initialize(options)
@skip_locking = options['skip_locking'] === true
super
end
def skip_locking?
@skip_locking
@annikoff
annikoff / issue_patch.rb
Last active May 21, 2019 09:23
Redmine IssuePatch
# Put this file into app/models/concerns
module IssuePatch
extend ActiveSupport::Concern
included do
before_validation :do_some_work
end
def do_some_work
# Your code here
@annikoff
annikoff / arel_any_predicate.rb
Created February 8, 2019 12:42
An example of how to add a custom predicate to Arel
module Arel::Predications
def any(right)
Arel::Nodes::Any.new(self, quoted_node(right))
end
end
class Arel::Nodes::Any < Arel::Nodes::Binary
def operator
:'ANY'
end
@annikoff
annikoff / custom_enumerable.rb
Last active May 20, 2020 11:39
CustomEnumerable module
module CustomEnumerable
def map(sym = nil)
return enum_for(:map) if !block_given? && sym.nil?
raise SyntaxError if block_given? && !sym.nil?
result = []
each do |item|
result << (sym.nil? ? yield(item) : item.send(sym))
end
result
end
@annikoff
annikoff / repair_nested_set_tree.sql
Created August 8, 2017 10:09
Repair nested set tree
DROP PROCEDURE IF EXISTS tree_recover;
DELIMITER //
CREATE PROCEDURE tree_recover()
MODIFIES SQL DATA
BEGIN
DECLARE currentId, currentParentId CHAR(36);
DECLARE currentLeft INT;
DECLARE startId INT DEFAULT 1;
@annikoff
annikoff / custom_generators.md
Last active February 22, 2024 14:09
Custom generators

The main generator

# lib/generators/rails/policy/policy_generator.rb

module Rails
  module Generators
    class PolicyGenerator < NamedBase
      source_root File.expand_path('templates', __dir__)

      def copy_policy_file