Skip to content

Instantly share code, notes, and snippets.

# In Layouts
<head>
<title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
<%= javascript_include_tag :defaults %>
<%= stylesheet_link_tag "style", "theme" %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
<%= javascript_include_tag "admin" %>
<% if @cartridge&&@printers %>
<script type="text/javascript">
<input type="text" class="add_to_cart_quantity">
<input type="submit" class="add_to_cart_button">
<input type="text" class="add_to_cart_quantity">
<input type="submit" class="add_to_cart_button">
@janjiss
janjiss / Can something be done better?.rb
Created March 24, 2011 17:15
Can something be done better?
# AJAX request: This adds line items to cart.
def add_line_item_to_cart
#Get current_cart object
current_cart
quantity = params[:quantity].to_i
id = params[:id].to_i
line_item = LineItem.find(:first, :conditions => {:cartridge_id => id, :cart_id => current_cart.id})
#If there is line item wich maches conditions then just update quantity
#Returns quantity with total of items in cart
if line_item != nil
#In controller
@clients_ip = request.remote_ip
@comments = @user.comments.ordered
#In views
<% for comment in @comments %>
<div class="comment <%= gender_check(comment.gender) %>">
<div class="created_at"><%= comment.created_at.strftime("%H:%M") %></div>
<div class="comment_rating">
<span class="rating"><%= comment.rating %></span>
<span class="thumbs" value="<%= comment.id %>">
@janjiss
janjiss / gist:1075915
Created July 11, 2011 14:07
Does not set to true even if question count is more than progress
class Tracker < ActiveRecord::Base
attr_accessible :survey_id, :respondent_id, :progress, :completed
belongs_to :survey
after_save :set_completed?
#set tracker.completed to true if all questions are answered
def set_completed?
if progress >= survey.questions.count
[25] pry(main)> t.route_ids = [1,1,1]
=> [1, 1, 1]
[26] pry(main)> t.route_ids
=> [1]
[25] pry(main)> t.route_ids = [1,1,1]
=> [1, 1, 1]
[26] pry(main)> t.route_ids
=> [1]
@janjiss
janjiss / gist:2945860
Created June 17, 2012 22:05
MongoDB vs MySQL test for creating and destroying 10000 records
# MongoDB vs MySQL test for creating and destroying 10000 recordst.
# Was using faker gem to generate random text for body and content
# Code for Benchmark
def self.do_bm
Benchmark.bm do |x|
x.report do
10000.times do
self.create(:title => Faker::Lorem.sentence, :body => Faker::Lorem.paragraph(10))
def new
if current_user && current_user.has_products?
redirect_to :action => :edit
elsif current_user && current_user.has_no_products?
current_user.create_product!
redirect_to :action => :edit
else
redirect_to root_url
end
end
require 'mime/types'
#
# Built with massive inspiration (ok, more like following their lead) from the following:
# - https://github.com/JackDanger/file-uploader/commit/03ed9ba68d46805e22a0014ac0eee9ecbd5acd8d
# - https://github.com/newbamboo/example-html5-upload/blob/master/lib/rack/raw_upload.rb
#
class RawFileUpload
def initialize(app)
@app = app