Skip to content

Instantly share code, notes, and snippets.

View dmitryck's full-sized avatar

dmitryck dmitryck

  • krasava
View GitHub Profile

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

@tmm1
tmm1 / gist:329682
Created March 11, 2010 21:31
EM Chat Server Demo
require 'rubygems'
require 'eventmachine'
require 'em-http' # gem install em-http-request
require 'yajl' # gem install yajl-ruby
class String
def bold
"\033[1m#{self}\033[0m"
end
end
@mrk21
mrk21 / tailcall_optimization.rb
Created May 21, 2020 06:28
Tail call optimization for Ruby
# @see [Rubyの末尾呼び出し最適化を試す - Qiita](https://qiita.com/rsnni/items/8f818f2c2da07896fc7e)
RubyVM::InstructionSequence.compile_option = {
:tailcall_optimization => true,
:trace_instruction => false
}
RubyVM::InstructionSequence.new(<<-EOS).eval
def fact(x)
return 1 if x == 0
@vishwassharma
vishwassharma / .vimrc
Created March 25, 2012 00:10
Biggest vimfile ever
" =========================
" _ __(_)__ _ ________
" _| |/ / // ' \/ __/ __/
" (_)___/_//_/_/_/_/ \__/
" =========================
" Author: stardiviner ( numbchild at gmail dot com )
" [[ vim Settings ]] {{{2
" :echo $MYVIMRC
set nocompatible " not vim compatible. must be first one, will affect other options.
require 'cucumber'
require 'selenium-webdriver'
# require 'cukehub' # optional, but recommended. See cukehub.com for more details
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: { binary: "/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary",
args: [ "--headless" ]})
Before do
@browser = Selenium::WebDriver.for :chrome, desired_capabilities: caps
end
@steve9001
steve9001 / application.rb
Created December 7, 2011 16:18
Rails middleware to provide information about errors during requests
module MyApp
class Application < Rails::Application
if Rails.env == 'test'
require 'diagnostic'
config.middleware.use(MyApp::DiagnosticMiddleware)
end
end
end
@rkh
rkh / chat.rb
Created December 14, 2011 12:55
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
@UKNC
UKNC / Install headless Firefox on CentOS 6 for Selenium automation.md
Last active May 5, 2023 15:06
Install headless Firefox on CentOS 6 for Selenium automation

Abstract

In the manual the steps required to install headless Firefox on CentOS 6 are described. Headless Firefox may be needed to perform GUI automated testing.

All steps of this manual have been validated on CentOS 6.8. It should not be a problem to adapt the solution to other distros.

Steps

Upgrade yum

@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:
@unnitallman
unnitallman / gist:944011
Created April 27, 2011 10:11
sqlite with activerecord outside rails
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)