Skip to content

Instantly share code, notes, and snippets.

View lexmag's full-sized avatar

Aleksei Magusev lexmag

View GitHub Profile
@lexmag
lexmag / app_start.ex
Last active November 19, 2020 15:10
Zen and the Art of Elixir Applications Monitoring
# We do this at compile-time as we don't have Mix available in releases.
@version Mix.Project.config().version
def start(_type, _args) do
#...
with {:ok, _} = result <- Supervisor.start_link(children, options) do
MyApp.Fluxter.write("start", version: @version)
result
end
@lexmag
lexmag / Results.md
Last active August 29, 2015 14:18
MapSet evaluation - elixir#3242

Erlang/OTP 18 RC-1

Elixir 1.1.0-dev

  • new

  • put

    • 100 elements
    MapSet:    10000000   0.16 µs/op
    
@lexmag
lexmag / counter.rb
Created August 4, 2012 22:32
Terribly simple Sinatra streaming
require 'sinatra'
set server: :thin
get '/' do
erb :welcome
end
get '/stream', provides: 'text/event-stream' do
stream do |out|
loop do
@lexmag
lexmag / application.js
Created August 4, 2012 19:28
Rails streaming
//= require jquery
//= require jquery_ujs
$(function() {
var source = new EventSource('/stream');
source.addEventListener('counter', function(e) {
$('body').after(e.data + '<br />');
});
});
@lexmag
lexmag / _form.html.erb
Created July 29, 2012 21:11
DRYing Up Rails Views
<%= simple_form_for(@product) do |f| %>
<%= f.input :title %> <!-- string -->
<%= f.input :description %> <!-- text -->
<%= yield(f) if block_given? %>
<%= f.button :submit %>
<% end %>
@lexmag
lexmag / article.rb_product.rb
Created July 28, 2012 15:12
Two-way polymorphic models / Polymorphic HABTM association
class Article < ActiveRecord::Base # Product class is similar
belongs_to :user
has_many :media, as: :ownable
with_options through: :media, source: :representable do |assn|
assn.has_many :videos, source_type: 'Video'
assn.has_many :images, source_type: 'Image'
end
end
@lexmag
lexmag / new.html.erb
Created July 27, 2012 18:08
`disabled` vs `readonly` form helper options
<%= form_for @post do |f| %>
<%= f.text_field :title, value: 'It will never change', readonly: true %>
<%= f.text_field :description, value: 'You will not see it in params', disabled: true %>
<%= f.button :submit %>
<% end %>
<!-- params[:post] => { "title"=>"It will never change" } -->
@lexmag
lexmag / a.sh
Created July 25, 2012 20:45
Namespaced models
% rails g model Hotel
% rails g model Hotel::Translation # or hotel/translation
@lexmag
lexmag / virtual_attributes.rb
Created July 5, 2012 19:33
Virtual attributes #3
module VirtualAttributes
extend ActiveSupport::Concern
class VirtualAttribute
attr_reader :name, :type
def initilaize(name, type = :string)
@name = name
@type = type
end
@lexmag
lexmag / virtual_attributes.rb
Created July 5, 2012 19:32
Virtual attributes #1
module VirtualAttributes
extend ActiveSupport::Concern
module ClassMethods
def attr_virtual(*attrs)
options = attrs.extract_options!
attrs.each do |attr|
class_eval do
send("attr_accessor", attr)