Skip to content

Instantly share code, notes, and snippets.

@mimosz
mimosz / counter_cache.rb
Created June 7, 2011 08:18
Mongoid counter cache
## 引入文件
# require 'mongoid/counter_cache'
## 插入Document行後
# include Mongoid::Relations::CounterCache
## 默認
# referenced_in :person, :inverse_of => :posts, :counter_cache => true
## 自定義
# referenced_in :person, :inverse_of => :posts, :counter_cache => :posts_count
module Mongoid
@mimosz
mimosz / activity.rb
Created June 12, 2011 04:47
Mongoid::Activity::Trackable
## 引入文件
# require 'mongoid/activity'
## 插入Document行後
# include Mongoid::Activity::Trackable
# 啟用跟蹤
# tracking :person => :follower, :scope => :followable, :create_bones => 5, :destroy_bones => -3
module Mongoid
module Activity
module Trackable
extend ActiveSupport::Concern
@mimosz
mimosz / active_admin_views_pages_base.rb
Created August 31, 2011 09:50 — forked from jocubeit/active_admin_views_pages_base.rb
Override footer content in Active Admin gem
# encoding: utf-8
ActiveAdmin::Views::Pages::Base.class_eval do
private
# Renders the content for the footer
def build_footer
div :id => "footer" do
para "Copyright © #{Date.today.year.to_s} #{link_to('Example.com', 'http://example.com')}. Powered by #{link_to('Active Admin', 'http://www.activeadmin.info')} #{ActiveAdmin::VERSION}".html_safe
end
end
end
@mimosz
mimosz / active_admin_dsl.rb
Created August 31, 2011 10:14
add awesome_nested_set helper nested_set_options Support On Active Admin
# /lib/active_admin_dsl.rb
require 'active_admin/dsl.rb'
include CollectiveIdea::Acts::NestedSet::Helper
@mimosz
mimosz / sign.html
Created September 14, 2011 05:47
郵件簽名模板
<body>
<sign signid='0'>
<style type='text/css'>
#signature{font-size:13px;font-family:'STHeiti Light';color:#9fa0a0}
#signature .b{font-size:21px;font-weight:700;margin-right:60px}
#signature .s{font-size:11px;font-style:italic}
#signature dl,#signature a{line-height:2;color:#9fa0a0}
#signature dl dt{width:60px;float:left;text-align:right;margin:0 .5em}
#signature dl dt:after{content:" : "}
#signature .logo{padding-right:13px;background:#fff;border-bottom-right-radius:18px;margin-bottom:-27px}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>库存更新结果</title>
<link href="../css/base.css" type="text/css" rel="stylesheet">
<style type="text/css">
body {
background: url('../images/allbg.gif');
@mimosz
mimosz / weibo.js
Created January 6, 2012 04:00
微博JS过滤
/**** 隐藏对象 ****/
function hide(el){
el.style.display='none';
}
/**** 找对象 ****/
function find(id){
return document.getElementById(id);
}
@mimosz
mimosz / group.rb
Created January 12, 2012 07:06
临时
weekly = 4.weeks.ago.beginning_of_day..4.weeks.ago.end_of_day # 时间段
conditions = Order.where(created_at:weekly).selector # 查询条件
reduce = <<javascript
function(doc, prev) {
if(doc.paid_at == '') {
prev.quantity += doc.quantity;
prev.payment += doc.payment;
}
if(prev.skus[doc.sku_id] == undefined) {
prev.skus[doc.sku_id] = {quantity: 0, payment:0};
@mimosz
mimosz / helper.rb
Created January 19, 2012 09:52
帮着优化优化呗~~
def show_props(str)
props = str.split(';') # 切割
if props.is_a?(Array)
str = "<dl class='props'>"
props.each do |property|
prop_arr = property.split(':') # 切割
if prop_arr.is_a?(Array) && prop_arr.count == 4
str += "<dt title='#{prop_arr[0]}'>#{prop_arr[2]}</dt><dd title='#{prop_arr[1]}'>#{prop_arr[3]}</dd>"
else
str += "<dt class='warning'>提示:</dt><dd>无属性</dd>"
@mimosz
mimosz / is_brand_sale.rb
Created April 20, 2012 06:13
Ruby:品牌特卖订单导出
require 'csv'
user = User.all[0]
user_id = user.nick.to_s
start_at = "2012-03-01".to_date
end_at = "2012-03-31".to_date
range = (start_at.beginning_of_day..end_at.end_of_day)
trades = Trade.where(seller_nick: user_id, pay_time: range, is_brand_sale: true)
CSV.open("./#{Time.now.to_i}.csv", "wb:GB18030") do |csv|
csv << ['订单号', '宝贝ID', '款式', '商家SKU', '数量','应付', '折扣', '实付', '付款时间', '发货时间', '退款ID']
trades.each do |trade|