Skip to content

Instantly share code, notes, and snippets.

View Alir3z4's full-sized avatar
💭
Rock'nRolla

Alireza Savand Alir3z4

💭
Rock'nRolla
View GitHub Profile
@Alir3z4
Alir3z4 / models.rb
Created April 5, 2011 17:53
You cannot call create unless the parent is saved
# OLD One with error => 'You cannot call create unless the parent is saved'
def number_list=(numbers_string)
self.numbers.destroy_all
numbers = numbers_string.split(",").collect{|num| num.strip.downcase}.uniq
numbers.each do |number|
num = self.numbers.find_or_create_by_content(number)
end
end
@Alir3z4
Alir3z4 / models.rb
Created April 5, 2011 17:54
Improved one base on condition, bad coding but it's work
# Improved one base on condition, bad coding but it's work!
def number_list=(numbers_string)
self.numbers.destroy_all
numbers = numbers_string.split(",").collect{|num| num.strip.downcase}.uniq
numbers.each do |number|
unless !self.save
num = self.numbers.find_or_create_by_content(number)
end
end
end
@Alir3z4
Alir3z4 / schema.rb
Created April 5, 2011 17:58
Nested Model & Controller & Route
ActiveRecord::Schema.define(:version => 20110221190024) do
create_table "comments", :force => true do |t|
t.string "title"
t.text "body"
t.integer "commentable_id"
t.string "commentable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
@Alir3z4
Alir3z4 / twitter_controller.rb
Created April 5, 2011 18:11
twitter clone controller by Rails Tutorial
class UsersController < ApplicationController
before_filter :authenticate, :except => [:show, :new, :create]
before_filter :correct_user, :only => [:edit, :update]
before_filter :admin_user, :only => :destroy
def index
@title = "All users"
@users = User.paginate(:page => params[:page])
end
@Alir3z4
Alir3z4 / main.cpp
Created April 6, 2011 06:59
hello world with Qt/C++
#include <QApplication>
#include <QLabel>
int main(int argc, char **argv)
{
QApplication App(argc, argv);
QLabel Lbl("Hello KhoshGel!");
Lbl.show();
App.exec();
}
@Alir3z4
Alir3z4 / plupload_init.js
Created April 16, 2011 02:42
using plupload and with django to limit files uploading with some effect!
var maxfiles = {{ maxfiles }}; // {{ maxfiles }} returns from views.py
$(function() {
$("#uploader").pluploadQueue({
runtimes : 'gears', // Initialize gears runtime
url : '{{ obj.get_image_upload_url }}', // Url to current object image uploading url
max_file_size : '4mb',
chunk_size: '4mb',
multiple_queues : true,
multi_selection: false, // It's necessary for file uploading limit
rename: true,
@login_required
def delete(request, msg_pk):
msg = get_object_or_404(Message, pk=msg_pk)
## is owner?
if request.user != msg.user and request.user != msg.to:
raise Http404()
# Sender or Receiver ?
if msg.delete_status == 2:
msg.delete_status = 3
elif request.user == msg.user and request.user != msg.to:
def clean(self):
from decimal import Decimal
cleaned_data = self.cleaned_data
price_mode = cleaned_data.get('price_mode')
price = cleaned_data.get('price')
if price_mode == u'0':
if price:
return self.cleaned_data['price']
return self.cleaned_data['price_mode']
else:
from django.contrib import admin
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=60)
created = models.DateTimeField(auto_now_add=True)
priority = models.IntegerField(default=0)
difficulty = models.IntegerField(default=0)
done = models.BooleanField(default=False)
const std::vector<std::string> toStdVector(const QVector<QString> &q_vect)
{
std::vector<string> std_vect;
for (int i = 0; i < q_vect.size(); ++i) {
std_vect.push_back(q_vect[i].toStdString());
}
return std_vect;
}