flatten_record, denormalizes插件,用于你现有的ActiveRecord模型

分享于 

4分钟阅读

GitHub

  繁體 雙語
An ActiveRecord plugin that denormalizes your existing ActiveRecord models
  • 源代码名称:flatten_record
  • 源代码网址:http://www.github.com/alvinsj/flatten_record
  • flatten_record源代码文档
  • flatten_record源代码下载
  • Git URL:
    git://www.github.com/alvinsj/flatten_record.git
    Git Clone代码到本地:
    git clone http://www.github.com/alvinsj/flatten_record
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/alvinsj/flatten_record
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    FlattenRecord Code ClimateBuild Status

    帮助denormalize实现现有ActiveRecord模型的ActiveRecord插件。

    它为 denormalize/展平规范化记录和生成表模式提供了一种简单的方法。

    示例

    现有标准化表

    订单客户 line_items
    ididid
    customer_id姓名描述
    折扣总计
    总计order_id

    非规范化表,由flatten_record生成

    denormalized_orders
    id
    order_id
    折扣
    总计
    customer_id
    customer_name
    line_item_id
    line_item_description
    line_item_total
    line_items_sum ( 自定义列)
    total_in_usd ( 自定义列)

    用法

    添加 gem 依赖项

    
    gem 'flatten_record'
    
    
    
    

    在新定义的模型中包含模块

    
    include FlattenRecord::Flattener
    
    
    
    

    定义非规范化

    
    class DenormalizedOrder <ActiveRecord::Base
    
    
     include FlattenRecord::Flattener
    
    
    
     denormalize :order, {
    
    
    
     # specifying association
    
    
     include: { 
    
    
     # :belongs_to association
    
    
     customer: {}
    
    
    
     # :has_many association, create multiple denormalized records 
    
    
     line_items: {}
    
    
    
     },
    
    
    
     # save results of methods defined in Normalized model
    
    
     methods: {
    
    
     total_in_usd: :decimal 
    
    
     },
    
    
    
     # compute results of methods defined in Denormalized model
    
    
     compute: {
    
    
     line_items_sum: { type: :decimal, default: 0 } 
    
    
     }
    
    
     }
    
    
    
     private
    
    
     def compute_line_items_sum(order)
    
    
     order.line_items.collect(&:total).inject(:+)
    
    
     end
    
    
    end
    
    
    
    class Order <ActiveRecord::Base
    
    
     def total_in_usd
    
    
     # calculation
    
    
     end
    
    
    end
    
    
    
    

    生成迁移文件

    基于定义生成迁移文件

    
    $ rails generate flatten_record:migration denormalized_order
    
    
     create db/migrate/20140313034700_create_table_denormalized_orders.rb 
    
    
    
    

    更新定义并生成新的迁移文件

    
    $ rails generate flatten_record:migration denormalized_order
    
    
    Warning. Table already exists: denormalized_orders
    
    
    Generating migration based on the difference..
    
    
    Add columns: line_items_description
    
    
     create db/migrate/20140313034736_add_line_items_description_to_denormalized_orders.rb 
    
    
    
    

    使用denormalizer方法

    创建记录

    
    irb(main)> DenormalizedOrder.create_with(order)
    
    
    
    

    删除记录

    
    irb(main)> DenormalizedOrder.destroy_with(order)
    
    
    
    

    更新记录

    
    irb(main)> DenormalizedOrder.update_with(order)
    
    
    
    

    API Documentation/Development/Discussion

    请参考维基

    版本

    #####v1

    #####v0

    贡献者

    许可证

    查看mit许可证。


    plugin  模式  act  Active  exi  Activerecord