Skip to content

Instantly share code, notes, and snippets.

View dLobatog's full-sized avatar
🤗

Daniel Lobato García dLobatog

🤗
View GitHub Profile
@dLobatog
dLobatog / gist:1431933
Created December 5, 2011 01:34
Refactored create
# POST /issues
# POST /issues.xml
def create
@issue = Issue.new(params[:issue])
# A D D N E W C A U S E / E F F E C T
if params[:action_carrier]
# Read in the :type passed with form to recognize whether this is a Cause or Effect
@causality = params[:action_carrier].to_s
@causality_id = params[:id_carrier]
@dLobatog
dLobatog / gist:1431937
Created December 5, 2011 01:36
Old create method
# POST /issues
# POST /issues.xml
def create
@issue = Issue.new(params[:issue])
# A D D N E W C A U S E / E F F E C T
if params[:action_carrier]
# Read in the :type passed with form to recognize whether this is a Cause or Effect
@dLobatog
dLobatog / gist:1856734
Created February 18, 2012 01:22
Bad code
<% people = Person.find(
:conditions => ["added_at > ? and deleted = ?", Time.now.utc, false],
:order => "last_name, first_name") %>
<% people.reject { |p| p.address.nil? }.each do |person| %>
<div id="person-<%= person.new_record? ? "new" : person.id %>">
<span class="name">
<%= person.last_name %>, <%= person.first_name %>
</span>
<span class="age">
<%= (Date.today - person.birthdate) / 365 %>
<% if user.has_image %>
<td><%= image_tag(user.img, :size => "10x10") %></td>
<% end %>
resources :your_model, :id => /[a-z0-9-]*/
def img= image
self.has_image = true
AWS::S3.new.buckets[:images_cloudstock].objects[id].write(image.read)
end
def img
AWS::S3.new.buckets[:images_cloudstock].objects[id].url_for(:read) if has_image
end
class User < AWS::Record::HashModel
string_attr :id
string_attr :name
integer_attr :age
string_attr :sex
string_attr :city
boolean_attr :has_image, :default => false
@dLobatog
dLobatog / gist:2427588
Created April 20, 2012 10:13
find_all ruby
find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
@dLobatog
dLobatog / gist:2427596
Created April 20, 2012 10:15
find_all and cached
def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
cached(key, [name, prefix, partial], details, locals) do
find_templates(name, prefix, partial, details)
end
end
def cached(key, prefix, name, partial)
return yield unless key && caching?
@cached[key][prefix][name][partial] ||= yield
end
@dLobatog
dLobatog / gist:2427600
Created April 20, 2012 10:16
Benchmark
require "benchmark"
foo = "foo"
bar = "bar"
array = [foo, bar]
hash = {:a => foo, :b => bar}
nested_hash = Hash.new { |h,k| h[k] = {} }
nested_hash[foo][bar] = true