Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Edison Machado edison

🏠
Working from home
View GitHub Profile
@edison
edison / classify.rb
Created May 6, 2018
Benchmarking de Métodos de Classificação - Estrutura de Dados
View classify.rb
def bubble_sort(array)
n = array.length
swapped = true
while swapped do
swapped = false
(n - 1).times do |i|
if array[i] > array[i + 1]
array[i], array[i + 1] = array[i + 1], array[i]
@edison
edison / controller.rb
Created Jan 28, 2015
Devise Sessions Controller for Ajax
View controller.rb
class Website::SessionsController < ::Devise::SessionsController
respond_to :js
layout false
# POST /resource/sign_in
def create
self.resource = warden.authenticate(auth_options)
if resource && resource.active_for_authentication?
sign_in(resource_name, resource)
View gist:5390686
# Amount should be a decimal between 0 and 1. Lower means darker
def darken_color(hex_color, amount=0.4)
hex_color = hex_color.gsub('#','')
rgb = hex_color.scan(/../).map {|color| color.hex}
rgb[0] = (rgb[0].to_i * amount).round
rgb[1] = (rgb[1].to_i * amount).round
rgb[2] = (rgb[2].to_i * amount).round
"#%02x%02x%02x" % rgb
end
View redis-server
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
View localizacao.md

Estrutura do arquivo .yml de localizacao:

  pt:
    produtos:
      cadastro:
        salvar: Salvar produto

Utilizando o helper t para traduzir nas views:

View errors.yml
pt:
activerecord:
attributes:
quote:
content: Frase
author: Autor
errors:
models:
quote:
View pt.yml
pt:
activerecord:
attributes:
quote:
content: Frase
author: Autor
View index.erb
<% if @quotes.any? %>
<table class="table table-striped">
<tbody>
<% @quotes.each do |quote| %>
<tr>
<td><%= quote.content %></td>
<td><%= link_to 'Excluir', quote_path(quote), method: :delete, confirm: 'Tem certeza?', class: 'btn' %></td>
</tr>
<% end %>
</tbody>
View _quote.erb
<div class="hero-unit">
<% if @quote %>
<blockquote>
<p><%= @quote.content %></p>
<small><%= @quote.author %></small>
</blockquote>
<% else %>
<p>Nenhuma frase cadastrada.</p>
<% end %>
</div>
View controller.rb
class QuotesController < ApplicationController
def index
@quotes = Quote.all
respond_to :html
end
def home
@quote = Quote.all.sort_by{rand}.slice(0,10).first
respond_to :html
end
You can’t perform that action at this time.