Skip to content

Instantly share code, notes, and snippets.

View rossmari's full-sized avatar
🏠
Working from home

Roman Buzhenko rossmari

🏠
Working from home
  • Russian Federation, Krasnoyarsk
View GitHub Profile
@rossmari
rossmari / campaigns_helper.rb
Last active April 20, 2016 08:50
For wwwermishel. Example.
module CampaignsHelper
COLORS = [
{ percents: [-1], color: 'gray', message: 'no_capping'},
{ percents: 0..30, color: 'red', message: 'msg'},
{ percents: 30..70, color: 'yellow', message: 'msg'},
{ percents: 70..100, color: 'green', message: 'msg'}
]
def campaign_state(draft_campaign)
@rossmari
rossmari / country_storage.rb
Created December 10, 2015 16:26
another variation of constant loading with metaprogramming
module CountryStorage
private
# extend this module than call some constant
# using agreement COUNTRY_#{country_code}
# this will raise const_missing and than create constant COUNTRY_#{country_code}
# equal to Country.find_by(code: country_code)
# instead of using
@rossmari
rossmari / staging.rb
Created September 21, 2015 08:05
mail config for st - staging
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
@rossmari
rossmari / application.rb
Last active August 29, 2015 14:18
Implementation of sitemap generator for engine ( use gem SitemapGenerator )
# STEP 3
# in application.rb of your main application add
require "fortuna/sitemap"
# and then you can use it from anywhere
# [1] Example from console, generate sitemap for engine:
>> Fortuna::Sitemap.new
# In '/home/rossmari/Projects/ebox/public/':
# Fortuna::Girl Load (0.8ms) SELECT "fortuna_girls".* FROM "fortuna_girls"
@rossmari
rossmari / show.haml
Last active August 29, 2015 14:16
First DSL
-# to DRY usage of bootstrap hovered table with small font that used to SHOW model attributes was created a simple DSL
-# can assign new table with 2 columns (attribute name, attribute value)
-# can add header row/rows and content rows
= ShowTable::TableGenerator.generate_table(self) do
- add_header('Название атрибута', { width: '30%' })
- add_header('Значение атрибута')
- add_row('Имя') do
@rossmari
rossmari / parametrize_searcher.rb
Last active August 29, 2015 14:15
Refactoring example [02]
# === BEFORE REFACTORING
# problems : include russian text, include translation, include unused code
#encoding: utf-8
class SearchOption < ActiveRecord::Base
has_no_table
column :include, :string
column :field_name, :string
column :text, :string
@rossmari
rossmari / region_const.rb
Last active August 29, 2015 14:15
Refactoring example [01]
# === BEFORE REFACTORING
### При добавлении нового региона необходимо также внести информацию о поддомене в файл роутов
module RegionConst
# Местный локатив или второй предложный
LOCATIVE = {"Москва" => "Москве", "Кемерово" => "Кемерово"}
ACCUSATIVE = {"Москва" => "Москвы", "Кемерово" => "Кемерова"}
SUBDOMAINS = Hash[Subdomain.all(:conditions => ['title in (?)', ['Красноярск', 'Омск', 'Новосибирск', 'Абакан', 'Барнаул', 'Москва', 'Кемерово', 'Иркутск', 'Томск', 'Россия', 'Кызыл', 'В мире', 'Чита']]).map{|r| [r.title, r]}]
@rossmari
rossmari / breadcrumbs.rb
Created October 11, 2014 14:11
For Item Good, with nested Categories for Bootstrap 3 - breadcrumbs navigation bar
def goods_breadcrumbs(selected_category)
# no matter how many levels category will have
# we take parents tree
parents_tree = selected_category.self_and_ancestors
# and if category has children from next.level add one of them
if selected_category.children.any?
parents_tree << selected_category.children.first
end
@rossmari
rossmari / publication_form_presenter.rb
Created October 9, 2014 07:11
50% of multi class form presenter - #useless
class Magazine::FormPresenter < ActiveRecord::Base
# === CONSTANTS
CONSTANT_EXCEPT = [:id, :created_at, :updated_at, :type]
# === COLUMN METHODS
class << self
def columns
@columns ||= [];
end
@rossmari
rossmari / event_logger.rb
Last active August 29, 2015 14:07
Store changes for any model
class Magazine::EventLogger
attr_reader :history
FIELD_EXCEPTIONS = [:updated_at]
@namespace = 'Magazine::'
@suffix = 'Event'
def initialize(object)