Flexbox, 更优雅的布局

我关于 Flexbox 最初到知识来自于 Flexbox,更优雅的布局 这篇帖子, 而实际练习到的例子来自于:

  • http://css-tricks.com/snippets/css/a-guide-to-flexbox/

Basics & Terminology

<div class='container basics-terminology-container' style='padding:0;'>
  <div class='item basics-terminology-item' style='width:90px;'></div>
  <div class='item basics-terminology-item' style='width:160px;'></div>
  <div class='item basics-terminology-item' style='width:90px;'></div>
</div>
.contanier{display: flex;}

要想知道 flex 是干什么用的,我们把 .contanier{display: flex;} 注释掉,看看会发生什么,点击这里注释掉 flex. 点击这里恢复 flex.

SqlKnit, 帮助你写 sql, 开发中

在公司的 ruby 项目中需要维护一些比较复杂的 sql, 这些 sql 是由字符串加一些 ruby 变量拼凑出来的。 比如下面的sql,

select distinct d.id ds_id,
       ad.firstname || ' ' || ad.lastname as name1,
       ad.joint_firstname || ' ' || ad.joint_lastname as name2,
       ad.city,
       st.name state,
       c.iso country,
       to_char(u.entry_date, 'YYYY-MM-DD') as join_date,
       case sq2.role_id when null then 'online' when 2 then 'online' else 'phone' end
       from
         (select user_id, count(1) from rs_uss group by user_id having count(1) < 2) sq,
         (select uc.id, uc.entry_operator, ru.role_id from users uc left join users up on (uc.entry_operator = up.id), roles_users ru where ru.user_id = up.id) sq2,
        ds d, roles_users ru, addresses ad, countries c, states st, users u
      left join users sponsor_users on (sponsor_users.id = u.entry_operator)
      where
       u.entry_date >= '#{query_params[:start_date]}' and u.entry_date < '#{query_params[:end_date]}' and
       sq2.id = u.id and
       sq.user_id = ru.user_id
	   and ru.role_id = 2
	   and d.user_id = u.id
	   and u.id = sq.user_id
	   and u.sold_address_id = ad.id
	   and ad.country_id = c.id
	   and ad.state_id = st.id

使用字符串加 ruby 变量拼凑出 sql 有两大缺点:

  1. 难以格式化
  2. 如果需要加入条件判断,会比较麻烦

于是我想到用 ruby 写一个 sql builder, 语法类似于下面的例子:


    @needle.sql_from :ors do
      join(:l_iems).on(:ors)
      join(:variants).on(:l_iems)
      join(:pdus).on(:variants)
      join(:pdus_taxons).on(:pdus)
      join(:st_es).on do
        text "st_es.stateful_id = ors.id"
        and
        st_es name: 'payment',  stateful_type: 'Order', next_state: ['paid', 'credit_owed']
      end
      left_join(:shipts).on(:ors)
      left_join(:wares).on(:shipts)
      left_join(:ues).on(:ors)
      left_join(:addres, as: 's_addr').on(:ues)
      left_join(:dists).on(:ues)
      left_join(:addres).on('ors.ship_address_id = :.id')
      left_join(:add_ads).on(:addres)
      left_join(:sts).on(:addres)
      left_join(:cts).on(:addres)
      left_join(:currs).on(:ors)
      left_join(:adjusts, as: 'shipping_adjusts').on do
        shipping_adjusts label: 'Shipping', source_type: 'Shipment'
        text "ors.id = :.order_id"
        text ":.source_id = shipts.id"
      end
      left_join(:adjusts, as: 'gst_adjusts').on do
        gst_adjusts label: 'gst', source_type: 'Order'
        text "ors.id = :.order_id"
        text ":.source_id = ors.id"
      end
      left_join(:adjusts, as: 'gst_shipping_adjusts').on do
        gst_shipping_adjusts label: 'gst_shipping', source_type: 'Order'
        text "ors.id = :.order_id"
        text ":.source_id = ors.id"
      end
    end
	

目前这个 sql builder 还在开发中, 项目地址: https://github.com/baya/sqlknit

Fence(篱笆), 一个权限管理系统, 设计中

与 Cancan 等权限系统不同的是,Fence 本身并不定义权限, 而是把需要作访问控制的的代码圈起来,然后在后台控制哪些用户或者角色能够访问这 些被’篱笆’圈起来的代码。例子如下:


  fence 'update user name' do
    user.name = params[:name]
	user.save
  end
  
  class OrdersController

    def new
	end

    def create
	end

    fence 'create order', :new, :create
	
  end

然后后台会有类似下面的表格:

fences
id name
1 update user name
2 create order
user_fences
user_id fence_id
2009 1

id 为 2009 的这个用户可以访问下面的代码,

  user.name= params[:name]
  user.save

Ruby excel parser

这周主要用到了 rooaxlsx 来处理 Excel 文件。

我使用 roo 来读取和编辑已经存在的 excel 文件,使用 axlsx 来生成新的 excel 文件。


p = Axlsx::Package.new
p.workbook.add_worksheet(:name => "Basic Worksheet") do |sheet|
  sheet.add_row ["First Column", "Second", "Third"]
  sheet.add_row [1, 2, 3]
end
p.use_shared_strings = true
p.serialize('simple.xlsx')

# 或者

File.open('simple.xlsx', 'w') {|f| f.write p.to_stream.read }

使用 pg 连接 postgresql 数据库

conn = PG.connect(dbname: 'ggg', port: 11111, user: 'xxx', host: '127.0.0.1')

sql =<<-EOF
  long sql string ...
EOF

result = conn.exec(sql)