Skip to content

Instantly share code, notes, and snippets.

View firedev's full-sized avatar
👁️
👁

Nick Ostrovsky firedev

👁️
👁
View GitHub Profile
@firedev
firedev / localized.rb
Last active March 27, 2020 04:53
Easy localization for Rails model attributes. Models should have attributes ending with `_<locale>`, i.e. `title_en`, `title_ru`. `attr_translated :title` will add a `.title` method. If `.title_<current_locale>` is empty, `.title_en` will be returned.
# localized.rb
#
# Usage:
#
# include Localized
# attr_translated :title
#
# Model.title now returns title_<locale> or falls back to title_en
#
namespace :inquiries do
resources :combiners, only: [:create]
end
# supervise_inquiries_combiners_path
# /supervise/inquiries/combiners ! нет :inquiry_id
# supervise/inquiries/combiners#create
resources :inquiries do
resources :combiners
require 'ostruct'
css = File.read('apps/src/stylesheets/sagamore/_buttons.scss').split("\n")
find_images = -> line { line[/"(.+)"/]; $1 }
check_for_file = -> name { OpenStruct.new(name: name, exists: File.exists?("apps/src/images/icons/#{name}.png")) }
format_filenames = -> file_exists { OpenStruct.new(name: (file_exists.name + '.png' + ' ' * 20)[0, 20], exists: file_exists.exists) }
print_results = -> file_exists { puts "#{file_exists.name}\t #{file_exists.exists ? '✅' :'⛔'}" }
css.map(& find_images).compact.map(& check_for_file).map(& format_filenames).map(& print_results)
@firedev
firedev / colors.rb
Last active April 15, 2016 09:41
Colors.rb – Functional programming in Ruby example
#!/usr/bin/env ruby
require 'paleta'
to_paleta = ->(color) { Paleta::Color.new(:hex, color) rescue nil }
to_div = ->(str) {
"<div style='font-family: sans-serif; width: 5em; height: 3em;
line-height: 3em; display: inline-block; text-align: center;
margin: 0.25em; border-radius: 0.25em; padding: 1em;
background: ##{str}'>
@firedev
firedev / routing_helper.rb
Created January 21, 2014 14:41
Fix for Rails locales when testing with Rspec, Capybara etc...
# spec/support/routing_helper.rb
class ActionDispatch::Routing::RouteSet::NamedRouteCollection::UrlHelper
def call(t, args)
t.url_for(handle_positional_args(t, args, { locale: nil }.merge( @options ), @segment_keys))
end
end
@firedev
firedev / Solarized (Firedev).tmTheme
Created January 9, 2014 09:53
Put this in `~/Library/Application Support/Sublime Text 3/Packages` and add the following line in config: ``` "color_scheme": "Packages/Solarized (Firedev).tmTheme", ```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Solarized (Firedev)</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
@firedev
firedev / application_controller.rb
Last active January 1, 2016 07:09
Make Action Mailer use request host and protocol for images and links in emails.
# application_controller.rb
class ApplicationController < ActionController::Base
before_action :make_action_mailer_use_request_host_and_protocol
def make_action_mailer_use_request_host_and_protocol
ActionMailer::Base.default_url_options[:protocol] = request.protocol
ActionMailer::Base.default_url_options[:host] = request.host_with_port
ActionMailer::Base.asset_host = "#{request.protocol}#{request.host_with_port}"
end
@firedev
firedev / kaminari.rb
Created May 23, 2013 05:53
Fix for Kaminari strange window implementation. Works with Kaminari 0.13
# Fixes https://github.com/amatsuda/kaminari/issues/133
# put into /initializers/kaminari.rb
module Kaminari
module Helpers
class Paginator < Tag
class PageProxy
def inside_window?
if @options[:current_page] <= @options[:window]
@page <= (@options[:window] * 2) + 1
@firedev
firedev / nav_link_to_helper.rb
Last active December 15, 2015 19:09
Provides a `nav_link_to` view helper method that mimics `link_to` but adds a `selected` class if the link equals current page. Put into `intializers`.
@firedev
firedev / autoslug.rb
Last active December 13, 2015 16:48
Creates :slug from given :title when creating models if no :slug given
# autoslug.rb
#
# Usage:
#
# include Autoslug
# attr_slug :title [, :slug] [, &block]
#
# Creates :slug from given :title when creating models if no :slug given
# Uses parameterize by default or can run :title through a &block:
#