Skip to content

Instantly share code, notes, and snippets.

View sergiomaia's full-sized avatar
💭
Focused

sergiomaia sergiomaia

💭
Focused
View GitHub Profile
@sergiomaia
sergiomaia / rp.rb
Created May 26, 2012 13:42
Random Passwords
#acho que da pra fazer assim, veja se funciona.
class RandomPasswords
def generate(size = 8)
chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('1'..'9').to_a
(1..size).collect {|c| chars[ rand(chars.size) ]}.join
end
end
#nesse caso uma senha com 8 caracteres, se precisar ser maior ou menor mude o parâmetro ... dohhhh!
@sergiomaia
sergiomaia / gist:5456120
Created April 24, 2013 22:26
Controller /action /create
MessagesController /action /create
def create
message = current_user.messages.build(params[:message])
message.created_at = Time.now # HACK
message.save!
respond_with(@messages, :location => messages_url)
end
Message.joins(:department_targets).joins(:user).where("department_targets.department_id = ? OR users.department_id = ?", department_id, department_id)
@sergiomaia
sergiomaia / gettemp.rb
Last active August 28, 2015 01:02 — forked from henriqueutsch/chartkick.js
Temperature monitor raspberry pi DS18B20 sensor
require "json"
def temp()
number = Dir.glob('/sys/bus/w1/devices/*').count-1
if number>0
@temperatures = Array.new
Dir.glob('/sys/bus/w1/devices/*').each_with_index {|folder, index|
values = Hash.new
foldername = folder.gsub('/sys/bus/w1/devices/','')
@sergiomaia
sergiomaia / test_helper.rb
Created September 15, 2016 00:21
Default Rails tests to show red and green
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
@sergiomaia
sergiomaia / Guardfile
Created September 15, 2016 00:24
Automated tests with Guard
# Defines the matching rules for Guard.
guard :minitest, spring: true, all_on_start: false do
watch(%r{^test/(.*)/?(.*)_test\.rb$})
watch('test/test_helper.rb') { 'test' }
watch('config/routes.rb') { integration_tests }
watch(%r{^app/models/(.*?)\.rb$}) do |matches|
"test/models/#{matches[1]}_test.rb"
end
watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
resource_tests(matches[1])
@sergiomaia
sergiomaia / addhashtag.rb
Created October 4, 2016 23:24
Add Hashtag
# para esse exemplo pressupõe-se que você tenha um recurso post com o campo >> content:string
#Primeiro criamos um model Tag
rails g model Tag name:string
# >> crie uma tabela CreatePostsTags com post_id e tag_id (para nosso relacionamento has_and_belongs_to_many)
rails g migration CreatePostsTags post:references tag:references
# >> modifique a migration CreatePostsTags add :id => false, já que não precisaremos de id nesta tabela
class CreatePostsTags < ActiveRecord::Migration[5.0]
1.upto(100) do |i|
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
# config/routes.rb
resources :resgistration, only: [:new, :create]
# app/controllers/registration_controller.rb
class RegistrationsController < ApplicationController
respond_to :html
def new
@registration = Registration.new
end
def create
@registration = Registration.new(registration_params)