Skip to content

Instantly share code, notes, and snippets.

@Olefine
Olefine / metaprogrammin.rb
Created June 26, 2013 13:03
2 ways define method
class Module
def access(*args)
args.each do |arg|
instance_var = ("@" + arg.to_s)
define_method(arg.to_s + '=') {|val| instance_variable_set(instance_var, val)}
define_method(arg) { instance_variable_get(instance_var)}
end
end
def attr_access(*args)
@Olefine
Olefine / user.rb
Created July 9, 2013 10:55
user model
class User < ActiveRecord::Base
#default_scope where(:banned => false)
scope :unbanned, where(:banned => false)
include ChooseCity
rolify
acts_as_voter
has_karma(:questions, :as => :submitter, :weight => 0.5)
mount_uploader :avatar, AvatarUploader
@Olefine
Olefine / deploy.rb
Created July 12, 2013 07:52
tasks for faye server with thin
namespace :faye_server do
desc "Start faye server"
task :start do
run "cd #{current_path};RAILS_ENV=production bundle exec rackup faye.ru -s thin -E production -D -P tmp/pids/faye.pid"
end
desc "Stop faye server"
task :stop do
run "cd #{current_path};if [ -f tmp/pids/faye.pid ] && [ -e /proc/$(cat tmp/pids/faye.pid) ]; then kill -9 `cat tmp/pids/faye.pid`; fi"
end
@Olefine
Olefine / append.rb
Created July 20, 2013 09:30
append something into middle of file
def append_config_to_file
tempfile = File.open("file.rb", 'w')
f = File.new("config/application.rb")
f.each do |line|
tempfile << line
if line.match(/class Application/)
tempfile << " config.autoload_paths += %W(\#{config.root}/searches)\n"
end
end
f.close
@Olefine
Olefine / ru.yml
Created August 5, 2013 12:02
localize for russian
ru:
date:
abbr_day_names:
- Вс
- Пн
- Вт
- Ср
- Чт
- Пт
- Сб
@Olefine
Olefine / application.html.erb
Created August 9, 2013 09:12
bad code, from php programmer
<body>
<div id="sv-header">
<div class="sv-wrapper clearfix">
<div id="sv-search-box">
<%= render :partial=> 'layouts/sv-search-box' %>
</div>
<div id="sv-sign-box">
<% if user_signed_in? %>
<div class="sv-logged-box clearfix">
<div class="avatar"><div class="border"><a href="<%= user_path(current_user) %>"><%= image_tag current_user.avatar %></a></div></div>
@Olefine
Olefine / post.rb
Created August 13, 2013 10:42
rails model post
class Post < ActiveRecord::Base
resourcify
belongs_to :user
belongs_to :journal, :polymorphic => true
has_many :images, :class_name => "PostImage", :as => :imageable, :dependent => :destroy
has_one :illustration, :class_name => "PostIllustration", :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :illustration
accepts_nested_attributes_for :images,\
MacBook-Pro-egor.local:17096 on MESSAGES_QUEUE at just now Retry or Remove
Class
AddImageToMessage
Arguments
131
[{"image"=>{"original_filename"=>"kfc.png", "content_type"=>"image/png", "headers"=>"Content-Disposition: form-data; name=\"message[images_attributes][][image]\"; filename=\"kfc.png\"\r\nContent-Type: image/png\r\n", "tempfile"=>["�PNG\r\n", "\u001A\n", "\u0000\u0000\u0000\rIHDR\u0000\u0000\u0000�\u0000\u0000\u0000�\b\u0006\u0000\u0000\u0000�X��\u0000\u0000\u0000\u0006bKGD\u0000�\u0000�\u0000�����\u0000\u0000\u0000\atIME\a�\u0004\u0019\u000F\u0016!7\u0011\u001A-\u0000\u0000 \u0000IDATx���wt\u0014�����m��$��{�]D@\u0001�M�t\u0011\u0005EA�l(XP\u0004\u0015\u0010Q�H���S� �t�\"��P\u0013\b\u0010 !��-3�\u001FK����4\u0012���\u001C�w��\\6������\u001F��\u001Fv\u0011\n", "{\u0000�\u0018\u0001@\t \b�X\u001DK\u0000\u0012��\a?��\u001D�\u007F\u0014\u0004�\t�2��\u0016@#�\u0001P\u001D(\u000F�\\<�\u0000D\u0000\u0017�0�\fp\u0002�\u00170��P�� �O@�h\b�\u0000t�,\u0014b\u0001�#\u00118T]T��Qi���b�\n", "Q� {��\u0013�=$Av\u
class AmazonImageUploader
def initialize(id, file)
name = file[0][:image].original_filename
directory = "public/uploads/"
path = File.join(directory, name)
File.open(path, "wb") { |f| f.write(file[0][:image].read) }
@file = path
@id = id
end
class MessageImageWorker
@queue = :message_image_queue
def self.perform(message_id, image)
message = Message.find(message_id)
message.images << MessageImage.create!(image: image)
end
end