Skip to content

Instantly share code, notes, and snippets.

@pete-a
pete-a / example-01.rb
Last active August 29, 2015 14:23
Switch vs Duck Typing
order_type = params[:order_type]
if(order_type == 'chronological')
products.order_by_date
elsif(order_type == 'alphabetical')
products.order_by_name
elsif(order_type == 'price')
products.order_by_price
else
# ... order alist by some default
@pete-a
pete-a / aspect02.html
Last active August 29, 2015 14:22
Oomph 2.0 concept
<aspect min-screen="lg" ratio="0.5625">
<oomph-analytics job-id="1542" />
<animation id="anim01" type="move-in-from-left" />
<layer z-index="0" background-src="./aspect02/layer0/lg0535.jpg">
<ips id="ips01" width="53%" height="30%" left="10%" top="5%">
<ips-slide background-src="./aspect02/ips01/01.jpg">
<link href="http://www.google.com" top="90%" left="80%" width="20%" height="10%" />
</ips-slide>
<ips-slide background-src="./aspect02/ips01/02.jpg" /></ips-slide>
@pete-a
pete-a / original.rb
Last active August 29, 2015 14:17
Nested if refactor
# https://github.com/mogeneration/adomatic/blob/ruth/feature/duplicate_job/app/controllers/jobs_controller.rb#L32
if check_required_fields @job
if @job.save
@job.send_status_email
if current_user.is_admin?
redirect_to admin_organisation_job_queue_job_url(@job.job_queue.organisation, @job.job_queue, @job), notice: 'Job was successfully created.'
else
redirect_to job_queue_job_url(@job.job_queue, @job), notice: 'Job was successfully created.'
end
else
duplicate_fields = (url) ->
if $('.duplicatable_nested_form').length == 1 && (typeof $('#upload_content_location').attr('value') == 'undefined')
$('#upload_content_location').attr('value', url)
return
if $('.duplicatable_nested_form').length > 0
lastNestedForm = $('.duplicatable_nested_form').last()
nestedForm = $('.duplicatable_nested_form').last().clone()
update_clone_block(nestedForm[0], url)
$( nestedForm ).insertAfter( lastNestedForm )
@pete-a
pete-a / refactor-example.rb
Last active August 29, 2015 13:56
Refactor example
# GET /everything
def index
@page = params[:page] ||= 1 # Set as 1 if page number not provided
gon.current_page = @page # Not sure I'm a fan of setting JS variables this way, but will leave for now
@shots = Shot.seen_by(current_user).paginate(page: @page)
@notifications = Notification.unseen_counts(shots: @shots, user: current_user) # Not great, but better
end
<% flash.each do |key, value| %>
<p class="<%= key %>"><%= value %></p>
<% end %>