Skip to content

Instantly share code, notes, and snippets.

View dmitryck's full-sized avatar

dmitryck dmitryck

  • krasava
View GitHub Profile
@theconektd
theconektd / github.css
Created April 30, 2012 02:11
Github Markdown CSS - for Markdown Editor Preview
body {
font-family: Helvetica, arial, sans-serif;
font-size: 14px;
line-height: 1.6;
padding-top: 10px;
padding-bottom: 10px;
background-color: white;
padding: 30px; }
body > *:first-child {

Rendering Deferred Views Outside of Rails Controllers

Typically, Rails views are rendered after some controller action is executed. But the code that powers Rails controllers are flexible and extensible enough to create custom rendering objects that can reuse views and helpers, but live outside of web request processing. In this post, I'll cover what a Rails controller is and what it's composed of. I'll also go over how to extend it to create your own customer renderers, and show an example of how you can render views in your background jobs and push the results to your frontend.

What's a Controller?

A Rails controller is a subclass of ActionController::Base. The documentation says:

Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either render a template or redirect to another action. An action is defined as a public method on the controller, wh

#!/usr/bin/env ruby
# Usage
# $ htpasswd.rb 'your_usrename', 'your_password'
# UseCase
# Generate htpasswd for nginx without apache tools.
require "base64"
username, password = ARGV
salt = Base64.encode64((("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a).shuffle[0..7].join)
puts "#{username}:#{password.crypt(salt)}"
@theinventor
theinventor / teamviewer.rb
Created April 1, 2014 17:30
A quick ruby api wrapper for the teamviewer api
module Teamviewer
class Connector
attr_reader :client
def initialize
@client = connection(credentials)
end
@cmouse
cmouse / gist:27c2af929d0e3c31d24b
Created January 13, 2015 13:48
bgp-peer test
require 'rubygems'
require 'bgp4r'
require 'yaml'
require 'observer'
require 'daemons'
include BGP
# Set this where your config file is
configFile = "/etc/bgp-daemon.conf"
@shuber
shuber / add_index_patch.rb
Created April 1, 2015 07:43
ActiveRecord `add_index` patch for PostgreSQL JSONB GIN indexes
# This patch detects {index_columns} that match something like
# the example below, then strips the surrounding double quotes.
#
# "(settings->'example')"
#
# The resulting CREATE INDEX sql looks something like:
#
# CREATE INDEX "idx_users_on_settings_example" ON "users" USING gin ((settings->>'example'))
#
# Your {add_index} call should looks something like:
@maxivak
maxivak / readme.md
Last active January 12, 2024 06:53
Integrating Gem/Engine and Main Rails App
@Ravenstine
Ravenstine / reactor.cr
Created September 8, 2015 05:29
A very simple event loop written in Crystal. (crystal-lang.org)
module Reactor
class Task
property :block
def initialize(timestamp=Time.now, milliseconds=0, &block : Loop ->)
@block = block
@timestamp = timestamp.epoch_ms
@milliseconds = milliseconds
end
def run reactor
@Val
Val / build_rest-like_api.rb
Created January 19, 2016 14:28
Token protected REST-like API using Grape, Swagger on top of Rails. Based on http://www.toptal.com/ruby/grape-gem-tutorial-how-to-build-a-rest-like-api-in-ruby/
# -*- mode:ruby;tab-width:2;indent-tabs-mode:nil;coding:utf-8 -*-
# vim: ft=ruby syn=ruby fileencoding=utf-8 sw=2 ts=2 ai eol et si
#
# build_rest-like_api.rb: sample REST-like API server
# (c) 2016 Laurent Vallar <val@zbla.net>, WTFPL license v2 see below.
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
@k1r8r0wn
k1r8r0wn / devise.ru.yml
Last active May 12, 2023 18:47
Russian i18n locale for `Devise` gem
# Русский перевод для https://github.com/plataformatec/devise/tree/v4.7.1
# Другие переводы на https://github.com/plataformatec/devise/wiki/I18n
ru:
devise:
confirmations:
confirmed: "Ваша учётная запись успешно подтверждена."
send_instructions: "В течение нескольких минут вы получите письмо с инструкциями по подтверждению вашей учётной записи."
send_paranoid_instructions: "Если ваш адрес email есть в нашей базе данных, то в течение нескольких минут вы получите письмо с инструкциями по подтверждению вашей учётной записи."
failure: