RAILS中的 rubygem: cells ( cells rubygem )

访问量: 2019

这几天在弄Rails engine gem, 然后从 regular rubygem -> enginex -> rails plugin ->  Apotomo  -> 'cells'.    ( these days I am dealing with how to setup 'rails engine gem', the resources I have read are:  
1. regular rubygems doc.
2. enginex rubygem.  ( for rails 3.0.x)
3. rails plugin ( 3.1.x, 3.2.x ) 
4. apotomo  ( to run the gem's test)
5. cells. 

 今天试了下"cells" 这个rubygem, github上关注度到了1300+。 ( https://github.com/apotonick/cells)   

很简单,按照官方的文档一步一步做就可以了:

1. 在Gemfile中 :    gem 'cells'

2. 生成对应的scaffolding:    

$ rails generate cell cart show -e haml
  create  app/cells/
  create  app/cells/cart
  create  app/cells/cart_cell.rb
  create  app/cells/cart/show.html.haml
  create  test/cells/cart_test.rb

3. 可以在某个view中把 生成的cell views 嵌入进去

# in other views: 

controller:

# cells controller 
class CartCell < Cell::Rails
  def show(args)
    user    = args[:user]
    @items  = user.items_in_cart

    render  # renders show.html.haml
  end
end

# cells views

# show.html.haml
#cart
  You have #{@items.size} items in your shopping cart.

cells的优点: 可以减少 before filter, partial 和 helper的使用。 一个cell跟一个controller差不多。一个页面中可以包含多个cells.   

给我的感觉: 貌似比before_filter 好一些,确实是一个 Cart的不错的解决方案。 用起来跟controller一样,还可以在某个页面中多次使用。

(Say you’re writing a Rails online shop - the shopping cart is reappearing again and again in every view. You’re thinking about a clean solution for that part. A mixture of controller code, before-filters, partials and helpers? 

Cells are View Components for Rails. They look and feel like controllers. They don’t have no DoubleRenderError. They can be rendered everywhere in your controllers or views. They are cacheable, testable, fast and wonderful. They bring back OOP to your view and improve your software design. )

订阅/RSS Feed

Subscribe

分类/category