Skip to content

Instantly share code, notes, and snippets.

View paneq's full-sized avatar
🏠
Working from home

Robert Pankowecki paneq

🏠
Working from home
View GitHub Profile
@paneq
paneq / v01.rb
Created September 5, 2011 08:03
Methods for module
module Mailing
singleton_class.class_eval do
attr_accessor :engine
delegate :add, :clear, :each, :deliver, :to => :engine
end
end
@paneq
paneq / pointer.rb
Created September 6, 2011 09:16
pointer?
ruby-1.9.2-p290-fastrequire :017 > o.inspect
=> "#<Object:0x9b517fc>"
ruby-1.9.2-p290-fastrequire :018 > (o.object_id*2).to_s(16)
=> "9b517fc"
@paneq
paneq / application_controller.rb
Created September 22, 2011 11:14
skip loading all helper
class SkipAutoloadingAllHelpersController < ActionController::Base
# Fuck you, rails!
def self.helper(*params)
if params.include?(:all)
Rails.logger.warn("Rejected :all")
params.reject!{|p| p == :all}
end
super
end
@paneq
paneq / controller.rb
Created December 16, 2011 11:03
Typical controller action with setting flash
class PapersController < ApplicationController
def create
@paper = Paper.new(params[:paper])
if @paper.save
redirect_to @paper, notice: "Talk proposal submitted" # notice: I18n.t(:paper_created_flash_message)
else
flash[:alert] = "An error occurred while submitting a proposal"
render action: :new
@paneq
paneq / controller.rb
Created December 16, 2011 11:14
Proposed solution for controller / view communication.
class PapersController < ApplicationController
def create
@paper = Paper.new(params[:paper])
if @paper.save
redirect_to @paper, notice: :created
else
flash[:alert] = :invalid
render action: :new
@paneq
paneq / _form.html.erb
Created December 28, 2011 17:56
Initial situation
<%= form_for(@user) do |f| %>
<fieldset>
<div class="clearfix <%= @user.errors[:login].present? ? :error : nil %>">
<%= f.label :login %><br />
<div class="input">
<span class="help-block">
Your login. You know... login.
</span>
<% @user.errors[:login].each do |err| %>
<span class="help-block">
<p><%= error_message(err) %></p>
</end>
<% end%>
class User < ActiveRecord::Base
validate :login_length
private
def login_length
return if login.size >= 2 && login.size <= 10
errors.add(:login, {
code: :INVALID_LENGTH,
class InvalidLengthError
attr_accessor :min, :max, :current
private :min=, :max=, :current=
CODE = "INVALID_LENGTH"
DESCRIPTION = "Invalid length"
def initialize(min, max, current)
self.min = min
self.max = max
class InvalidLengthError
attr_accessor :min, :max, :current
private :min=, :max=, :current=
CODE = "INVALID_LENGTH"
DESCRIPTION = "Invalid length"
def initialize(min, max, current)
self.min = min
self.max = max